当前位置: 首页 > news >正文

@EnableConfigurationProperties注解使用

前言

当我们想把配置的内容,动态赋值到某个配置类上的时候,可以使用@EnableConfigurationProperties + @ConfigurationProperties注解

代码准备

创建配置文件prop.properties

name=ada
age=18
email=123@qq.com

创建配置类

@ComponentScan("com.test.pops")
@PropertySource("classpath:prop.properties")
public class AppConfig {}

创建启动类

public class Main {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);}
}

查看@ConfigurationProperties源码

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface ConfigurationProperties {/*** The prefix of the properties that are valid to bind to this object. Synonym for* {@link #prefix()}. A valid prefix is defined by one or more words separated with* dots (e.g. {@code "acme.system.feature"}).* @return the prefix of the properties to bind*/@AliasFor("prefix")String value() default "";/*** The prefix of the properties that are valid to bind to this object. Synonym for* {@link #value()}. A valid prefix is defined by one or more words separated with* dots (e.g. {@code "acme.system.feature"}).* @return the prefix of the properties to bind*/@AliasFor("value")String prefix() default "";/*** Flag to indicate that when binding to this object invalid fields should be ignored.* Invalid means invalid according to the binder that is used, and usually this means* fields of the wrong type (or that cannot be coerced into the correct type).* @return the flag value (default false)*/boolean ignoreInvalidFields() default false;/*** Flag to indicate that when binding to this object unknown fields should be ignored.* An unknown field could be a sign of a mistake in the Properties.* @return the flag value (default true)*/boolean ignoreUnknownFields() default true;}

由源码我们可以得知这个注解可以作用于类上,作用于方法上

作用于类上

@EnableConfigurationProperties注解不指定value

创建TypeConfig

@ConfigurationProperties
@Component
public class TypeConfig {private String name;private Integer age;private String email;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}

配置类不指定value 

@ComponentScan("com.test.pops")
@PropertySource("classpath:prop.properties")
@EnableConfigurationProperties
public class AppConfig {}

运行main方法,查看运行结果

@EnableConfigurationProperties注解指定value

TypeConfig去除@Component注解

@ConfigurationProperties
public class TypeConfig {private String name;private Integer age;private String email;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}

配置类指定value

@ComponentScan("com.test.pops")
@PropertySource("classpath:prop.properties")
@EnableConfigurationProperties({TypeConfig.class})
public class AppConfig {}

运行main方法,查看运行结果 

@ConfigurationProperties设置prefix

改造TypeConfig

@ConfigurationProperties(prefix = "prop")
public class TypeConfig {private String name;private Integer age;private String email;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}

修改配置文件prop.properties

name=ada
age=18
email=123@qq.comprop.name=tom
prop.age=17
prop.email=456@qq.com

运行main方法,查看运行结果 

使用@ConstructorBinding注解绑定到构造器上

改造TypeConfig

@ConfigurationProperties(prefix = "prop")
public class TypeConfig {private String name;private Integer age;private String email;public TypeConfig() {}@ConstructorBindingpublic TypeConfig(String name, Integer age, String email) {this.name = name;this.age = age;this.email = email;}
}

运行main方法,查看运行结果 

PS : @ConstructorBinding注解只能在@EnableConfigurationProperties注解指定value的时候使用

作用于方法上

修改配置类prop.properties

name=ada
age=18
email=123@qq.comprop.name=tom
prop.age=17
prop.email=456@qq.commethod.name=bob
method.age=16
method.email=789@qq.com

创建MethodConfig

@Configuration
public class MethodConfig {@Bean@ConfigurationProperties(prefix = "method")public Person person() {return new Person();}public static class Person {private String name;private Integer age;private String email;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}}
}

运行main方法,查看运行结果 

相关文章:

  • 【数据结构和算法】-贪心算法
  • 浅谈Spring框架
  • Python 网络编程
  • [Java、Android面试]_10_Java中==与equal()方法的区别?重写equal()方法?
  • Redisson 分布式锁原理分析
  • 智能合约 之 ERC-20介绍
  • vue-admin-template极简的 vue admin 管理后台的动态路由实现方法
  • 002_avoid_for_loop_in_Matlab避免使用for循环
  • Session、Cookie 和 Token的保存
  • 【蓝桥杯选拔赛真题72】python输出整数 第十五届青少年组蓝桥杯python选拔赛真题 算法思维真题解析
  • 软件测试教程 自动化测试之Junit框架
  • 网络安全进入AI赋能时代
  • 记录C++中,子类同名属性并不能完全覆盖父类属性的问题
  • Linux编程3.8 进程-守护进程
  • nodejs中使用@maxmind/geoip2-node 查询地理位置信息
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 4. 路由到控制器 - Laravel从零开始教程
  • ES6--对象的扩展
  • golang 发送GET和POST示例
  • Hibernate【inverse和cascade属性】知识要点
  • HTTP那些事
  • jdbc就是这么简单
  • JS字符串转数字方法总结
  • nginx 负载服务器优化
  • Objective-C 中关联引用的概念
  • PaddlePaddle-GitHub的正确打开姿势
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • 爱情 北京女病人
  • 百度小程序遇到的问题
  • 彻底搞懂浏览器Event-loop
  • 翻译:Hystrix - How To Use
  • 浮现式设计
  • 构建工具 - 收藏集 - 掘金
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 深度学习中的信息论知识详解
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 我看到的前端
  • 一天一个设计模式之JS实现——适配器模式
  • NLPIR智能语义技术让大数据挖掘更简单
  • python最赚钱的4个方向,你最心动的是哪个?
  • ​io --- 处理流的核心工具​
  • #单片机(TB6600驱动42步进电机)
  • (简单) HDU 2612 Find a way,BFS。
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (四) Graphivz 颜色选择
  • (一) storm的集群安装与配置
  • (转)nsfocus-绿盟科技笔试题目
  • (转)visual stdio 书签功能介绍
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • ./configure、make、make install 命令
  • .jks文件(JAVA KeyStore)
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .net core webapi 大文件上传到wwwroot文件夹