- 在自动装配时,spring不在意装配的bean来自哪里。
一、在JavaConfig中引用xml配置
1.1、注入java实体
1、定义接口
public interface CompactDisc {
void play();
}
2、定义实现类
public class SgtPepper implements CompactDisc {
public void play() {
System.out.println("sgt-pepper play ... ...");
}
}
3、定义配置文件
@Configuration
public class CDConfig {
@Bean
public CompactDisc compactDisc() {
return new SgtPepper();
}
}
1.2、添加实体类并引用配置文件
1、定义java类
public class CDPlayer {
private CompactDisc compactDisc;
public CDPlayer(CompactDisc compactDisc) {
this.compactDisc = compactDisc;
}
public void play() {
compactDisc.play();
}
}
2、创建配置文件
@Configuration
@Import(CDConfig.class)
public class CDPlayerConfig {
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
- 说明:
- @Import(CDConfig.class):注入配置类CDConfig
3、创建测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CDPlayerConfig cdPlayerConfig;
@Autowired
private CDConfig cdConfig;
@Test
public void getBean() {
CompactDisc compactDisc = cdConfig.compactDisc();
CDPlayer cdPlayer = cdPlayerConfig.cdPlayer(compactDisc);
cdPlayer.play();
}
}
- 说明:
- @ContextConfiguration(classes = CDPlayerConfig.class):指定spring的配置文件
- 在CDPlayerConfig配置文件中注入了CDConfig,所以可以直接注入CDConfig和CDPlayerConfig
1.3、引用多个配置文件
1、创建配置文件
@Configuration
@Import({CDConfig.class, CDPlayerConfig.class})
public class SystemConfig {
}
2、创建配置类
@Configuration
@Import({CDConfig.class, CDPlayerConfig.class})
public class SystemConfig {
}
- 说明:
- @Import({CDConfig.class, CDPlayerConfig.class}):同时注入两个配置类
3、测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SystemConfig.class)
public class SystemConfigTest {
@Autowired
private CDPlayerConfig cdPlayerConfig;
@Autowired
private CDConfig cdConfig;
@Test
public void getBean() {
CompactDisc compactDisc = cdConfig.compactDisc();
CDPlayer cdPlayer = cdPlayerConfig.cdPlayer(compactDisc);
cdPlayer.play();
}
}
1.4、在config中引用xml配置文件
1、创建实体类
public class MediaPlayer implements CompactDisc {
private String title;
private String desc;
private List<String> list;
public MediaPlayer(String title, String desc, List<String> list) {
this.title = title;
this.desc = desc;
this.list = list;
}
public void play() {
System.out.println("title : " + title);
System.out.println("desc : " + desc);
System.out.println();
for (String data : list) {
System.out.println(data);
}
}
}
2、创建配置类
@Configuration
@Import(CDPlayerConfig.class)
@ImportResource("classpath:bean.xml")
public class JavaAndXmlConfig {
}
- 说明:
- @ImportResource(“classpath:bean.xml”):注入配置文件
3、创建配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mediaPlayer" class="com.demo.MediaPlayer">
<constructor-arg value="test-title" name="title"/>
<constructor-arg name="desc" value="test-desc"/>
<constructor-arg name="list">
<list>
<value>test-list-1</value>
<value>test-list-2</value>
<value>test-list-3</value>
</list>
</constructor-arg>
</bean>
</beans>
4、创建测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JavaAndXmlConfig.class)
public class JavaAndXmlConfigTest {
@Autowired
private MediaPlayer mediaPlayer;
@Autowired
private CDPlayerConfig cdPlayerConfig;
@Autowired
private CDConfig cdConfig;
@Test
public void getMedia() {
mediaPlayer.play();
}
@Test
public void getBean() {
CompactDisc compactDisc = cdConfig.compactDisc();
CDPlayer cdPlayer = cdPlayerConfig.cdPlayer(compactDisc);
cdPlayer.play();
}
}
- 说明:
- JavaAndXmlConfig配置类中注入了CDPlayerConfig和CDConfig和配置文件,因此可以直接注入CDPlayerConfig,CDConfig,MediaPlayer