springboot中@ConfigurationProperties注解的用法

embedded/2024/10/11 0:47:20/

简介

@ConfigurationProperties注解在Spring Boot框架中提供了一种将外部配置(如application.properties或application.yml文件中的属性)绑定到Java对象的便捷方式。以下是@ConfigurationProperties注解的详细用法

基本用法

定义配置类

  • 创建一个普通的Java类,并使用@ConfigurationProperties注解来指定配置文件中属性的前缀。
  • 类中的字段需要与配置文件中的属性名称相匹配(考虑驼峰命名和下划线的转换)。
java">@Component  
@ConfigurationProperties(prefix = "test")  
public class ConfigProperties {  private String hostName;  private int port;  private String from;  // 标准的getter和setter方法  
}

在这个例子中,@ConfigurationProperties注解的prefix属性指定了配置文件中属性的前缀为"test",因此Spring会查找以"test"开头的属性,并将它们映射到ConfigProperties类的相应字段上。这里配置类上如果没有使用@Component等注解,则需要在主类上使用@EnableConfigurationProperties注解来绑定属性到POJO中。,如果使用了@Component等注解,那就不需要在主类上使用@EnableConfigurationProperties注解来绑定属性到POJO中,直接使用 @Autowired这类注解直接注入就好

在配置文件中定义属性

  • 在application.properties或application.yml文件中定义与配置类字段相匹配的属性。
# application.properties  
test.hostname=mailer@mail.com  
test.port=9000  
test.from=mailer@mail.com

使用配置类

  • 配置类可以通过依赖注入的方式在应用程序的其他部分中使用。
java">@RestController  
@RequestMapping(value = "/config")  
public class ConfigurationPropertiesController {  @Autowired  private ConfigProperties configProperties;  @GetMapping("/test")  public Map<String, Object> test() {  Map<String, Object> map = new HashMap<>();  map.put("hostName", configProperties.getHostName());  map.put("port", configProperties.getPort());  map.put("from", configProperties.getFrom());  return map;  }  
}

高级用法

与@ConfigurationPropertiesScan注解一起使用:
  • 从Spring Boot 2.2开始,Spring通过类路径扫描查找并注册@ConfigurationProperties类。因此,在某些情况下,不需要使用@Component注解来注释此类类,甚至不需要使用@EnableConfigurationProperties注解。
  • 可以使用@ConfigurationPropertiesScan注解来扫描配置属性类的自定义位置。
java">@SpringBootApplication  
@ConfigurationPropertiesScan("com.xxx.configurationproperties")  
public class EnableConfigurationDemoApplication {  public static void main(String[] args) {  SpringApplication.run(EnableConfigurationDemoApplication.class, args);  }  
}

嵌套属性

  • @ConfigurationProperties注解支持嵌套属性,可以在配置类中定义List、Map和类作为字段。
java">@Configuration  
@ConfigurationProperties(prefix = "test")  
public class ConfigProperties {  private String host;  private int port;  private String from;  private List<String> defaultRecipients;  private Map<String, String> additionalHeaders;  private Credentials credentials;  // 标准的getter和setter方法  
}  public class Credentials {  private String authMethod;  private String username;  private String password;  // 标准的getter和setter方法  
}

配置文件内容需要相应地设置所有字段:

# Simple properties  
test.hostname=mailer@mail.com  
test.port=9000  
test.from=mailer@mail.com  
# List properties  
test.defaultRecipients[0]=admin@mail.com  
test.defaultRecipients[1]=owner@mail.com  
# Map Properties  
test.additionalHeaders.redelivery=true  
test.additionalHeaders.secure=true  
# Object properties  
test.credentials.username=john  
test.credentials.password=password  
test.credentials.authMethod=SHA1

使用@ConfigurationProperties作用在@Bean注释的方法上

  • 这种方式非常适合绑定到无法控制的第三方组件上。
java">@Configuration  
public class ConfigProperties {  @Bean  @ConfigurationProperties(prefix = "item")  public Item item() {  return new Item();  }  
}

