Spring实战--通过Java装配bean

在有些场景中,自动化配置是行不通的,@Autowired和@Component不能用了,因此需要使用显式配置;这一节讲述Java代码装配的流程;
JavaConfig有以下两点好处:

  • JavaConfig是更好的方案,因为它更强大、更安全且对重构友好;
  • JavaConfig与应用程序的业务领域不同,它不应侵入到业务逻辑代码之中;

业务逻辑如下:CD需要CDPlayer才能播放,即CD依赖于CDPlayer

本文设计的文件目录结构如下:
在这里插入图片描述
其中,除了CDPlayerConfig类以外的代码如下:

//CompactDisc接口,定义CD的操作
public interface CompactDisc {
    public void play();
}

//MediaPlayer接口,定义CDPlayer的操作
public interface MediaPlayer {
    public void play();
}

//SgtPeppers,实现CompactDisc接口
public class SgtPeppers implements CompactDisc{
    private String title="Sgt.Pepper's Lonely Hearts Club Band";
    private String artist="The Beatles";

    public void play() {
        System.out.println("play "+title+" by "+artist);
    }
}

//CDPlayer,实现MediaPlayer接口
public class CDPlayer implements MediaPlayer{
    private CompactDisc cd;

    public CDPlayer(CompactDisc cd) {
        this.cd = cd;
    }

    public void play(){
        cd.play();
    }
}

下面重点讲解CDPlayerConfig配置类

1.创建配置类

@Configuration表明这是一个配置类,移除了@ComponentScan,就目前来看,这个配置类没有任何作用,还不能扫描bean;

@Configuration
public class CDPlayerConfig{
    
}

2.声明简单的bean

在JavaConfig中声明bean,只需要给方法添加注解@Bean;对bean的命名有两种方法:

  • 默认命名
  • 指定命名
@Configuration
public class CDPlayerConfig{
    //方法1.默认名称sgtPeppers
    @Bean
    public CompactDisc sgtPeppers() {
        return new SgtPeppers();
    }
	//方法2.指定名称为lonelyHeartsClubBand
	@Bean(name="lonelyHeartsClubBand")
    public CompactDisc sgtPeppers() {
        return new SgtPeppers();
    }
}

3.借助JavaConfig实现注入

目前,我们已经声明了bean,但是从业务逻辑上讲,CD是不能播放的,只能依赖CDPlayer播放,即对外暴露的方法应该是CDPlayer.play()而不是CompactDisc.play();那么下面需要对CDPlayer注入CompactDisc;

@Bean
public CDPlayer cdPlayer(){
    return new CDPlayer(sgtPeppers());
}

分析:

  • 这个方法会创建一个bean实例并将其注册到Spring应用上下文中;
  • 看起来是调用了cdPlayer方法返回了对象,实际上,在添加了@Bean注解后,Spring会拦截所有对该方法的调用,并确保直接返回该方法创建的bean;
    在这里插入图片描述
    CDPlayerConfig中的两个方法声明@Bean后,相当于注册到容器中(如图所示),而cdPlayer()依赖于sgtPeppers(),这就形成了sgtPeppers()的注入,一旦外部需要获取cdPlayer这个bean,就会注入sgtPeppers bean;
    其实注入的方式有很多种,不局限于CDPlayer的构造方法,属性setter或者更普通的方法名都可以,只要是给普通方法声明@Bean且需要注入sgtPeppers(或CompactDisc) bean即可;带有@Bean注解的方法可以采用任何必要的Java功能来产生bean实例,上面的构造器只是一个简单样例;

4.验证JavaConfig装配

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
//这两个注解是必需的
public class SgtPeppersTest {

    AnnotationConfigApplicationContext annotationConfigApplicationContext=
            new AnnotationConfigApplicationContext(CDPlayerConfig.class);
    CDPlayer cdPlayer= (CDPlayer) annotationConfigApplicationContext.getBean("cdPlayer");

    @Test
    public void play() {
        cdPlayer.play();
    }
}

测试结果在这里插入图片描述**另外,bean都是单例的,即代码中的getBean方法无论执行多少次,只要Bean ID不变,获取的对象都是一样的;**测试代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class SgtPeppersTest {

    AnnotationConfigApplicationContext annotationConfigApplicationContext=
            new AnnotationConfigApplicationContext(CDPlayerConfig.class);
    CDPlayer cdPlayer= (CDPlayer) annotationConfigApplicationContext.getBean("cdPlayer");
    CDPlayer cdPlayer1= (CDPlayer) annotationConfigApplicationContext.getBean("cdPlayer");
    @Test
    public void singleBean(){
        System.out.println(cdPlayer.hashCode());
        System.out.println(cdPlayer1.hashCode());
    }
}

在这里插入图片描述
两个对象hashcode一样,即两者是一样的;

5.总结

JavaConfig装配Bean流程如下:

  • 确定业务流程,创建接口类和实现类;
  • 创建配置类(声明@Configuration),在配置类中使用@Bean声明bean(可以使用默认命名或者指定命名);
  • 在配置类中声明方法(加上@Bean),该方法通过构造器、setter方法或者普通方法与需要依赖的bean发生依赖关系;
  • (测试)通过AnnotationConfigApplicationContext加载配置类,使用Spring的应用上下文,通国getBean方法获取bean并测试bean的方法;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值