《Spring实战》学习笔记(一)装配Bean
来源:互联网 发布:图文编辑排版软件 编辑:程序博客网 时间:2024/05/17 10:26
自动转配Bean
创建可被发现的Bean
代码结构:
程序清单:
package soundsystem;public interface CompactDisc { void play();}
PS:@Component表明组件类
package soundsystem;import org.springframework.stereotype.Component;@Componentpublic 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@ComponentScanpublic 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;@Componentpublic 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
@Componentpublic 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;@Componentpublic 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@ComponentScanpublic 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. */@Componentpublic 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;@Configurationpublic class CDPlayerConfig {}
声明简单的bean
(1)默认bean的ID为sgtPeppers
package soundsystem;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic 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;@Configurationpublic 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;@Configurationpublic 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;@Configurationpublic 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()); }}
1 0
- 《Spring实战》学习笔记(一)装配Bean
- [学习笔记]spring实战-装配Bean
- Spring学习笔记一:装配Bean
- Spring 学习(一)-----装配Bean
- 《Spring实战》学习笔记-第二章:装配Bean
- 《Spring实战》学习笔记-第二章:装配Bean
- Spring实战(装配Bean)
- spring实战-声明,装配Bean(一)
- Spring实战笔记2---Bean的装配
- Spring实战笔记——装配Bean
- Spring实战-装配bean
- Spring(一)装配Bean
- Spring学习笔记(9.Spring Bean装配之Resource)
- Spring学习笔记(11.Spring Bean装配注释)
- Spring学习笔记-C2-Spring装配Bean
- (笔记)Spring实战_装配Bean(3)_使用表达式装配
- Spring实战--(二)装配Bean
- Spring 学习笔记(二)——装配 bean
- Strut2里拦截器不能加载的问题
- 设计模式(Java)-装饰者模式
- Lambda 表达式回顾
- enum与static final map
- 浏览器访问网页的详细内部过程
- 《Spring实战》学习笔记(一)装配Bean
- 5-二维数组_练习_字符串处理函数_指针
- Codeforces 764C Timofey and a tree(思路)
- IOS10 Notification推送通知(一)
- 6-字符指针_删除字符串中的空格_register_指针数组
- 进程和线程的区别
- 7-函数指针_字符串转整数练习
- Deep Q-Network,NIPS-2013:Playing Atari with Deep Reinforcement Learning
- 8-数据类型_处理5个学生的数据