Spring @Import和@ImportResource詳解

概述:
@Import註解是引入帶有@Configuration的java類。

@ImportResource是引入spring配置文件.xml

demo項目結構(Maven約定):

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── jiaobuchong
    │   │           ├── config
    │   │           │   ├── CDConfig.java
    │   │           │   ├── CDPlayerConfig.java
    │   │           │   └── SoundSystemConfig.java
    │   │           ├── dao
    │   │           │   └── CompactDisc.java
    │   │           │   └── MediaPlayer.java
    │   │           └── soundsystem
    │   │               ├── BlankDisc.java
    │   │               ├── CDPlayer.java
    │   │               └── SgtPeppers.java
    │   └── resources
    │       └── cons-injec.xml
    └── test
        └── java
            └── com
                └── jiaobuchong
                    ├── soundsystem
                        ├── CDPlayerTest.java
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

dao下兩個interface:

CompactDisc.java:

package com.jiaobuchong.dao;

public interface CompactDisc {
    void play();
}
  • 1
  • 2
  • 3
  • 4
  • 5

MediaPlayer.java

package com.jiaobuchong.dao;

public interface MediaPlayer {
    void play();
}
  • 1
  • 2
  • 3
  • 4
  • 5

soundsystem包下的interface實現類:

BlankDisc.java:

package com.jiaobuchong.soundsystem;

import com.jiaobuchong.dao.CompactDisc;

import java.util.List;

public class BlankDisc implements CompactDisc {
    private String title;
    private String artist;
    private List<String> tracks;

    public BlankDisc(String title, String artist, List<String> tracks) {
        this.title = title;
        this.artist = artist;
        this.tracks = tracks;
    }

    public void play() {
        System.out.println("Playing " + title + " by " + artist);
        for (String track : tracks) {
            System.out.println("-Track: " + track);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

CDPlayer.java:

package com.jiaobuchong.soundsystem;

import com.jiaobuchong.dao.MediaPlayer;
import org.springframework.beans.factory.annotation.Autowired;

public class CDPlayer implements MediaPlayer {
    private CompactDisc cd;

    public CDPlayer(CompactDisc cd) {
        this.cd = cd;
    }

    public void play() {
        cd.play();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

SgtPeppers.java:

package com.jiaobuchong.soundsystem;

import com.jiaobuchong.dao.CompactDisc;
import org.springframework.stereotype.Component;

public class SgtPeppers implements CompactDisc {
    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

config包下的三個配置類

CDConfig.java:

package com.jiaobuchong.config;

import com.jiaobuchong.dao.CompactDisc;
import com.jiaobuchong.soundsystem.SgtPeppers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration  // @Configuration註解表示定義一個配置類
public class CDConfig {
            // 方法名heyGirl即是bean的name
    @Bean   // 將SgtPeppers註冊爲 SpringContext中的bean
    public CompactDisc heyGirl() {
        return new SgtPeppers();  // CompactDisc類型的
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

@Bean註解等同於xml的配置:

<beans>
   <bean name="heyGirl" class="com.jiaobuchong.soundsystem.SgtPeppers" />
</beans>
  • 1
  • 2
  • 3

CDPlayerConfig.java:

package com.jiaobuchong.config;

import com.jiaobuchong.dao.CompactDisc;
import com.jiaobuchong.soundsystem.CDPlayer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(CDConfig.class)  //導入CDConfig的配置
public class CDPlayerConfig {
    @Bean(name = "cDPlayer")
    public CDPlayer cdPlayer(CompactDisc heyGirl) {  
    /* 這裏注入的bean是
    上面CDConfig.java中的name爲heyGirl的CompactDisc類型的bean*/
        return new CDPlayer(heyGirl);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

這裏的註解@Bean等同於xml配置的元素,

<bean name="cDPlayer" class="com.jiaobuchong.soundsystem.CDPlayer">
       <property name="cd" ref="heyGirl" />
</bean>
  • 1
  • 2
  • 3

SoundSystemConfig.java:

package com.jiaobuchong.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

@Configuration
@Import(CDPlayerConfig.class)  
@ImportResource("classpath:cons-injec.xml") //導入xml配置項
public class SoundSystemConfig {

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

@ImportResource等同於xml配置:

<import resource="cons-injec.xml" />
  • 1

xml配置文件

cons-injec.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       ">
       <bean id="compactDisc"
             class="com.jiaobuchong.soundsystem.BlankDisc"
             c:_0="Sgt. Pepper's Lonely Hearts Club Band"
             c:_1="The Beatles">
              <constructor-arg>
                     <list>
                            <value>Sgt. Pepper's Lonely Hearts Club Band</value>
                            <value>With a Little Help from My Friends</value>
                            <value>Lucy in the Sky with Diamonds</value>
                            <value>Getting Better</value>
                            <value>Fixing a Hole</value>
                            <!-- ...other tracks omitted for brevity... -->
                     </list>
              </constructor-arg>
       </bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

測試類

CDPlayerTest.java:

package com.jiaobuchong.soundsystem;

import com.jiaobuchong.config.CDConfig;
import com.jiaobuchong.config.CDPlayerConfig;
import com.jiaobuchong.dao.CompactDisc;
import com.jiaobuchong.dao.MediaPlayer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/*
CDPlayerTest takes advantage of Spring’s SpringJUnit4ClassRunner to have a Spring application
context automatically created when the test starts. And the @Context-Configuration annotation
tells it to load its configuration from the CDPlayerConfig class.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDConfig.class)
public class CDPlayerTest {

    @Autowired
    private MediaPlayer mediaPlayer;

    @Autowired
    @Qualifier("heyGirl")
    private CompactDisc keyGirl;

    @Autowired
    @Qualifier("compactDisc")
    private CompactDisc cd;

    @Test
    public void beanTest() {
        mediaPlayer.play();
        cd.play();
        keyGirl.play();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章