您的位置:首页 > 编程语言 > Java开发

第2章 装配Bean---xml和javaConfig混合使用装配---笔记5

2017-06-20 15:42 483 查看
概述:

在典型的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可以省略
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: