Spring入门(三):通过JavaConfig装配bean

上一篇博客中,我们讲解了使用组件扫描和自动装配实现自动化装配bean,这也是最好的使用方式。

但是某些场景下,我们可能无法使用自动装配的功能,此时就不得不显式的配置bean。

比如我们引用了一个第三方类库,需要将类库中的某个类装配到项目中,我们不可能在该类上添加@Component注解,因此无法使用自动装配的功能。

Spring中有以下两种方式显式配置bean:

  1. 通过JavaConfig配置bean
  2. 通过xml配置bean

本篇博客主要讲解下通过JavaConfig配置bean的实现方法,通过xml配置bean的实现方法后续再单独写一篇博客。

我们还使用上一篇博客中的例子,不过代码会做适当修改。

package soundsystem.javaconfig;

public interface CompactDisc {
    void play();
}
复制代码
package soundsystem.javaconfig;

public class SgtPeppers implements CompactDisc {

    @Override
    public void play() {
        String title = "Sgt.Pepper's Lonely Hearts Club Band";
        String artists = "The Beatles";
        System.out.println("Playing " + title + " By " + artists);
    }
}
复制代码
package soundsystem.javaconfig;

public class CDPlayer {

    private CompactDisc compactDisc;

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

    public void play() {
        compactDisc.play();
    }
}
复制代码

注意:和上一篇博客相比,我们去掉了SgtPeppers类和CDPlayer类上的@Component注解。**

1.创建配置类

package soundsystem.javaconfig;

import org.springframework.context.annotation.Configuration;

@Configuration
public class CDPlayerConfig {
}
复制代码

2.声明bean

在JavaConfig中,我们使用@Bean注解来声明bean,如下所示:

package soundsystem.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CDPlayerConfig {
    @Bean
    public CompactDisc sgtPeppers() {
        return new SgtPeppers();
    }
}
复制代码

默认生成的bean ID和方法名一致,即sgtPeppers,不过我们可以自定义:

@Bean(name = "lonelyHeartsClub")
public CompactDisc sgtPeppers() {
     return new SgtPeppers();
}
复制代码

上面声明的bean比较简单,没有任何其它依赖,但是有些复杂的bean,比如CDPlayer,它依赖于CompactDisc,那我们该如何声明呢?

简单的一种方式是,直接使用刚刚定义的sgtPeppers()方法作为CDPlayer构造器的参数依赖:

@Bean
public CDPlayer cdPlayer() {
    return new CDPlayer(sgtPeppers());
}
复制代码

不过更建议的是以下方式,将依赖项作为bean方法的参数,Spring会自动匹配到参数依赖项:

@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
    return new CDPlayer(compactDisc);
}
复制代码

此时配置类的代码为:

package soundsystem.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CDPlayerConfig {
    @Bean
    //@Bean(name = "lonelyHeartsClub")
    public CompactDisc sgtPeppers() {
        return new SgtPeppers();
    }

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

    @Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc) {
        return new CDPlayer(compactDisc);
    }
}
复制代码

3.验证bean是否装配成功

新建测试类CDPlayerTest:

package soundsystem.javaconfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class CDPlayerTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        CompactDisc compactDisc = (SgtPeppers) applicationContext.getBean("sgtPeppers");
        compactDisc.play();

        CDPlayer cdPlayer = applicationContext.getBean(CDPlayer.class);
        cdPlayer.play();
    }
}
复制代码

运行结果:

从运行结果可以看出,bean装配成功。

4.源码地址

github.com/zwwhnly/Spr…,欢迎大家下载,有问题可以多多交流。

转载于:https://juejin.im/post/5c7f900cf265da2d8d6a03ac

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值