注意事项

属性名称的一致性:
  • 字段名称和属性名称之间需要保持一致性,考虑下划线和驼峰命名的转换。
配置类的注册:
  • 如果配置类上没有使用@Component等注解,则需要在主类上使用@EnableConfigurationProperties注解来绑定属性到POJO中。
动态更新:
  • @ConfigurationProperties支持配置的动态更新,但在某些情况下,频繁的配置更新可能会带来性能开销。

综上所述,@ConfigurationProperties注解在Spring Boot中提供了一种灵活且动态的方式来管理配置属性,通过简单的注解和配置文件即可实现配置的绑定和更新。


http://www.ppmy.cn/embedded/125630.html

相关文章

机器学习:关联规则:Apriori算法、FP - Growth算法的原理、应用场景及优缺点介绍

一、关联规则算法概述 关联规则挖掘是数据挖掘中的一个重要任务&#xff0c;用于发现数据集中不同项之间的关联关系。 二、Apriori算法 原理 频繁项集生成&#xff1a;Apriori算法基于一个先验原理&#xff0c;即如果一个项集是频繁的&#xff0c;那么它的所有子集也是频繁的…

springboot 模版集成方案(第二章)

springboot 模版集成方案 jsp 模版集成 ​ 在SpringBoot框架中默认模板推荐使用Thymeleaf模板,但是也不能排除有些公司还是使用jsp 模版解析&#xff1b; 1. 引入jsp 集成的 jar 包 <!--c标签库--> <dependency><groupId>jstl</groupId><artifact…

力扣10.9

3171. 找到按位或最接近 K 的子数组 给你一个数组 nums 和一个整数 k 。你需要找到 nums 的一个 子数组 &#xff0c;满足子数组中所有元素按位或运算 OR 的值与 k 的 绝对差 尽可能 小 。换言之&#xff0c;你需要选择一个子数组 nums[l..r] 满足 |k - (nums[l] OR nums[l 1…

PclSharp1.12.0库文件下载地址

C#Winfrom实现3D点云目标识别 使用PclSharp1.12.0库及PlcSharp可视化库&#xff0c;利用Winform框架开发点云算法处理应用程序&#xff0c;可适配激光雷达点云数据或者是3D相机拍摄扫描的点云数据&#xff0c;定位识别目标物体&#xff0c;得出抓取中心&#xff0c;通过数据通信…

jmeter学习(4)提取器

同线程组https://blog.csdn.net/vikeyyyy/article/details/80437530 不同线程组 在JMeter中&#xff0c;正则表达式提取的参数可以跨线程组使用。 通过使用Beanshell后置处理器和属性设置函数&#xff0c;可以将提取的参数设置为全局变量&#xff0c;从而在多个线程组之间共享…

ai智能电话机器人的核心技术有哪些?

ai智能电话机器人是一种高智能语音系统&#xff0c;它能够非常智能化的和用户进行畅通的交流&#xff0c;而不会存在任何的障碍问题&#xff0c;这个主要是由于它使用了很多的核心技术&#xff0c;我们一起来看看有哪些核心技术。 1.VAD 准确定位语音的开始点和结束点&#x…

模拟实现字符函数和字符串函数(一)

目录 一、模拟实现strlen 二、模拟实现strcpy 三、模拟实现strcmp 四、模拟实现strcat 五、模拟实现strstr 模拟实现strlen模拟实现strcpy模拟实现strcmp模拟实现strcat模拟实现strstr 一、模拟实现strlen strlen函数是用来求字符串长度的函数 #include <stdio.h>…

输出平方矩阵

题目&#xff1a; 输入一个正整数n&#xff0c;输出一个n阶的平方矩阵。 例如&#xff1a; 输入&#xff1a;5 输出&#xff1a; 1 4 9 16 25 4 9 16 25 1 9 16 25 1 4 16 25 1 4 9 25 1 4 9 16 解题思路&#xff1a; 本题我分别采用一维数组和二维数组来实现。 一…