@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.com

prop.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() {
    }

    @ConstructorBinding
    public 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.com

prop.name=tom
prop.age=17
prop.email=456@qq.com

method.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方法,查看运行结果 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/474950.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

天地一体化5G网络中LNA的辐射效应

Youssouf A S, Habaebi M H, Hasbullah N F. The radiation effect on low noise amplifier implemented in the space-aerial–terrestrial integrated 5G networks[J]. IEEE Access, 2021, 9: 46641-46651. 图2 面向卫星的5G综合网络架构方案 这篇论文《The Radiation Effect…

docker快速安装达梦数据库

docker快速安装达梦数据库 文章目录 docker快速安装达梦数据库前言环境准备下载镜像运行、配置容器 前言 因为公司需要将自己的底代码平台与客户的需求做适配,客户要求必须满足信创要求,使用达梦数据库。所以需要将原有的MySQL数据库与达梦数据库适配&a…

Android:adb命令

执行adb命令的窗口如下 Mac或Linux系统里的终端窗口; window系统运行输入cmd打开的指令窗口; Android Studio 里控制下面的Terminal窗口 1. 查看已链接的设备和模拟器 adb devices -l 2. 查看Android内核版本号 adb shell getprop ro.build.version.re…

面试笔记——Redis(集群方案:主从复制、哨兵模式和分片集群)

主从复制 在 Redis 主从集群中,一个主节点(Master)负责处理客户端的读写请求,而多个从节点(Slave)则负责复制主节点的数据,并对外提供读取服务——解决高并发问题。 主节点(Master&…

vue@2.7.16 使用less、less-loader

遇到问题,npm install less-loader7.3.0 --save安装好less-loader后,执行npm run serve 项目运行不起来,排查后发现在安装less-loader后就提示需要安装less,正确的安装应如下: npm install less less-loader7.3.0 --sa…

了解电子元器件商城价格变动的背后逻辑

电子元器件商城价格的变动背后存在着多种逻辑和因素,这些因素相互交织、相互作用,共同影响着价格的波动。以下是一些可能存在的背后逻辑: 供需关系: 供应量变化:电子元器件市场的供应量受到供应商生产能力、原材料供应…

linux内核input子系统概述

目录 一、input子系统二、关键数据结构和api2.1 数据结构2.1.1 input_dev2.1.2 input_handler2.1.3 input_event2.1.4 input_handle 2.2 api接口2.2.1 input_device 相关接口input_device 注册流程事件上报 2.2.2 input handle 相关接口注册 handle指定 handle 2.2.3 input han…

[隐私计算实训营学习笔记] 第1讲 数据要素流通

信任四基石 数据的分级分类 技术信任:全链路审计、闭环完成的数据可信流通体系 技术信任:开启数据密态时代 数据可流通的基础设施:密态天空计算

第3章 数据治理

思维导图 数据治理的定义:是在管理数据资产过程中行使权力和管控,包括计划、监控、和实施。 职能:指导所有其他数据管理领域的活动。目的:确保根据数据管理制度和最佳实践正确地管理数据。整体驱动力:确保组织可以从其…

sd卡数据不小心删除了如何恢复,sd卡中的数据不小心被删除了如何进行恢复

在现代科技快速发展的时代,SD卡已经成为我们存储和传输数据的重要工具之一。当您不小心删除了SD卡中的数据时,这种意外情况可能引起您的困惑和焦虑。那些重要的文件、无价的回忆似乎在转瞬间消失得无影无踪。面对这种突发的数据丢失问题,我深感理解。sd卡数据不小心删除了如…

Mac上轻松几步搞定Docker与Redis安装:从下载安装到容器运行实测全程指南

1、去官网下载docker 安装:把图标拉到应用程序即可把图标拉到应用程序即可 https://docs.docker.com/desktop/install/mac-install/ 2、docker拉取redis镜像 拉取命令,后面填上版本号3.2.1,可以看到已经成功了。 docker pull redis:3.2.1…

Guitar Pro8吉他学习 、打谱 、 创作神器,让你的吉他之路更上一层楼!

Guitar Pro8吉他学习 、打谱 、 创作神器,让你的吉他之路更上一层楼!轻松学习吉他,实现音乐梦想,Guitar Pro8助你一臂之力! Guitar Pro 2024 win-安装包下载如下: https://wm.makeding.com/iclk/?zoneid5…

u盘数据删除或者移除了怎么办?冷静,恢复指南来帮你

在数字化时代,U盘已成为我们存储和传输数据的重要设备。然而,有时由于疏忽或误操作,我们可能会不小心删除或移除了U盘上的重要数据。面对这种情况,许多人可能会感到焦虑和困惑,不知道如何是好。本文将为您提供一些建议…

【Unity】宏定义Scripting Define Symbols

1.宏的用处 我们在使用Unity开发的时候,经常需要根据不同环境执行不同的代码 比如安卓手机和苹果手机获取路径代码 这个时候,宏就派上用场了。 代码示例: //获取路径public string GtePath(){//不同平台,取不同的存储路径string…

java 项目新建遇到的问题

IntelliJ IDEA创建Spring工程 报错1:Selected version of Java 17 is not supported by the project SDK ‘1.8’. Either choose a lower version of Java, or set a higher version of the SDK. 解决方法: 报错2:Cannot download ‘htt…

电脑照片分辨率怎么调?这款dpi修改工具好用

许多考试平台在上传证件照片的时候,大多都会对图片分辨率有具体要求,但是如果遇上手上的图片分辨率达不到要求,那么怎么改图片分辨率呢?可以利用专业的dpi修改工具来处理,比如今天分享的就是一个在线修改图片分辨率的方…

右键菜单事件

<div id"editor-container"></div> <div class"custom-context-menu" id"customContextMenu"> <ul> <li value"copy">创建副本</li> <li value"delete" class"ed-bottom-line&…

每日一题——LeetCode1716.计算力扣银行的钱

方法一 循环模拟 每七天为一个节点&#xff0c;从周一到周日每天比前一天1&#xff0c;到了下一个周一&#xff0c;比上一个周一1&#xff0c;再继续从周一到周日每天1 var totalMoney function(n) {let Monday 1,now1,sum1for(let i2;i<n;i){now1sumnowif(i%70){Monday1…

vue学习笔记27-组件生命周期⭐

每个vue组件实例在创建时都需要经历一系列初始化步骤&#xff0c;比如设置好数据侦听&#xff0c;编译模板&#xff0c;挂载实列到DOM&#xff0c;以及在数据改变时更新DOM。在此过程中&#xff0c;它也会运行称为生命周期钩子的函数&#xff0c;让开发者有机会在特定阶段运行自…
最新文章