• SpringBoot2.0---------------4、SpringBoot底层注解之添加组件


    1. @Configuration注解(属于Spring中的注解)


    @Configuration注解用于告诉Springboot这是一个配置类,从而取代了配置文件;在Spring中要需要通过配置文件将属性配置到Spring容器中。两者配置方式如下:

    1)配置文件方式将User、Pet类注入到Spring容器中:

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="user01" class="com.lyy.boot.bean.User">
            <property name="name" value="zhangsan">property>
            <property name="age" value="18">property>
        bean>
        <bean id="cat" class="com.lyy.boot.bean.Pet">
            <property name="name" value="tomcat">property>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2)配置类方式将User、Pet类注入到Spring容器中:

    /*
    * 1、配置类中使用@Bean注解标注在方法上给容器注册组件,默认也是单实例的
    * 2、配置类本身也是组件
    * 3、proxyBeanMethods = true,代理bean的方法,该属性默认为true
    *      Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
           Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
           组件依赖(个组件之间有调用)必须使用Full模式默认。其他默认是否Lite模式
    * */
    
    @Configuration(proxyBeanMethods = true) //告诉springboot这是一个配置类==配置文件
    public class Myconfig {
        /*
        * 外部对配置类中的这个组件注册方法调用多少次,获取的都是之前注册容器中的单实例对象
        * */
        @Bean //给容器中添加组件。以方法名作为组件的id,返回类型就是组件类型;返回的值,就是组件在容器中保存的实例
        public User user01(){
            User zhangsan = new User("zhangsan", 18);
            zhangsan.setPet(Tomcat());
            return zhangsan;
        }
        @Bean("tom")
        public Pet Tomcat(){
            return new Pet("luopeixi");
        }
    }
    
    • 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

    @Configuration注解声明该类是一个配置类,注解标于类的上方;对类中的每个方法添加@Bean注解实现对方法的bean对象注册,将其添加到Spring的IOC容器中。

    1)在@Configuration注解中:proxyBeanMethods属性用于代理bean的方法,默认值为true;
    Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
    Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
    组件依赖(个组件之间有调用)必须使用Full模式默认;其他默认是否Lite模式。如配置类中的user01所示,该配置调用了配置类中的其他配置,构成了组件依赖,因此必须使用Full模式。

    2)@Bean注解中,若不添加属性,则方法名对应bean标签中的id;若在注解后添加属性,所添加的属性为bean标签的id。


    2. @Component、@Controller、@Service、@Repository、@Import注解

    @Component、@Controller、@Service、@Repository、@Import、@Bean注解的功能都是给容器中注册组件

    @Import注解:将制定类型的组件添加到IOC容器中
    给容器中注册组件除了@bean,还有其他方法:
    @Component代表一个普通组件;
    @Controller代表一个控制器组件;
    @Service代表一个业务逻辑组件;
    @Repository代表一个数据库层组件
    这四个组件功能是一致的,只是应用的层级不同。

    @ComponentScan注解:根据注解中定义的路径,将符合扫描规则的类装配到Spring容器中

    @Import注解:将制定类型的组件添加到IOC容器中

    @Import({User.class, DBHelper.class})
    @Configuration(proxyBeanMethods = true) //告诉springboot这是一个配置类==配置文件
    public class Myconfig {
    ........
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    该代码中的@Import注解就是将User类、DBHelper类注册到IOC容器中


    3. @Conditional注解,该注解下面有许许多多子注解用于各种判定

    在这里插入图片描述

    @Conditional注解作用:按照一定条件进行判定,将满足条件的组件注入到IOC容器中

    @Import({User.class, DBHelper.class})
    @ConditionalOnBean(name = "tom")
    @Configuration(proxyBeanMethods = true) //告诉springboot这是一个配置类==配置文件
    public class Myconfig {
    
        /*
        * 外部对配置类中的这个组件注册方法调用多少次,获取的都是之前注册容器中的单实例对象
        * */
        
        @Bean //给容器中添加组件。以方法名作为组件的id,返回类型就是组件类型;返回的值,就是组件在容器中保存的实例
        public User user01(){
            User zhangsan = new User("zhangsan", 18);
            zhangsan.setPet(Tomcat());
            return zhangsan;
        }
        @Bean("tom22")
        public Pet Tomcat(){
            return new Pet("luopeixi");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    以@ConditionalOnBean(name = “tom”)为例,注解放在类上表示容器中若没有tom组件,则该类不会注册到IOC容器中(user01和tom22都不会进行注入);若放在方法上面,表示若容器中没有该组件,则该方法不会注入到容器中

  • 相关阅读:
    [单片机框架][bsp层][N32G4FR][framework][key_scan] 按键扫描
    JUC——ReentrantReadWriteLock
    labelme 使用笔记
    数据库总结
    【数据结构】轻松掌握二叉树的基本操作及查找技巧
    k8s配置集ConfigMap详解
    `算法知识` 二分图Biparitite-Graph
    SuccBI,带你玩转各种复杂的中国式报表
    FoLR:Focus on Local Regions for Query-based Object Detection论文学习笔记
    读vue源码搞懂响应式原理
  • 原文地址:https://blog.csdn.net/lyy_sss/article/details/126123615