• @ConfigurationProperties注解使用


    https://blog.csdn.net/qq_44754515/article/details/125758879
    @ConfigurationProperties注解使用

    业务场景是这样的,在项目中最近涉及到一个某个内容剩余量预警的需求,当余量低于某个值,就发送短信给相关人。
    而关于接收短信的相关人员是写在配置文件中,需要通过代码去获取。
    但是虽然我们只需要用到接受短信相关人员这个配置,但是配置文件中相关人员的配置有个前缀warning,格式为warning.xxxx.且不仅仅有相关人员这一个配置,而是多个带有以warning为前缀的配置
    当时第一时间想到的当然是@Value,但是通过看前辈的代码,发现它专门写了一个类,并且用上了@ConfigurationProperties,对于这个注解不太熟悉,特来学习:

    一、@ConfigurationProperties简介

    @ConfigurationProperties是springboot提供读取配置文件的一个注解。其对应的bean的后置处理器为ConfigurationPropertiesBindingPostProcessor。
    在这里插入图片描述
    它实现了BeanPostProcessor接口,在bean被实例化后,会调用后置处理,递归的查找属性,通过反射注入值,对大多数属性而言强制需提供其setter和getter方法
    但是属性名称不要求一定相同,只需保证“set”字符串拼接配置文件的属性和setter方法名相同即可
    如下:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    二、 @ConfigurationProperties和@Value 2个注解的区别

    @ConfigurationProperties注解支持属性文件和javabean的映射,而@Value支持spel表达式。
    如果是多个属性映射,而且常常被复用,推荐使用@ConfigurationProperties,如果只读取单个属性则使用@Value要方便许多

    三、@ConfigurationProperties的用法

    场景一

    使用@ConfigurationProperties和@Component注解到bean定义类上,这里@Component代指同一类实例化Bean的注解。所以@Configuration,@Component,@Service,@Controller,@Repository等注解同样可以使用。

    基本使用实例如下:

    @Component
    // 表示使用配置文件中前缀为user1的属性的值初始化该bean定义产生的的bean实例的同名属性
    // 在使用时这个定义产生的bean时,其属性name会是Tom
    @ConfigurationProperties(prefix = "user1")
    public class User {
    	private String name;
    	// 省略getter/setter方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    对应application.properties配置文件内容如下:

    user1.name=Tom
    
    • 1

    在此种场景下,当Bean被实例化时,@ConfigurationProperties会将对应前缀的后面的属性与Bean对象的属性匹配。符合条件则进行赋值。

    场景二

    使用@ConfigurationProperties和@Bean注解在配置类的Bean定义方法上。以数据源配置为例:

    @Configuration
    public class DataSourceConfig {
    	@Primary
    	@Bean(name = "primaryDataSource")
    	@ConfigurationProperties(prefix="spring.datasource.primary")
    	public DataSource primaryDataSource() {
    		return DataSourceBuilder.create().build();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    这里便是将前缀为“spring.datasource.primary”的属性,赋值给DataSource对应的属性值。

    @Configuration注解的配置类中通过@Bean注解在某个方法上将方法返回的对象定义为一个Bean,并使用配置文件中相应的属性初始化该Bean的属性。

    场景三

    使用@ConfigurationProperties注解到普通类,然后再通过@EnableConfigurationProperties定义为Bean

    @ConfigurationProperties(prefix = "user1")
    public class User {
    	private String name;
    	// 省略getter/setter方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这里User对象并没有使用@Component相关注解。
    而该User类对应的使用形式如下:

    @SpringBootApplication
    @EnableConfigurationProperties({User.class})
    public class Application {
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    四、参考链接

    https://blog.csdn.net/lucky_ly/article/details/90049513
    https://hello.blog.csdn.net/article/details/104575745?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-104575745-blog-90049513.pc_relevant_aa&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-104575745-blog-90049513.pc_relevant_aa&utm_relevant_index=1

  • 相关阅读:
    君の名は~ 观后感
    dp1,明天补题解
    【bzoj1222】[HNOI2001]产品加工
    Daily~Miracles
    luogu 1273有线电视网
    luogu 1373
    codeforces 721C
    codeforces 706E
    The~Best~Chanteur~宇多田ヒカル
    codeforces706D
  • 原文地址:https://www.cnblogs.com/sunny3158/p/16574116.html
Copyright © 2020-2023  润新知