zoukankan      html  css  js  c++  java
  • 笔记3 装配Bean总结

    一、自动化装配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文件中进行声明即可。

  • 相关阅读:
    shell(7):四则运算
    linux命令(46):程序运行前后台切换
    ASP.NET WEB API必知必会:特性路由
    分享在winform下实现左右布局多窗口界面-续篇
    简述.NET事务应用原则
    分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续3篇-导出时动态生成多Sheet EXCEL)
    Winform应用程序实现通用消息窗口
    分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续2篇-模板导出综合示例)
    关于WEB Service&WCF&WebApi实现身份验证之WebApi篇
    分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续篇)
  • 原文地址:https://www.cnblogs.com/lyj-gyq/p/8870048.html
Copyright © 2011-2022 走看看