SpringBoot的底层注解-@Import、@Conditional、@ImportResource、@ConfigurationProperties

  1. 底层注解:@Import

    功能:导入组件

    在配置类MyConfig上添加注解@Import({User.class,DBHelper.class})

    会添加User和DBHelper组件,默认组件的名字就是全类名

    MyConfig类中

    @Import({User.class, DBHelper.class})
    public class MyConfig {
    }
    

    在主程序类中测试

    //返回IOC容器
    ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
    String[] beanNamesForType = run.getBeanNamesForType(User.class);
    for (String s : beanNamesForType){
    		System.out.println(s);
    }
    DBHelper dbHelper = run.getBean(DBHelper.class);
    System.out.println(dbHelper);
    

在这里插入图片描述

  1. 底层注解:@Conditional

    功能:条件装配,满足Conditional指定的条件,则进行组件注入

    @Condition是一个根注解,下面还派生了好多的注解

    idea下查找注解:shift两下查找@Conditional,点进去@Conditional,ctrl+H查看结构

SpringBoot的底层注解-@Import、@Conditional、@ImportResource、@ConfigurationProperties_第1张图片

​ 以containsBean为例,containsBean:容器中是否含有某组件

        boolean containsBean = run.containsBean("user01");
        System.out.println("是否含有user01组件:" + containsBean);
        boolean tom = run.containsBean("tom");
        System.out.println("是否含有tom组件:" + tom);

在这里插入图片描述

​ 没加@ConditionalOnBean(name = “tom”):当容器中含有tom组件时,再给容器中注入其他组件user01

@Bean   //给容器中添加组件。以方法名作为组件的id,返回类型就是组件类型,方法返回的对象就是组件在容器中的实例
@ConditionalOnBean(name = "tom")	//在容器中含有tom组件,再给容器注入组件user01
        public User user01(){
        return new User("zhangsan",18);
}

在这里插入图片描述

​ 因为在容器中没有tom组件,所以user01组件也没有注入成功,在结果中显示false

也可以把@ConditionalOnBean(name = “tom”)标注在类上,意思是只有容器中含有tom组件时,类中所有的其他组件才可以注入

​ 在容器中注入tom组件,就能够同时将user01组件也注入

@Bean("tom")
public Pet tomcatPet(){
        return new Pet("tomcat");
}

@Bean   //给容器中添加组件。以方法名作为组件的id,返回类型就是组件类型,方法返回的对象就是组件在容器中的实例
@ConditionalOnBean(name = "tom")
public User user01(){
		return new User("zhangsan",18);
}

在这里插入图片描述

注意:一定要保证tom组件注入在user01组件之前,否则user01组件不会注入

与此相对的@ConditionalOnMissingBean(name = “tom”):意思是容器中没有tom的时候添加组件

3.底层注解:@ImportResource

使用SpringBoot使用配置类替代了xml文件配置,当之前采用了xml注入的时候,想要使用SpringBoot替代,不想要一个个的将xml的组件替代到配置类中,就可以采用@ImportResource注解

如下,如果在xml配置中注册了两个组件


        
        



		

不加入@ImportResource注解时,组件未曾加载

在这里插入图片描述

将xml文件注入的组件通过配置类注入,就在配置类的上方加上@ImportResource注解

@ImportResource("classpath:bean1.xml")	//加载原生配置文件资源bean1.xml
public class MyConfig {
}

在这里插入图片描述

4.底层注解:@ConfigurationProperties

功能:配置绑定

在开发过程中需要使用到的配置信息一般会方法properties配置文件中,然后通过解析properties文件使用,回忆一下JDBC数据库信息的配置,使用原生的java解析properties文件还是比较麻烦的,通过properties类加载进来配置文件,然后遍历每一个key-values值进行抽取数据,很是麻烦,在SpringBoot里面就很简单,因为出现了@ConfigurationProperties注解。

在.properties文件中写入所需配置

mycar.brand=BMW
mycar.price=100000

在Car类的上方添加注解@ConfigurationProperties,@ConfigurationProperties注解需要一个前缀属性prefix,这个前缀可以看作是一个对象,注意,只有在容器中的组件才可以拥有SpringBoot提供的强大功能,所以需要在类上加上@Component注解

/**
 * 只有在容器中的组件才可以拥有SpringBoot提供的强大功能
 */
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
}

在Controller类中进行测试

@Autowired  //自动注入car,因为car是和properties中的配置绑定的
    Car car;

    /**
     * @RequestMapping注解,映射请求,希望浏览器能给我们发送/car请求
     * @return
     */
    @RequestMapping("/car")
    public Car car(){
        return car;
    }

在这里插入图片描述

当配置文件改动了,刷新之后也可以重新读取配置文件

除此之外,还有其他的方式

在配置类中进行处理,在配置类上添加注解@EnableConfigurationProperties,意思就是开启属性配置功能,想要开启谁的属性配置功能就把谁传进来,比如想要开启Car的属性配置功能

@EnableConfigurationProperties(Car.class)
@EnableConfigurationProperties(Car.class) 
//两个功能:
//1、开启Car的属性配置功能 
//2、将这个Car这个组件自动注册到容器中

这种写法就解决了需要在Car类上添加@Component注解,当在开发中所使用的是第三方jar包,我们不能在别人的类上添加注解,就可以使用配置类的这种方法
在这里插入图片描述

如果不使用配置类@EnableConfigurationProperties(Car.class) ,那么就必须加入到容器中才可以,就是在Car类上添加@Component注解

你可能感兴趣的:(java,springboot,idea)