SpringBoot底层注解

1. @Configuration

  1. 基本使用
    • 告诉SpringBoot这是一个配置类等同于配置文件(bean.xml)
    • 配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
    • 配置类本身也是组件
    • proxyBeanMethods:代理bean的方法
      • Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
      • Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
      • 组件依赖必须使用Full模式默认。其他默认是否Lite模式
      package com.marchsoft.boot.config;
      
      import com.marchsoft.boot.bean.Pet;
      import com.marchsoft.boot.bean.User;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      /**
       * 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
       * 2、配置类本身也是组件
       * 3、proxyBeanMethods:代理bean的方法
       */
      @Configuration(proxyBeanMethods = true)//告诉SpringBoot这是一个配置类等同于配置文件
      public class MyConfig {
      
          @Bean//给容器中添加组件。以方法名作为组建的id。返回值类型是组件类型。返回的值,就是组件在容器中的实例
          public User user01() {
              User zhangsan = new User("zhangsan",17);
              zhangsan.setPet(tomcatPet());
              return zhangsan;
          }
      
          @Bean(name = "benben")
          public Pet tomcatPet() {
              return new Pet("笨笨");
          }
      
      }
      
      当上面代码中的proxyBeanMethods值为true时
      User user01 = run.getBean("user01",User.class);
      Pet tom = run.getBean("benben",Pet.class);
      System.out.println("宠物"+(user01.getPet() == tom));
      
      输出的结果为true,为false则输出的为false
      * proxyBeanMethods的属性值为true时,它会在容器中加载是否有该组件,如果有则直接调用,如果没有再创建
      * proxyBeanMethods的属性值为false时,不管容器中是否有对应的组件,都不会去调用,而是直接创建。

2. @Bean

  1. 基本使用:给容器中添加组件。以方法名为组件的id。返回值类型是组件类型,返回的值,就是组件的容器中的实例。说白了就是将实体类进行实例化,然后交给SpringBoot来管理。
    @Bean//给容器中添加组件。以方法名作为组建的id。返回值类型是组件类型。返回的值,就是组件在容器中的实例
    public User user01() {
        User zhangsan = new User("zhangsan",17);
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }
    
    @Bean(name = "benben")
    public Pet tomcatPet() {
        return new Pet("笨笨");
    }
    

3. 一些其他注解

  • @Component 将一些类变成组件加到容器中只有在容器中的组件,才会拥有SpringBoot提供的强大功能
  • @Controller 配置Controller层注解
  • @Service 配置Service层注解
  • @Repository 配置dao层注解

4. @Import

  1. 基本用法:@Import({User.class,DBHelper.class})给容器中自动创建出这两个类型组件、默认组件的名字就是全类名
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false)//告诉SpringBoot这是一个配置类等同于配置文件
public class MyConfig {

    @Bean//给容器中添加组件。以方法名作为组建的id。返回值类型是组件类型。返回的值,就是组件在容器中的实例
    public User user01() {
        User zhangsan = new User("zhangsan",17);
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }

    @Bean(name = "benben")
    public Pet tomcatPet() {
        return new Pet("笨笨");
    }

}

5. @Conditional

  1. 基本用法:满足Conditional指定条件,则进行组件注入

6. @ImportResource

  1. 基本用法:此注解添加在配置类上面,用来导入原spring中xml配置文件
    @ImportResource("classpath:bean.xml")

7. @Component+@ConfigurationProperties

  1. 基本用法:这两个注解是加在实体类上面的其中第一个注解就是将该实体类加入容器中,第二个注解就是将配置文件中的对应属性的值赋值给此实体类中对应属性的值。
    • 实体类中的代码
      @Component
      @ConfigurationProperties(prefix = "mycar")
      
    • 配置文件中的代码
      mycar.brand=BYD
      mycar.price=100000
      

8. @EnableConfigurationProperties + @ConfigurationProperties

  • 该注解的功能与上面的注解一样都是将配置文件中的值赋值给实体类中对应的属性。
  • @EnableConfigurationProperties注解的功能有两个
    • 开启Car配置绑定功能
    • 把这个Car这个组件自动注册到容器中

猜你喜欢

转载自blog.csdn.net/weixin_45566730/article/details/113839194