自动转配Bean
创建可被发现的Bean
代码结构:
程序清单:
package soundsystem;
public interface CompactDisc {
void play();
}
PS:@Component表明组件类
package soundsystem;
import org.springframework.stereotype.Component;
@Component
public class SgtPeppers implements CompactDisc{
private String title = "Sgt.Pepper's Lonely Hearts Club Bands";
private String artist = "The Beatles";
public void play() {
System.out.println("Playing "+title+" by "+artist);
}
}
PS:@ComponentScan注解启用了组件扫描,如果没有其他配置的话,@ComponentScan默认会扫描与配置类相同的包以及它的子包。这里是基于Java的配置启用组件扫描
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class CDPlayerConfig {
}
测试
PS:@Autowired注解表明自动装配
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;
import soundsystem.CDPlayerConfig;
import soundsystem.CompactDisc;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
通过XML启用组件扫描
代码结构
程序清单
PS:applicationContext.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"
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">
<!-- 启用组件扫描 -->
<context:component-scan base-package="soundsystem"/>
</beans>
PS:一样
package soundsystem;
public interface CompactDisc {
void play();
}
PS:一样
package soundsystem;
import org.springframework.stereotype.Component;
@Component
public class SgtPeppers implements CompactDisc{
private String title = "Sgt.Pepper's Lonely Hearts Club Bands";
private String artist = "The Beatles";
public void play() {
System.out.println("Playing "+title+" by "+artist);
}
}
测试
PS:变为
@ContextConfiguration(locations = { “classpath*:/applicationContext.xml”})
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/applicationContext.xml"})
public class CDPlayerTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
为组件扫描的bean命名
(1)默认bean的ID为sgtPeppers
@Component
public class SgtPeppers implements CompactDisc{
private String title = "Sgt.Pepper's Lonely Hearts Club Bands";
private String artist = "The Beatles";
public void play() {
System.out.println("Playing "+title+" by "+artist);
}
}
(2)bean的ID为Leo
@Component("Leo")
public class SgtPeppers implements CompactDisc{
private String title = "Sgt.Pepper's Lonely Hearts Club Bands";
private String artist = "The Beatles";
public void play() {
System.out.println("Playing "+title+" by "+artist);
}
}
(3)bean的ID为Leo
@Named("Leo")
public class SgtPeppers implements CompactDisc{
private String title = "Sgt.Pepper's Lonely Hearts Club Bands";
private String artist = "The Beatles";
public void play() {
System.out.println("Playing "+title+" by "+artist);
}
}
设置组件扫描的基础包
(1)扫描soundsystem
@Configuration
@ComponentScan("soundsystem")
public class CDPlayerConfig {
}
(2)扫描soundsystem,video
@Configuration
@ComponentScan(basePackages={"soundsystem","video"})
public class CDPlayerConfig {
}
(3)扫描CDPlayer类,DVDPlayer类所在的包
@Configuration
@ComponentScan(basePackageClasses = {CDPlayer.class,DVDPlayer.class})
public class CDPlayerConfig {
}
通过为bean添加注解实现自动装配
代码结构
程序清单
PS:任何方法都可以实现自动装配
package soundsystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CDPlayer implements MediaPlayer {
private CompactDisc cd;
@Autowired
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
@Autowired
public void setCompactDisc(CompactDisc cd) {
this.cd = cd;
}
@Autowired
public void insertDisc(CompactDisc cd) {
this.cd = cd;
}
public void play() {
cd.play();
}
}
PS:Java配置组件扫描
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class CDPlayerConfig {
}
package soundsystem;
public interface CompactDisc {
void play();
}
package soundsystem;
public interface MediaPlayer {
public void play();
}
package soundsystem;
import org.springframework.stereotype.Component;
/**
* Created by adc333 on 2017/2/3.
*/
@Component
public class SgtPeppers implements CompactDisc{
private String title = "hello";
private String artist = " world";
public void play() {
System.out.print(title+artist);
}
}
测试
PS:控制台输出比较,这里简单输出hello world
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 soundsystem.CDPlayerConfig;
import soundsystem.CompactDisc;
import soundsystem.MediaPlayer;
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 cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
@Test
public void play() {
player.play();
assertEquals("hello world",log.getLog());
}
}
通过Java代码装配bean
创建配置类
package soundsystem;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CDPlayerConfig {
}
声明简单的bean
(1)默认bean的ID为sgtPeppers
package soundsystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CDPlayerConfig {
@Bean
public CompactDisc sgtPeppers() {
return new SgtPeppers();
}
}
(2)bean的ID为Leo
package soundsystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CDPlayerConfig {
@Bean(name="Leo")
public CompactDisc sgtPeppers() {
return new SgtPeppers();
}
}
借助JavaConfig实现注入
ackage soundsystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CDPlayerConfig {
@Bean
public CompactDisc sgtPeppers() {
return new SgtPeppers();
}
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
测试
代码结构
程序清单
package soundsystem;
import org.springframework.beans.factory.annotation.Autowired;
public class CDPlayer implements MediaPlayer {
private CompactDisc cd;
@Autowired
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
@Autowired
public void setCompactDisc(CompactDisc cd) {
this.cd = cd;
}
@Autowired
public void insertDisc(CompactDisc cd) {
this.cd = cd;
}
public void play() {
cd.play();
}
}
package soundsystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CDPlayerConfig {
@Bean
public CompactDisc sgtPeppers() {
return new SgtPeppers();
}
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
package soundsystem;
public interface CompactDisc {
void play();
}
package soundsystem;
public interface MediaPlayer {
public void play();
}
package soundsystem;
public class SgtPeppers implements CompactDisc{
private String title = "hello";
private String artist = " world";
public void play() {
System.out.print(title+artist);
}
}
测试
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 soundsystem.CDPlayerConfig;
import soundsystem.CompactDisc;
import soundsystem.MediaPlayer;
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 cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
@Test
public void play() {
player.play();
assertEquals("hello world",log.getLog());
}
}
通过XML装配bean
声明一个简单的bean
(1)bean的ID为soundsystem.SgtPeppers#0,如果声明了另外一个SgtPeppers,同样没有明确标识,bean的ID为soundsystem.SgtPeppers#1
<bean class="soundsystem.SgtPeppers"/>
(2)bean的ID为compactDisc
<bean id="compactDisc" class="soundsystem.SgtPeppers"/>
借助构造器注入初始化bean
构造器注入bean引用
(1)
<bean id="cdPlayer" class="soundsystem.CDPlayer">
<constructor-arg ref="compactDisc"/>
</bean>
(2)
<bean id="cdPlayer" class="soundsystem.CDPlayer" c:cd-ref="compactDisc" />
将字面量注入到构造器中
package soundsystem;
public class BlankDisc implements CompactDisc {
private String title;
private String artist;
public BlankDisc(String title, String artist) {
this.title = title;
this.artist = artist;
}
public void play() {
System.out.println("Playing "+title+" by "+artist);
}
}
(1)
<bean id="compactDisc" class="soundsystem.BlankDisc">
<constructor-arg value="hello"/>
<constructor-arg value="world"/>
</bean>
(2)
<bean id="compactDisc" class="soundsystem.BlankDisc"
c:_title="hello"
c:_artist="world" />
装配集合
package soundsystem;
import java.util.List;
/**
* Created by adc333 on 2017/2/3.
*/
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);
}
}
}
<bean id="compactDisc" class="soundsystem.BlankDisc">
<constructor-arg value="hello"/>
<constructor-arg value="world"/>
<constructor-arg>
<list>
<value>11111</value>
<value>22222</value>
<value>33333</value>
<value>44444</value>
<value>55555</value>
<list>
</constructor-arg>
</bean>
设置属性
package soundsystem;
import org.springframework.beans.factory.annotation.Autowired;
public class CDPlayer implements MediaPlayer {
private CompactDisc cd;
@Autowired
public void setCompactDisc(CompactDisc cd) {
this.cd = cd;
}
public void play() {
cd.play();
}
}
(1)
<bean id="cdPlayer" class="soundsystem.CDPlayer">
<property name="compactDisc" ref="compactDisc"/>
</bean>
(2)
<bean id="cdPlayer" class="soundsystem.CDPlayer" p:compactDisc-ref="compactDisc" />
将字面量注入到属性中
package soundsystem;
import java.util.List;
public class BlankDisc implements CompactDisc {
private String title;
private String artist;
private List<String> tracks;
public void setTitle(String title) {
this.title = title;
}
public void setArtist(String artist) {
this.artist = artist;
}
public void setTracks(List<String> tracks) {
this.tracks = tracks;
}
public void play() {
System.out.println("Playing "+title+" by "+artist);
for(String track : tracks) {
System.out.println("-Track: "+track);
}
}
}
(1)
<bean id="compactDisc" class="soundsystem.BlankDisc">
<property name="title" value="hello"/>
<property name="artist" value="world"/>
<property name="tracks">
<list>
<value>11111</value>
<value>22222</value>
<value>33333</value>
<value>44444</value>
<value>55555</value>
<list>
</property>
</bean>
(2)
<bean id="compactDisc" class="soundsystem.BlankDisc" p:title="hello" p:artist="world">
<property name="tracks">
<list>
<value>11111</value>
<value>22222</value>
<value>33333</value>
<value>44444</value>
<value>55555</value>
<list>
</property>
</bean>
测试
代码结构
程序清单
package soundsystem;
import java.util.List;
public class BlankDisc implements CompactDisc {
private String title;
private String artist;
private List<String> tracks;
public void setTitle(String title) {
this.title = title;
}
public void setArtist(String artist) {
this.artist = artist;
}
public void setTracks(List<String> tracks) {
this.tracks = tracks;
}
public void play() {
System.out.print(title+artist);
for(String track : tracks) {
System.out.print(track);
}
}
}
package soundsystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class CDPlayer implements MediaPlayer {
private CompactDisc cd;
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
public void play() {
cd.play();
}
}
package soundsystem;
public interface CompactDisc {
void play();
}
package soundsystem;
public interface MediaPlayer {
public void play();
}
applicationContext.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"
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">
<!-- 设置属性注入bean -->
<bean id="compactDisc" class="soundsystem.BlankDisc">
<property name="title" value="hello"/>
<property name="artist" value="world"/>
<property name="tracks">
<list>
<value>11111</value>
<value>22222</value>
<value>33333</value>
<value>44444</value>
<value>55555</value>
</list>
</property>
</bean>
<!-- 构造器注入bean -->
<bean id="cdPlayer" class="soundsystem.CDPlayer">
<constructor-arg ref="compactDisc"/>
</bean>
</beans>
测试
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.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import soundsystem.*;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/applicationContext.xml"})
public class CDPlayerTest {
@Autowired
private ApplicationContext context;
@Rule
public final StandardOutputStreamLog log = new StandardOutputStreamLog();
@Test
public void play() {
CompactDisc compactDisc = (CompactDisc)context.getBean(BlankDisc.class);
MediaPlayer cdPlayer = (MediaPlayer)context.getBean(CDPlayer.class);
cdPlayer.play();
assertEquals("helloworld1111122222333334444455555",log.getLog());
}
}