本篇内容
- 1、通过java配置代码方式获取bean对象
- 2、通过java配置代码引用方式获取对象
- 3、验证通过Spring中@Bean注解返回的对象是单例
- 4、采用构造器方式注入Bean
- 5、采用Setter()方式注入Bean
一、基础配置
1、创建接口
public interface CompactDisc {
void play();
}
2、创建实现类
@Component
public class SgtPepper implements CompactDisc {
public void play() {
System.out.println(" sgt-pepper play ... ... ");
}
}
public class CDPlayer {
private CompactDisc compactDisc;
public CDPlayer(CompactDisc compactDisc) {
this.compactDisc = compactDisc;
}
public void play() {
compactDisc.play();
}
public void setCompactDisc(CompactDisc compactDisc) {
this.compactDisc = compactDisc;
}
public CompactDisc getCompactDisc() {
return compactDisc;
}
}
3、创建配置文件
@Configuration
public class CDConfig {
}
- 注解说明:
- @Configuration:表示当前类是一个配置类此类应该包含在Spring应用上下文中如何创建Bean细节
- 注意:采用显示配置方式所以不用@ComponentScan注解
二、在配置文件中添加获取Bean的方法
1、配置类修改
@Configuration
public class CDConfig {
@Bean
public CompactDisc sgtPepper() {
return new SgtPepper();
}
}
2、测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDConfig.class)
public class CDPlayerTest {
@Autowired
private CDConfig cdConfig;
@Test
public void getBean() {
CompactDisc compactDisc = cdConfig.sgtPepper();
compactDisc.play();
}
}
3、修改bean的ID值
- 默认情况下,bean的ID与带有@Bean注解的方法名是一样的
@Configuration
public class PlayConfig {
@Bean(name = "longSgtPepper")
public CompactDisc sgtPepper() {
return new SgtPepper();
}
}
注解说明:
- 1、@Bean注解告诉Spring此方法将会返回一个对象,此对象要注册为Spring应用上下文中的ben。
- 2、name = “longSgtPepper”:将返回的bean对象声明为一个自定义的名称
三、引用创建Bean
1、修改配置文件
@Configuration
public class CDConfig {
@Bean
public CompactDisc sgtPepper() {
return new SgtPepper();
}
@Bean
public CDPlayer cdPlayer() {
return new CDPlayer(sgtPepper());
}
}
2、测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDConfig.class)
public class CDPlayerTest {
@Autowired
private CDConfig cdConfig;
@Test
public void getCDPlayer(){
CDPlayer cdPlayer = cdConfig.cdPlayer();
cdPlayer.play();
}
}
说明:
- 1、new CDPlayer(sgtPeppers()):此方法中返回的Bean对象会与sgtPeppers()中的对象一样
- 2、因为sgtPeppers()添加了bean注解Spring会拦截所有对此方法的调用,并确保直接返回此方法所创建的bean,不是每次都对此方法进行实际调用
四、验证通过Spring中@Bean注解返回的对象是单例
1、配置类修改
@Configuration
public class CDConfig {
@Bean
public CompactDisc sgtPepper() {
return new SgtPepper();
}
@Bean
public CDPlayer cdPlayer() {
return new CDPlayer(sgtPepper());
}
@Bean
public CDPlayer anotherPlayer() {
return new CDPlayer(sgtPepper());
}
}
- Spring中的bean都是单例的,所以在每次调用bean注解的方法是都会得到相同的bean
2、测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDConfig.class)
public class CDPlayerTest {
@Autowired
private CDConfig cdConfig;
@Test
public void getBean() {
CompactDisc compactDisc = cdConfig.sgtPepper();
compactDisc.play();
}
@Test
public void equalsBean() {
CompactDisc compactDisc = cdConfig.sgtPepper();
CDPlayer cdPlayer = cdConfig.anotherPlayer();
CompactDisc compactDisc1 = cdPlayer.getCompactDisc();
if (compactDisc.equals(compactDisc1)) {
System.out.println("对象相等");
}
}
}
五、采用构造器方式注入bean
1、配置类
@Configuration
public class CDConfig {
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
说明:
- 调用new CDPlayer();时会自动装配一个CompactDisc对象,方法体会用一个合适的方法类使用bean对象
六、采用setter方式
1、配置类
@Configuration
public class CDConfig {
@Bean
public CDPlayer anotherPlayer(CompactDisc compactDisc) {
CDPlayer cdPlayer = new CDPlayer(compactDisc);
cdPlayer.setCompactDisc(compactDisc);
return cdPlayer;
}
}
2、测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDConfig.class)
public class CDPlayerTest {
@Autowired
private CDConfig cdConfig;
@Test
public void equalsCDPlayer() {
CDPlayer cdPlayer = cdConfig.cdPlayer(cdConfig.sgtPepper());
CompactDisc compactDisc = cdPlayer.getCompactDisc();
cdPlayer.play();
CDPlayer cdPlayer1 = cdConfig.cdPlayer();
CompactDisc compactDisc1 = cdPlayer1.getCompactDisc();
cdPlayer1.play();
if (compactDisc.equals(compactDisc1)) {
System.out.println("对象相等");
}
}
}
代码地址:
https://github.com/brusion/brusion-code/tree/master/demo-java/03%20-%20demo%20-%20spring/spring-demo-study/01-di