首页 > 学院 > 开发设计 > 正文

《Spring实战》学习笔记(二)JavaConfig @Import 和 @ImportResource注解的使用

2019-11-14 11:14:15
字体:
来源:转载
供稿:网友

最近在看《sPRing in Action 4th》,讲到javaConfig的@Import和@ImportResource的使用,于是照着例子做了个小demo,加深自己的印象。在Spring中配置有xml和JavaConfig的配置方式,相比来说,使用JavaConfig的方式配置会更利于管理,类型安全。

demo项目结构(Maven约定):

├── pom.xml└── src ├── main │ ├── java │ │ └── com │ │ └── jiaobuchong │ │ ├── config │ │ │ ├── CDConfig.java │ │ │ ├── CDPlayerConfig.java │ │ │ └── SoundSystemConfig.java │ │ ├── dao │ │ │ └── CompactDisc.java │ │ └── soundsystem │ │ ├── BlankDisc.java │ │ ├── CDPlayer.java │ │ └── SgtPeppers.java │ └── resources │ └── cons-injec.xml └── test └── java └── com └── jiaobuchong ├── soundsystem ├── CDPlayerTest.java ├── CDPlayerTest1.java └── CDPlayerTest2.java12345678910111213141516171819202122232425261234567891011121314151617181920212223242526

config包下的三个配置类

CDConfig.java:
package com.jiaobuchong.config;import com.jiaobuchong.dao.CompactDisc;import com.jiaobuchong.soundsystem.SgtPeppers;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDConfig { @Bean // 将SgtPeppers注册为 SpringContext中的bean public CompactDisc compactDisc() { return new SgtPeppers(); // CompactDisc类型的 }}12345678910111213141234567891011121314
CDPlayerConfig.java:
package com.jiaobuchong.config;import com.jiaobuchong.dao.CompactDisc;import com.jiaobuchong.soundsystem.CDPlayer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;@Configuration@Import(CDConfig.class) //导入CDConfig的配置public class CDPlayerConfig { @Bean(name = "cDPlayer") public CDPlayer cdPlayer(CompactDisc compactDisc) { /*这里会注入CompactDisc类型的bean 这里注入的这个bean是CDConfig.class中的CompactDisc类型的那个bean*/ return new CDPlayer(compactDisc); }}12345678910111213141516171819201234567891011121314151617181920
SoundSystemConfig.java:
package com.jiaobuchong.config;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.ImportResource;@Configuration@Import(CDPlayerConfig.class) @ImportResource("classpath:cons-injec.xml") //导入xml配置项public class SoundSystemConfig {}1234567891011121312345678910111213@Configuration注解表示定义一个配置类,这里使用注解@Bean就好比如xml配置时的<bean>元素,如:<bean id="cdPlayer" class="com.jiaobuchong.soundsystem.CDPlayer"> <property name="cd" ref="compactDisc" /></bean>123123@ImportResource类似于xml配置时的:<import resource="cons-injecxml" />11

dao包下的一个接口:

CompactDisc.java:
package com.jiaobuchong.dao;public interface CompactDisc { void play();}123456123456

soundsystem包下的类:

BlankDisc.java:
package com.jiaobuchong.soundsystem;import com.jiaobuchong.dao.CompactDisc;import java.util.List;public class BlankDisc implements CompactDisc { private String title; private String artist; private List<String> tracks; public BlankDisc(String title, String artist, List<String> tracks) { this.title = title; this.artist = artist; this.tracks = tracks; } public void play() { System.out.println("Playing " + title + " by " + artist); for (String track : tracks) { System.out.println("-Track: " + track); } }}123456789101112131415161718192021222324123456789101112131415161718192021222324
CDPlayer.java:
package com.jiaobuchong.soundsystem;import com.jiaobuchong.dao.CompactDisc;import org.springframework.beans.factory.annotation.Autowired;public class CDPlayer implements CompactDisc { private CompactDisc cd; @Autowired //构造函数注入 public CDPlayer(CompactDisc cd) { this.cd = cd; } public void play() { cd.play(); }}123456789101112131415161718123456789101112131415161718
SgtPeppers.java:
package com.jiaobuchong.soundsystem;import com.jiaobuchong.dao.CompactDisc;import org.springframework.stereotype.Component;public class SgtPeppers implements CompactDisc { private String title = "Sgt. Pepper's Lonely Hearts Club Band"; private String artist = "The Beatles"; public void play() { System.out.println("Playing " + title + " by " + artist); }}123456789101112123456789101112

xml配置文件

cons-injec.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:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="compactDisc" class="com.jiaobuchong.soundsystem.BlankDisc" c:_0="Sgt. Pepper's Lonely Hearts Club Band" c:_1="The Beatles"> <constructor-arg> <list> <value>Sgt. Pepper's Lonely Hearts Club Band</value> <value>With a Little Help from My Friends</value> <value>Lucy in the Sky with Diamonds</value> <value>Getting Better</value> <value>Fixing a Hole</value> <!-- ...other tracks omitted for brevity... --> </list> </constructor-arg> </bean></beans>12345678910111213141516171819202122231234567891011121314151617181920212223

测试类

CDPlayerTest.java:
package com.jiaobuchong.soundsystem;import com.jiaobuchong.config.CDConfig;import com.jiaobuchong.config.CDPlayerConfig;import com.jiaobuchong.dao.CompactDisc;import org.junit.Test;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;/*CDPlayerTest takes advantage of Spring’s SpringJUnit4ClassRunner to have a Spring applicationcontext automatically created when the test starts. And the @Context-Configuration annotationtells it to load its configuration from the CDPlayerConfig class. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = CDConfig.class)public class CDPlayerTest { @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() {// assertNotNull(cd); cd.play(); }}12345678910111213141516171819202122232425262728291234567891011121314151617181920212223242526272829
CDPlayerTest1.java:
package com.jiaobuchong.soundsystem;import com.jiaobuchong.config.CDConfig;import com.jiaobuchong.config.CDPlayerConfig;import com.jiaobuchong.dao.CompactDisc;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;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = CDPlayerConfig.class)public class CDPlayerTest1 { @Autowired @Qualifier("cDPlayer") private CompactDisc cd; @Test public void cdShouldNotBeNull() {// assertNotNull(cd); cd.play(); }}123456789101112131415161718192021222324252627123456789101112131415161718192021222324252627
CDPlayerTest2.java:
package com.jiaobuchong.soundsystem;import com.jiaobuchong.config.CDPlayerConfig;import com.jiaobuchong.config.SoundSystemConfig;import com.jiaobuchong.dao.CompactDisc;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;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = SoundSystemConfig.class)public class CDPlayerTest2 { @Autowired @Qualifier("cDPlayer") private CompactDisc cd; @Autowired @Qualifier("compactDisc") private CompactDisc cd1; @Test public void cdShouldNotBeNull() {// assertNotNull(cd); cd.play(); } @Test public void testBlankDisc() { cd1.play(); }}12345678910111213141516171819202122232425262728293031323334351234567891011121314151617181920212223242526272829303132333435

看完这个demo代码,基本上@Import和ImportResource的意义应该也就明白了,更多细节请参考《Spring in Action 4th》。


上一篇:sso(单点登录一)

下一篇:The Game

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表