1. 程式人生 > >第2章 裝配Bean---xml和javaConfig混合使用裝配---筆記5

第2章 裝配Bean---xml和javaConfig混合使用裝配---筆記5

概述:

在典型的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可以省略