第2章 装配Bean---xml和javaConfig混合使用装配---笔记5
2017-06-20 15:42
483 查看
概述:
在典型的Spring应用中,我们可能会同时使用自动化和显式配置。即便你更喜欢通过JavaConfig实现显式配置,但有的时候XML却是最佳的方案。
来个珠帘合璧
1.在javaConfig中引用xml配置
2.在xml引用javaConfig配置
配置1 CDPlayerConfig1
package learn.chapter2.javaConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import learn.chapter2.AdeleSong;
import learn.chapter2.AnanRyokoSong;
import learn.chapter2.CompactDisc;
import learn.chapter2.KeshaSong;
import learn.chapter2.LadyGagaSong;
@Configuration
@Import(CDPlayerConfig2.class)
public class CDPlayerConfig1 {
@Bean (name="adeleSong") //如果不写name默认就是方法名
public CompactDisc getAdeleSong(){
return new AdeleSong();
}
/**
* 每次注入的bean不是确定的
* @return
*/
@Bean
public CompactDisc randomBeanLesCD() {
int choice = (int) Math.floor(Math.random()*4);
if(choice == 0) {
return new AdeleSong();
}else if(choice ==1) {
return new KeshaSong();
}else if(choice == 2) {
return new LadyGagaSong();
} else {
return new AnanRyokoSong();
}
}
}
配置2 CDPlayerConfig2
总结:
1、在配置1中用import引入配置2
2、注意配置2引用的randomBeanLesCD来自配置1,说明最后它们是一个整体呈现
测试类:
package learn.chapter2.javaConfig; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import learn.chapter2.CompactDisc; import learn.chapter2.GirlSong; @Configuration public class CDPlayerConfig2 { /** * 这是最佳的方式,这不限制的本类的注入的bean 还可以是其他JavaConfig配置的bean * @param compactDisc * @return */ @Bean public GirlSong constructor(@Qualifier("randomBeanLesCD")CompactDisc compactDisc){ return new GirlSong(compactDisc); } }其实还可以用一个统计的配置的统一import
去掉配置1 的@import注解
package learn.chapter2.javaConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({CDPlayerConfig1.class, CDPlayerConfig2.class})
public class CDPlayerConfigAll {
}
如何引用xml配置的bean? 答案就是用@ImportResource("配置文件路径")
learn/chapter2/constructor.xml 获取blankDisc
<?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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- property帮助标签 util,首先要引入命名空间 -->
<!-- 它可以将list单独分开出来,其它bean可以复用 -->
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
<util:map>
<entry key="">
<value></value>
</entry>
</util:map>
<bean id="blankDisc"
class="learn.chapter2.BlankDisc"
p:title="hello"
p:artist="adele"
p:tracks-ref="trackList"></bean>
</beans>
总结:在该类头部引用@ImportResource(“路径”)
package learn.chapter;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import learn.chapter2.CompactDisc;
import learn.chapter2.GirlSong;
import learn.chapter2.javaConfig.CDPlayerConfig1;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig1.class)
public class JavaConfigSeparateTest {
@Autowired
@Qualifier("constructor")
private GirlSong girlSong;
@Autowired
@Qualifier("blankDisc")
private CompactDisc compactDisc;
@Test
public void isNotNull(){
assertNotNull(girlSong);
}
@Test
public void getBlankDisc(){
compactDisc.toString();
compactDisc.play();
}
}
结果:
专辑为hello
歌手为:adele
---磁道: 磁道1
---磁道: 磁道2
---磁道: 磁道3
2.xml注入javaConfig类
首先当xml配置比较复杂的时候,我们可以将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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- property帮助标签 util,首先要引入命名空间 -->
<!-- 它可以将list单独分开出来,其它bean可以复用 -->
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
<bean id="blankDisc"
class="learn.chapter2.BlankDisc"
p:title="hello"
p:artist="adele"
p:tracks-ref="trackList"></bean>
<import resource=""/>
</beans>现在我们假设要讲util:list 和 id="blankDisc" 拆分到不同的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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- property帮助标签 util,首先要引入命名空间 -->
<!-- 它可以将list单独分开出来,其它bean可以复用 -->
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
</beans>
总结:只要在增加import标签进行引入的就行
测试类型
package learn.chapter;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import learn.chapter2.BlankDisc;
import learn.chapter2.javaConfig.CDPlayerConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class XMLClearTest2 {
/**
*
*/
@Test
public void CompactDiscBeanSet(){
ApplicationContext ac = new ClassPathXmlApplicationContext("constructor.xml");
assertNotNull(ac.getBean("blankDisc"));
BlankDisc girl = (BlankDisc) ac.getBean("blankDisc");
System.out.println(girl);
}
}
结果:
BlankDisc [title=hello, artist=adele, tracks=[磁道1, 磁道2, 磁道3]]
引入javaConfig,其实就是将这个作为普通写入xml配置一个bean标签,例如:
<?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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean class="learn.chapter2.javaConfig.CDPlayerConfig1"></bean>
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
<bean id="blankDisc"
class="learn.chapter2.BlankDisc"
p:title="hello"
p:artist="adele"
p:tracks-ref="trackList"></bean>
<import resource=""/>
</beans>总结:xml注入javaconfig 用@importResource , javaConfig注入xml当做普通的bean注入,id可以省略
在典型的Spring应用中,我们可能会同时使用自动化和显式配置。即便你更喜欢通过JavaConfig实现显式配置,但有的时候XML却是最佳的方案。
来个珠帘合璧
1.在javaConfig中引用xml配置
2.在xml引用javaConfig配置
1.在javaConfig中引用xml配置
分开javaConfig配置1 CDPlayerConfig1
package learn.chapter2.javaConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import learn.chapter2.AdeleSong;
import learn.chapter2.AnanRyokoSong;
import learn.chapter2.CompactDisc;
import learn.chapter2.KeshaSong;
import learn.chapter2.LadyGagaSong;
@Configuration
@Import(CDPlayerConfig2.class)
public class CDPlayerConfig1 {
@Bean (name="adeleSong") //如果不写name默认就是方法名
public CompactDisc getAdeleSong(){
return new AdeleSong();
}
/**
* 每次注入的bean不是确定的
* @return
*/
@Bean
public CompactDisc randomBeanLesCD() {
int choice = (int) Math.floor(Math.random()*4);
if(choice == 0) {
return new AdeleSong();
}else if(choice ==1) {
return new KeshaSong();
}else if(choice == 2) {
return new LadyGagaSong();
} else {
return new AnanRyokoSong();
}
}
}
配置2 CDPlayerConfig2
package learn.chapter2.javaConfig; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import learn.chapter2.CompactDisc; import learn.chapter2.GirlSong; @Configuration public class CDPlayerConfig2 { /** * 这是最佳的方式,这不限制的本类的注入的bean 还可以是其他JavaConfig配置的bean * @param compactDisc * @return */ @Bean public GirlSong constructor(@Qualifier("randomBeanLesCD")CompactDisc compactDisc){ return new GirlSong(compactDisc); } }
总结:
1、在配置1中用import引入配置2
2、注意配置2引用的randomBeanLesCD来自配置1,说明最后它们是一个整体呈现
测试类:
package learn.chapter2.javaConfig; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import learn.chapter2.CompactDisc; import learn.chapter2.GirlSong; @Configuration public class CDPlayerConfig2 { /** * 这是最佳的方式,这不限制的本类的注入的bean 还可以是其他JavaConfig配置的bean * @param compactDisc * @return */ @Bean public GirlSong constructor(@Qualifier("randomBeanLesCD")CompactDisc compactDisc){ return new GirlSong(compactDisc); } }其实还可以用一个统计的配置的统一import
去掉配置1 的@import注解
package learn.chapter2.javaConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({CDPlayerConfig1.class, CDPlayerConfig2.class})
public class CDPlayerConfigAll {
}
如何引用xml配置的bean? 答案就是用@ImportResource("配置文件路径")
learn/chapter2/constructor.xml 获取blankDisc
<?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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- property帮助标签 util,首先要引入命名空间 -->
<!-- 它可以将list单独分开出来,其它bean可以复用 -->
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
<util:map>
<entry key="">
<value></value>
</entry>
</util:map>
<bean id="blankDisc"
class="learn.chapter2.BlankDisc"
p:title="hello"
p:artist="adele"
p:tracks-ref="trackList"></bean>
</beans>
package learn.chapter2.javaConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import learn.chapter2.AdeleSong; import learn.chapter2.AnanRyokoSong; import learn.chapter2.CompactDisc; import learn.chapter2.KeshaSong; import learn.chapter2.LadyGagaSong; @Configuration @Import(CDPlayerConfig2.class) @ImportResource("classpath:constructor.xml") public class CDPlayerConfig1 { @Bean (name="adeleSong") //如果不写name默认就是方法名 public CompactDisc getAdeleSong(){ return new AdeleSong(); } /** * 每次注入的bean不是确定的 * @return */ @Bean public CompactDisc randomBeanLesCD() { int choice = (int) Math.floor(Math.random()*4); if(choice == 0) { return new AdeleSong(); }else if(choice ==1) { return new KeshaSong(); }else if(choice == 2) { return new LadyGagaSong(); } else { return new AnanRyokoSong(); } } }
总结:在该类头部引用@ImportResource(“路径”)
package learn.chapter;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import learn.chapter2.CompactDisc;
import learn.chapter2.GirlSong;
import learn.chapter2.javaConfig.CDPlayerConfig1;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig1.class)
public class JavaConfigSeparateTest {
@Autowired
@Qualifier("constructor")
private GirlSong girlSong;
@Autowired
@Qualifier("blankDisc")
private CompactDisc compactDisc;
@Test
public void isNotNull(){
assertNotNull(girlSong);
}
@Test
public void getBlankDisc(){
compactDisc.toString();
compactDisc.play();
}
}
结果:
专辑为hello
歌手为:adele
---磁道: 磁道1
---磁道: 磁道2
---磁道: 磁道3
2.xml注入javaConfig类
首先当xml配置比较复杂的时候,我们可以将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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- property帮助标签 util,首先要引入命名空间 -->
<!-- 它可以将list单独分开出来,其它bean可以复用 -->
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
<bean id="blankDisc"
class="learn.chapter2.BlankDisc"
p:title="hello"
p:artist="adele"
p:tracks-ref="trackList"></bean>
<import resource=""/>
</beans>现在我们假设要讲util:list 和 id="blankDisc" 拆分到不同的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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- property帮助标签 util,首先要引入命名空间 -->
<!-- 它可以将list单独分开出来,其它bean可以复用 -->
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
</beans>
<?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:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <import resource="subconstructor.xml"/> <!-- property帮助标签 util,首先要引入命名空间 --> <!-- 它可以将list单独分开出来,其它bean可以复用 --> <bean id="blankDisc" class="learn.chapter2.BlankDisc" p:title="hello" p:artist="adele" p:tracks-ref="trackList"></bean> </beans>
总结:只要在增加import标签进行引入的就行
测试类型
package learn.chapter;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import learn.chapter2.BlankDisc;
import learn.chapter2.javaConfig.CDPlayerConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class XMLClearTest2 {
/**
*
*/
@Test
public void CompactDiscBeanSet(){
ApplicationContext ac = new ClassPathXmlApplicationContext("constructor.xml");
assertNotNull(ac.getBean("blankDisc"));
BlankDisc girl = (BlankDisc) ac.getBean("blankDisc");
System.out.println(girl);
}
}
结果:
BlankDisc [title=hello, artist=adele, tracks=[磁道1, 磁道2, 磁道3]]
引入javaConfig,其实就是将这个作为普通写入xml配置一个bean标签,例如:
<?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:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean class="learn.chapter2.javaConfig.CDPlayerConfig1"></bean>
<util:list id="trackList">
<value>磁道1</value>
<value>磁道2</value>
<value>磁道3</value>
</util:list >
<bean id="blankDisc"
class="learn.chapter2.BlankDisc"
p:title="hello"
p:artist="adele"
p:tracks-ref="trackList"></bean>
<import resource=""/>
</beans>总结:xml注入javaconfig 用@importResource , javaConfig注入xml当做普通的bean注入,id可以省略
相关文章推荐
- 装配Bean:XML与JavaConfig混合装配
- Spring装配bean的三种方法:自动化装配,java代码装配,XML装配及它们的混合使用
- spring笔记:第二章(通过javaconfig类装配bean)
- 第2章 装配Bean---java显示装配---笔记3
- 6、使用javaConfig代替xml文件来声明和导入bean
- 自动装配、JavaConfig、XML 三种方案之间,怎么导入和混合配置?
- 译--Spring中 bean定义profiles的使用 javaConfig 和XML配置
- 第2章 装配Bean---xml显示装配---笔记4
- spring对于bean的javaconfig,xml,自动装配三种装配方式实现
- 使用JSP + JAVABEAN + XML 开发的一个例子
- Spring学习笔记-IOC高级特性3-用Java Config简化XML配置
- Spring journey -- JavaConfig 装配 Bean
- 使用xml及java代码混合的方式来设置图形界面
- 使用JSP + JAVABEAN + XML 开发的一个例子
- java使用commons-betwixt 实现bean与xml互转
- java使用dom4j解析xml的笔记
- (笔记)Spring实战_装配Bean(3)_使用表达式装配
- java学习笔记——使用DOM解析XML和使用SAX解析XML
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 使用JSP + JAVABEAN + XML 开发的一个例子