Spring从两个角度来实现自动化装配:java
为了更形象的解释组件扫描与自动装配,咱们举一个音响系统的例子,主要包含如下内容:git
关于CD和CD播放器关系的解释:github
若是你不将CD插入(注入)到CD播放器中,那么CD播放器实际上是没有太大用处的。因此,能够这样说,spring
CD播放器依赖于CD才能完成它的使命。安全
先建立CD接口CompactDisc:框架
package soundsystem; public interface CompactDisc { void play(); }
而后建立CD接口的一个实现类SgtPeppers:ide
package soundsystem; import org.springframework.stereotype.Component; @Component public class SgtPeppers implements CompactDisc { @Override public void play() { String title = "Sgt.Pepper's Lonely Hearts Club Band"; String artists = "The Beatles"; System.out.println("Playing " + title + " By " + artists); } }
SgtPeppers类与以往类的区别在于使用了@Component注解。这个注解代表该类会做为组件类,并告知Spring要为这个类建立bean。测试
建立了bean,那么如何让Spring发现它呢?这时就须要用到组件扫描,不过,在Spring中,组件扫描默认是不启用的。所以咱们须要显式配置一下Spring,从而命令它去寻找带有@Component注解的类,并为其建立bean。ui
建立CDPlayerConfig类:this
package soundsystem; import org.springframework.context.annotation.ComponentScan; @ComponentScan public class CDPlayerConfig { }
这个类与以往类的区别是使用了@ComponentScan注解,这个注解可以Spring中启用组件扫描。
@ComponentScan默认会扫描与配置类相同的包以及这个包下的全部子包,查找带有@Component注解的类。
为了验证建立的bean可否被Spring发现,咱们建立一个简单的JUnit测试,完成此测试须要导入如下两个jar包:
导入jar包的方式以下:
导入完成后的项目结构图以下所示:
package soundsystem; import static org.junit.Assert.*; 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; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CDPlayerConfig.class) public class CDPlayerTest { @Autowired private CompactDisc compactDisc; @Test public void cdShouldNotBeNull() { assertNotNull(compactDisc); compactDisc.play(); } }
代码简单讲解:
@RunWith(SpringJUnit4ClassRunner.class),会在测试开始的时候自动建立Spring的应用上下文。
@ContextConfiguration(classes = CDPlayerConfig.class)会告诉Spring须要在CDPlayerConfig中加载配置。
字段compactDisc上的@Autowired注解,会将CompactDisc bean(实际上是SgtPeppers)注入到测试代码之中。
运行测试方法cdShouldNotBeNull,会发现测试经过,compactDisc不为null:
Spring应用上下文中全部的bean都会给定一个ID,默认状况下,Spring会将类名的第一个字母变为小写,做为该bean的ID。
如上面代码中SgtPeppers bean的ID为sgtPeppers。
有如下两种方式来设置bean ID:
@Component("lonelyHeartsClub") public class SgtPeppers implements CompactDisc { ... }
@Named注解不是Spring框架的注解,而是Java 依赖注入规范(Java Dependency Injection)中的注解,所以须要导入jar包:javax.inject-1.jar。
package soundsystem; import javax.inject.Named; @Named("lonelyHeartsClub") public class SgtPeppers implements CompactDisc { ... }
在Spring项目中建议使用@Component注解。
按照默认规则 ,@ComponentScan注解会以配置类所在的包做为基础包(base package)来扫描组件。
但有时候,咱们会将配置类放在单独的包中,使其与其余的应用代码区分开来。
这种场景下,默认的基础包就知足不了需求。
@ComponentScan注解支持传入指定的基础包,有如下几种场景:
@ComponentScan("soundsystem") public class CDPlayerConfig { }
或者:
@ComponentScan(basePackages = "soundsystem") public class CDPlayerConfig { }
@ComponentScan(basePackages = {"soundsystem", "video"}) public class CDPlayerConfig { }
@ComponentScan(basePackageClasses = {CDPlayer.class}) public class CDPlayerConfig { }
如上所示,basePackageClasses也支持指定多个类,指定类所在的包将会做为组件扫描的基础包。
建议使用这种类型安全方式来指定扫描的基础包。
自动装配是让Spring自动知足bean 依赖的一种方法,在知足依赖的过程当中,会在Spring应用上下文中寻找匹配某个bean须要的其余bean。
实现自动装配,须要使用Spring的@Autowired注解。
@Autowired通常状况下,有如下3种使用方式:
package soundsystem; public interface MediaPlayer { void play(); }
package soundsystem; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CDPlayer implements MediaPlayer { private CompactDisc compactDisc; @Autowired public CDPlayer(CompactDisc compactDisc) { this.compactDisc = compactDisc; } @Override public void play() { compactDisc.play(); } }
package soundsystem; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CDPlayer implements MediaPlayer { private CompactDisc compactDisc; @Autowired public void setCompactDisc(CompactDisc compactDisc) { this.compactDisc = compactDisc; } @Override public void play() { compactDisc.play(); } }
package soundsystem; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CDPlayer implements MediaPlayer { private CompactDisc compactDisc; @Autowired public void insertDisc(CompactDisc compactDisc) { this.compactDisc = compactDisc; } @Override public void play() { compactDisc.play(); } }
无论是构造器、Setter方法仍是其余的方法,Spring都会尝试知足方法参数上所声明的依赖。
假若有且只有一个bean匹配依赖需求的话,那么这个bean将会被装配进来。
若是没有匹配的bean,那么在应用上下文建立的时候,Spring会抛出一个异常。
能够经过设置require属性为false避免该异常出现:
@Autowired(required = false) public CDPlayer(CompactDisc compactDisc) { this.compactDisc = compactDisc; }
不过建议谨慎使用,避免未找到bean进行匹配,并且代码没有进行null检查而出现NullPointerException。
若是有多个bean都能知足依赖关系的话,Spring将会抛出一个异常,代表没有明确指定要选择哪一个bean进行自动装配。
@Autowired注解也能够替换成@Inject注解(来源于Java依赖注入规范),一样能够实现自动装配:
package soundsystem; import org.springframework.stereotype.Component; import javax.inject.Inject; @Component public class CDPlayer implements MediaPlayer { private CompactDisc compactDisc; @Inject public CDPlayer(CompactDisc compactDisc) { this.compactDisc = compactDisc; } ... }
在Spring项目中建议使用@Autowired注解。
修改CDPlayerTest类代码测试自动装配
package soundsystem; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.StandardOutputStreamLog; 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; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CDPlayerConfig.class) public class CDPlayerTest { @Rule public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired private MediaPlayer player; @Autowired private CompactDisc compactDisc; @Test public void cdShouldNotBeNull() { assertNotNull(compactDisc); compactDisc.play(); } @Test public void play() { player.play(); assertEquals("Playing Sgt.Pepper's Lonely Hearts Club Band By The Beatles\r\n", log.getLog()); } }
由于代码中使用了StandardOutputStreamLog类,所以须要导入jar包:system-rules-1.16.0.jar
运行测试方法play(),输出内容和预期一致,说明字段player已经被MediaPlayer的实现类CDPlayer bean装配,测试经过,以下所示:
源码地址:https://github.com/zwwhnly/spring-action.git,欢迎下载。