您的位置:首页 > 编程语言 > Java开发

Spring Bean之JavaConfig自动化装配bean

2016-11-13 21:51 453 查看
一、简介:

Spring能够进行自动化的装配,它使用两种方式来进行自动化装配:

  1、组件扫描:

  Spring会自动发现应用上下文中所创建的bean

  2、自动装配:

  Spring会自动满足bean之间的依赖关系

比较有趣的是,Spring的自动化装配使用了零xml配置,而是使用全代码配置,其中代码的配置文件使用@Configuration注解进行标注。

二、组件扫描:

@Component能给一个类自动生成对象并注入到Spring容器中,比如下面的CDPlayer,会自动new一个CDPlayer的对象并放置到Spring容器中。

我们知道,Spring容器中每个bean都有自己唯一的一个id,那么自动注入的bean的id为多少呢?经测试,其规则如下:

1、如果@Component中有值,类似

@Component("xxx")

那么此bean的id即为xxx

2、如果类名为第一个字母大写,第二个字母小写,即满足首字母大写的驼峰命令规则,比如CdPlayer,。

那么其bean的id第一个字母要小写,其余不变,所以最终为cdplayer

3、如果不满足规则1,比如CDPlayer

那么其bean的id跟类名相同,所以最终为CDPlayer

1、

  Player:

  public interface Player {

      void play();

  }

  @Component

  public class CDPlayer implements Player {

      public void play() {

          System.out.print("播放CD");

      }

  }

  2、配置对象

  需要注意的是,配置对象需要 @Configuration和@ComponentScan注解,而@ComponentScan注解是标注Spring扫描的基础包名的,如果没有值,默认会扫描自己所在的包的所有类。

  其中的Configuration配置对象:

  @Configuration

  @ComponentScan

  public class PlayerConfig {

  }

  那么如何配置扫描包呢?比如我想扫描com.happyheng包下的所有类,可以这样:

  @ComponentScan(basePackages = "com.happyheng")

 3、使用:

   我们使用junit4来进行测试,注意在想获取Spring中bean上加上@Autowired,Spring会自动注入。

  另外,下面的Player可以换成CDPlayer,因为CDPlayer的对象既是CDPlayer的对象,也是Player的对象。

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = PlayerConfig.class)

public class TestCDPlayer {

    @Autowired

    private Player cdPlayer;

 

    @Test

    public void testNotNull(){

        cdPlayer.play();

    }

}

  三、将一个对象注入到另一个对象中

  比如有一个People类,要为其注入一个Player类,然后其跑步的时候会调用Player类的play方法,这样实现即可:

  @Component

  public class People {

      //Spring会自动把生成的Player注入进来

      @Autowired

      private Player player;

     //注入的规则分为两步:

     //1、Spring会先查找id为player的Bean对象,如果找到,注入进来

     //2、如果没有找到id为player的Player对象,那么就去寻找Spring容器中查找Player的对象,如果一个都没有,或者有两个或者多个,那么就会报错。

    

      public void run(){

          player.play();

          System.out.println("正在跑步");

      }

  }

  四、在JavaConfig中来装配bean

  一般来说,自动装配bean已经能为我们解决很多问题,但是,有的时候可能我们需要更自动的配置,比如我想获取同一个类的多个bean对象,就可以在JavaConfig中声明方法来装配bean:

@Bean

public Player player(){

    return new CDPlayer();

}

 此时Spring容器就会生成一个Player的对象,其id为player(注意id与方法名是一样的)

 我们也可以为其指定id:

@Bean(name="firstPlayer")

public Player player(){

    return new CDPlayer();

}

五、如何进行使用:

Spring是围绕ApplicationContext上下文容器的,所以无论如何,Spring都要根据配置信息将bean加载到ApplicationContext上下文容器中

 1、main函数中进行测试:

使用AnnotationConfigApplicationContext来从JavaConfig中获取到上下文对象:

public static void main(String args[]){

    ApplicationContext context = new AnnotationConfigApplicationContext(PlayerConfig.class);

    //通过id来获取:

   People people = (People) context.getBean("people");

   people.run();

}

2、test测试:

只需要指定ContextConfiguration即可:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = PlayerConfig.class)

public class TestSpring {

   @Autowired

   private Player cdPlayer;

   @Autowired

   private People people;

   @Test

   public void testNotNull(){

       cdPlayer.play();

   }

   @Test

   public void testPeople(){

       people.run();

   }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: