码迷,mamicode.com
首页 > 其他好文 > 详细

笔记3 装配Bean总结

时间:2018-04-17 22:22:23      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:java   red   函数的参数   rand   方法   turn   笔记   inter   wired   

一、自动化装配bean

1.组件扫描

2.自动装配

CompactDisc.java

1 package Autowiring;
2 
3 public interface CompactDisc {
4     void play();
5 
6 }

SgtPeppers.java

SgtPeppers类上使用了@Component注解。

这个简单的注解表明该类会作为组件类,并告知Spring要为这个类创建bean。没有必要显式配置SgtPeppersbean,因为这个类使用了@Component注解,所以Spring会为你把事情处理妥当。

 1 package Autowiring;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component
 6 public class SgtPeppers implements CompactDisc {
 7     private String title = "Sgt. Pepper‘s Lonely Hearts Club Band";
 8     private String artist = "The Beatles";
 9 
10     @Override
11     public void play() {
12         // TODO Auto-generated method stub
13         System.out.println("Playing " + title + " by " + artist);
14     }
15 
16 }

不过,组件扫描默认是不启用的。我们还需要显式配置一下Spring,从而命令它去寻找带有@Component注解的类,并为其创建bean。

 

 1 package Autowiring;
 2 
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.Configuration;
 5 
 6 @Configuration
 7 @ComponentScan
 8 public class CDPlayerConfig {
 9 
10 }

 

通过Java代码定义了Spring的装配规则@ComponentScan默认会扫描与配置类相同的包。

因为CDPlayerConfig类位于Autowiring包中,因此Spring将会扫描这个包以及这个包下的所有子包,查找带有@Component注解的类。

这样的话,就能发现CompactDisc的实现类,并且会在Spring中自动为其创建一个bean。

测试

CDPlayerTest.java

 1 package Autowiring;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.test.context.ContextConfiguration;
 7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 8 
 9 @RunWith(SpringJUnit4ClassRunner.class)
10 @ContextConfiguration(classes = Autowiring.CDPlayerConfig.class)
11 public class CDPlayerTest {
12     @Autowired
13     private CompactDisc cd;
14 
15     @Test
16     public void test() {
17         cd.play();
18     }
19 }

CDPlayerTest使用了Spring的SpringJUnit4ClassRunner,以便在测试开始的时候自动创建Spring的应用上下文。

注解@ContextConfiguration会告诉它需要在CDPlayerConfig中加载配置。因为CDPlayerConfig类中包含了@ComponentScan,因此最终的应用上下文中应该包含CompactDisc这个bean。在测试代码中有一个CompactDisc类型的属性,并且这个属性带有@Autowired注解,以便于将CompactDisc这个bean注入到测试代码之中

二、通过Java代码装配Bean

CDPlayerConfig.java

 

 1 package Javawiring;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 
 6 @Configuration
 7 public class CDPlayerConfig {
 8 
 9     @Bean
10     public CompactDisc randomBeatlesCD() {
11         int choice = (int) Math.floor(Math.random() * 4);
12         if (choice == 0) {
13             return new SgtPeppers();
14         } else if (choice == 1) {
15             return new WhiteAlbum();
16         } else if (choice == 2) {
17             return new HardDaysNight();
18         } else {
19             return new Revolver();
20         }
21     }
22 
23 
24     @Bean
25     public CDPlayer cdPlayer(CompactDisc randomBeatlesCD) {
26         return new CDPlayer(randomBeatlesCD);
27     }
28 }

 

@Configuration注解表明这个类是一个配置类 , 移除了@ComponentScan注解,此时的CDPlayerConfig类就没有任何作用了。

声明简单的bean

 

1     @Bean
2     public CDPlayer cdPlayer(CompactDisc randomBeatlesCD) {
3         return new CDPlayer(randomBeatlesCD);
4     }

 

CDPlayer实现了一个接口,构造函数的参数是实现了CompactDisc接口的类,然后执行其相应的方法。

 

 1     @Bean
 2     public CompactDisc randomBeatlesCD() {
 3         int choice = (int) Math.floor(Math.random() * 4);
 4         if (choice == 0) {
 5             return new SgtPeppers();
 6         } else if (choice == 1) {
 7             return new WhiteAlbum();
 8         } else if (choice == 2) {
 9             return new HardDaysNight();
10         } else {
11             return new Revolver();
12         }
13     }

三、通过XML装配bean

声明一个bean

1 <bean id="sgtPepperss" class="XMLwiring.SgtPepperss" ></bean>

测试的时候修改@ContextConfiguration("classpath:SoundSystemXMLwiring.xml"),然后定义相应的对象进行测试。

四、混合装配

两个Java配置文件,在一个总的配置文件中使用@Import({ CDConfig.class, CDPlayerConfig.class })进行结合。

1.在JavaConfig中引用XML配置  <@ImportResource注解>

@ImportResource("classpath:SoundSystemJavaXMLwiring.xml")

2.在XML配置中引用JavaConfig

将Java配置文件当作bean在XML文件中进行声明即可。

 

笔记3 装配Bean总结

标签:java   red   函数的参数   rand   方法   turn   笔记   inter   wired   

原文地址:https://www.cnblogs.com/lyj-gyq/p/8870048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!