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


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

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

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

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

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

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

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

    package chapter02.javaconfig;
    
    public interface CompactDisc {
        void play();
    }
    
    package chapter02.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 chapter02.javaconfig;
    
    public class CDPlayer {
    
        private CompactDisc compactDisc;
    
        public CDPlayer(CompactDisc compactDisc) {
            this.compactDisc = compactDisc;
        }
    
        public void play() {
            compactDisc.play();
        }
    }
    

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

    1. 创建配置类

    package chapter02.javaconfig;
    
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class CDPlayerConfig {
    }
    

    2. 声明bean

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

    package chapter02.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 chapter02.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 chapter02.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. 源码及参考

    源码地址:https://github.com/zwwhnly/spring-action.git,欢迎下载。

    Craig Walls 《Spring实战(第4版)》

    原创不易,如果觉得文章能学到东西的话,欢迎点个赞、评个论、关个注,这是我坚持写作的最大动力。

    如果有兴趣,欢迎添加我的微信:zwwhnly,等你来聊技术、职场、工作等话题(PS:我是一名奋斗在上海的程序员)。

  • 相关阅读:
    Ubuntu修改源
    设置Div多行文本超出时,以省略号代替
    通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面
    图片压缩的几种工具(初稿)
    DFA算法 处理屏蔽词库
    Xcode版本太低引发的bug,xcode各种版本下载方式详解
    Cocos2dx3.11.1Android播放视频,后台 黑屏,无法记忆播放bug修改
    cocos2d-x视频控件VideoPlayer的用户操作栏进度条去除(转载)
    python操作文件案例二则
    Live2d-cocos2dx教程(一)例子搭建及运行
  • 原文地址:https://www.cnblogs.com/zwwhnly/p/10484501.html
Copyright © 2020-2023  润新知