Spring基础_在JavaConfig中引用xml配置<四>

来源:互联网 发布:linux 打开vim编辑器 编辑:程序博客网 时间:2024/06/05 15:05

代码

1.CompactDisc.java

package one;/** * 光盘接口 */public interface CompactDisc {void play();}
2.BlankDisc.java

package one;import java.util.List;/** * 光盘类 */public class BlankDisc implements CompactDisc {private String title;//唱片名private String artist;//艺术家private List<String> tracks;//轨道:一个唱片中应该有多首歌曲,位于不同的光盘轨道//构造器注入//在xml中通过constructor-arg对构造器参数进行注入public BlankDisc(String title,String artist,List<String> tracks) {this.title=title;this.artist=artist;this.tracks=tracks;}@Overridepublic void play() {System.out.println("唱片:"+title+"演唱:"+artist);for(String a:tracks){System.out.println("歌曲详细:"+a);}}}

3.MediaPlayer.java

package one;/** *多媒体播放器  */public interface MediaPlayer {public void play();}

4.CDPlayer.java

package one;/** * CD播放器 */public class CDPlayer implements MediaPlayer {private CompactDisc compactDisc;public CDPlayer(CompactDisc compactDisc) {this.compactDisc=compactDisc;}@Overridepublic void play() {compactDisc.play();}}
5.CDPlayerConfig.java

package one;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDPlayerConfig {@Beanpublic MediaPlayer cdPlayer(CompactDisc compactDisc){return new CDPlayer(compactDisc);}}
6.OneSystemConfig.java
package one;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.ImportResource;/** * 首先明确一点:对于自动配置,它从整个容器上下文中查找合适的bean,无论这个bean是来自JavaConfig还是XML配置。 * 创建一个更高级别的SoundSystemConfig,在这个类中使用@Import将两个配置类组合在一起: */@Configuration@Import({CDPlayerConfig.class})@ImportResource("classpath:cd-config.xml")public class OneSystemConfig {}

7.cd-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:c="http://www.springframework.org/schema/c"xmlns:p="http://www.springframework.org/schema/p"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"        xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd             http://www.springframework.org/schema/mvc             http://www.springframework.org/schema/mvc/spring-mvc.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context.xsd             http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop.xsd             http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx.xsd "><bean id="compactDisc" class="one.BlankDisc"c:_0="怒放的时节"c:_1="汪峰"><constructor-arg><list><value>飞的更高</value><value>飞的更快</value><value>飞的更强</value><value>飞的更冷</value></list></constructor-arg></bean></beans>
8.CDTest.java

package one;import static org.junit.Assert.*;import org.junit.*;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;/** *两个bean——配置在JavaConfig中的CDPlayer以及配置在XML中 *BlankDisc都会被加载到Spring容器之中。因为CDPlayerConfig中带 *有@Bean注解的方法接受一个CompactDisc作为参数,因 *此BlankDisc将会装配进来,此时与它是通过XML配置的没有任何关系。 */@RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration(classes=OneSystemConfig.class) public class CDTest {@Autowired  private CompactDisc cd;  @Autowired  private MediaPlayer player;        @Testpublic void cdshouldBeNull(){      assertNotNull(cd);  }  @Testpublic void play(){      player.play();  }}





原创粉丝点击