• Spring中Bean装配方式整理,第三方jar包引入bean的方法


    在这里插入图片描述

    在SpringBoot的大环境下,基本上很少使用之前的xml配置Bean,主要是因为这种方式不好维护而且也不够方便。 因此本篇博文也不再介绍Spring中通过xml来声明bean的使用方式。

    注解装配Bean

    1、使用@Component等派生注解
    只要在类上加类上加 @Component 注解即可,该注解只要被扫描到就会注入到spring的bean容器中。

    @Component
    public class AnoDemoBean {
    }
    
    • 1
    • 2
    • 3

    当然不只是@Component注解可以声明Bean,还有如:@Repository、@Service、@Controller 等常用注解同样可以。

    如果去看这些注解,就发现这些注解上本身就有加 @Component 注解

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component   //可以看到@Service注解上有添加@Component,  @Repository和@Controller也一样。        
    public @interface Service {
        @AliasFor(
            annotation = Component.class
        )
        String value() default "";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这系列注解的出现,给我们带来了极大的便利。我们不需要像以前那样在bean.xml文件中配置bean了,现在只用在类上加上相关注解,就能轻松完成bean的定义。

    这四种注解在功能上其实没有特别的区别,不过在业界有个不成文的约定:

    • Controller 一般用在控制层
    • Service 一般用在业务层
    • Repository 一般用在数据层
    • Component 一般用在公共组件上

    2、@Bean定义方式
    这种方式,主要是结合Configuration来定义bean,首先是声明一个配置类,而后再配置类中,经过返回bean对象的方法形式来声明bean,通常使用姿式以下

    @Data
    public class ConfigDemoBean {
    }
    
    @Configuration
    public class BeanLoadConfig {
        @Bean
        public ConfigDemoBean configDemoBean() {
            return new ConfigDemoBean();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    须要说明的一点是BeanLoadConfig类自己也被Spring容器看为一个Bean。

    3、@Component VS @Bean
    1)作用对象不同:@Component 注解作用于类,而 @Bean 注解作用于方法。

    这样的特点会让 @Bean 方式更加灵活。比如当我们引用第三方库中的类需要装配到 Spring 容器时,只能通过 @Bean 来实现。

    比如

    @Configuration
    public class WireThirdLibClass {
        @Bean
        public ThirdLibClass getThirdLibClass() {
            //第三方的ThirdLibClass类
            return new ThirdLibClass();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    再比如

    @Bean
    public OneService getService(status) {
        case (status)  {
            when 1:
                    return new serviceImpl1();
            when 2:
                    return new serviceImpl2();
            when 3:
                    return new serviceImpl3();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    这两点都是@Component无法做到,只能@Bean实现,所以说@Bean更加灵活。

    2)@Component通常是通过类路径扫描来自动装配到Spring容器中。而@Bean通常我们会在该注解的方法中定义产生这个bean的逻辑。

    我们可以加一些@Conditional,@ConditionalOnBean等等一些注解来控制是否声明该Bean,不会一开始就自动装配到Spring容器中。

    比如

    public class MacCondition implements Condition {
    
       @Override
        public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
            Environment environment = conditionContext.getEnvironment();
            String property = environment.getProperty("os.name");
            if (property.contains("Mac")) {
                log.info("当前操作系统是:Mac OS X");
                return true;
            }
            return false;
        }
    }
    
    
    
    @Configuration
    public class ConditionalConfig {
        /**
         * 如果MacCondition的实现方法返回true,则注入这个bean
         */
        @Bean("mac")
        @Conditional({MacCondition.class})
        public SystemBean systemMac() {
            log.info("ConditionalConfig方法注入 mac实体");
            return new SystemBean("Mac ios系统","001");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    上面的例子表示,如果当前操作系统是Mac,才会注入当前Bean。这个也只能 @Bean 注解才能实现。

    总结:@Component和@Bean都是用来注册Bean并装配到Spring容器中,但是Bean比Component的自定义性更强。可以实现一些Component实现不了的自定义加载类。

    引入外部jar包的Bean

    如果上面的注解加在当前项目中那么当SpingBoot主类启动的时候,@SpringBootApplication注解会默认去扫描的本包和它的子包的所有需要装配的类,自动装配到spring的bean容器中。

    但是如果你提供了一个Jar包供第三方用户使用,那么你这个jar包中的Bean,能被第三方加载么?

    这就要看你当前项目的包名和你你引用的第三方Jar包的包名了。

    如果你当前项目本包的地址是com.jincou 而你引用的第三方Jar的本包是 com.third,那么也就是第三方Jar的Bean无法被扫描到,所以也就无法注入到Spring容器中。

    比如这里有个第三方的Bean。要如何做才能被扫描注入到Spring容器中呢。

    package com.third.bean;
    
    import org.springframework.stereotype.Component;
    
    /**
     * @Description: 这个bean作为第三方bean 给依赖该jar包的项目使用
     */
    @Component
    public class ThirdComponentBean {
        private String type = "第三方ThirdComponent注解生成bean实体";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1、@ComponentScan
    很简单,既然@SpringBootApplication注解默认扫描只是当前项目的本包和它的子包,那就想办法让它扫描第三方jar的包就好了。

    /**
     * @Description: Springboot 启动类
     */
    @ComponentScan(basePackages ={"com.third.bean"})
    @SpringBootApplication()
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    @ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中。

    需要装配的类也就是上面加了@Controller,@Service,@Repository,@Component,@Configuration等等的注解的Bean到IOC容器中。

    这里不一定要加在启动类上,你可以加在加在装配的类上,但建议加在启动类上,比较直观,后期如果要改动或者去除也比较好找。

    2、@Import注解
    @ComponentScan是扫描整个包,但其实你可能只需注入一个或者几个指定的Bean,那我们可以考虑用 @Import 注解

    @Import(value= com.third.bean.ThirdComponentBean.class)
    @SpringBootApplication()
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这样做同样也会成功的将ThirdComponentBean对象注入到Spring的bean容器中。

    3、spring.factories
    上面两种注入方式都有个很明显缺点,就是如果我需要引用外部jar包的Bean的时候,都需要在当前项目配置 @ComponentScan 或者 @Import 去扫描才能注入当前Bean,这样显然不够友好。

    可不可以当前项目什么都不做就可以直接引用第三方jar的Bean呢?

    当然可以。

    我们只需要在将配置放在第三方jar指定的文件中即可,使用者会自动加载,从而避免的代码的侵入

    • 在资源目录下新建目录 META-INF
    • 在 META-INF 目录下新建文件 spring.factories
    • 在文件中添加下面配置
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.third.bean.ConfigurationBean
    
    • 1

    在这里插入图片描述

    参考:
    SpringBoot引入第三方jar的Bean的三种方式

  • 相关阅读:
    RabbitMQ-网页使用消息队列
    LeetCode 算法:盛最多水的容器c++
    Spring boot easyexcel 实现复合数据导出、按模块导出
    Elasticsearch使用篇 - 关联查询
    Mac opencv 摄像头权限
    N个元素进栈 出栈情况种数
    聚L-酪氨酸/聚(苯胺-吡咯)共聚物/谷氨酸包裹Fe3O4磁性纳米颗粒
    Kubeadm安装k8s高可用集群实战
    剩余内存无法满足申请时,系统会怎么做?
    【2022秋线上作业-第5次-第11-13周】选择题
  • 原文地址:https://blog.csdn.net/Koikoi12/article/details/126182092