SpringBoot中@ConfigurationProperties实现配置自动绑定的方法
代码
pom.xml
:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> </parent> <packaging>jar</packaging> <groupId>com.kaven</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>springboot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置类:
package com.kaven.springboot.config; import lombok.Setter; import lombok.ToString; import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.List; import java.util.Map; import java.util.Set; @ConfigurationProperties(prefix = "user") @Setter @ToString public class UserProperties { private String username; private String password; private Set<String> hobbies; private Map<String, Integer> scores; private List<UserToken> userToken; }
UserToken
类:
package com.kaven.springboot.config; import lombok.Setter; import lombok.ToString; @Setter @ToString public class UserToken { private String token; }
接口:
package com.kaven.springboot.controller; import com.kaven.springboot.config.UserProperties; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class ConfigController { @Resource private UserProperties userProperties; @GetMapping("/config") public String getConfig() { return userProperties.toString(); } }
application.properties
:
user.username="kaven" user.password="itkaven" user.hobbies[0]="A" user.hobbies[1]="B" user.hobbies[2]="C" user.scores.mathematics=145 user.scores.english=80 user.user-token[0].token="A" user.user-token[1].token="B" user.user-token[2].token="C"
启动类:
package com.kaven.springboot; import com.kaven.springboot.config.UserProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties(value = {UserProperties.class}) //@ConfigurationPropertiesScan(basePackageClasses = {UserProperties.class}) public class SpringbootApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(SpringbootApplication.class); application.run(args); } }
下面这两个注解都可以使得Spring Boot
基于被@ConfigurationProperties
注解修饰的类创建bean
,因此UserProperties
实例可以自动注入到控制器中。
@EnableConfigurationProperties(value = {UserProperties.class}) @ConfigurationPropertiesScan(basePackageClasses = {UserProperties.class})
而@ConfigurationPropertiesScan
注解还可以指定要被扫描的包数组。
@ConfigurationPropertiesScan(basePackages = {"com.kaven.springboot.config"})
启动应用,访问http://localhost:8080/config
。
效果符合预期。
构造器绑定
Spring Boot
将配置文件中的配置自动绑定到配置类,无非就是通过反射等手段,创建配置类实例,而配置项需要绑定到配置类实例的属性,这一般通过属性的set
方法或者构造器来实现,上面的演示是通过set
方法来进行绑定,这是@Setter
注解的效果。
@Setter
如果需要通过构造器将配置项绑定到配置类实例的属性,可以使用@ConstructorBinding
注解。
package com.kaven.springboot.config; import lombok.ToString; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConstructorBinding; import java.util.List; import java.util.Map; import java.util.Set; @ConfigurationProperties(prefix = "user") @ToString @ConstructorBinding public class UserProperties { private String username; private String password; private Set<String> hobbies; private Map<String, Integer> scores; private List<UserToken> userToken; public UserProperties(String username, String password, Set<String> hobbies, Map<String, Integer> scores, List<UserToken> userToken) { this.username = username; this.password = password; this.hobbies = hobbies; this.scores = scores; this.userToken = userToken; } }
使用@ConstructorBinding
注解修饰类的问题在于类中可能有多个构造器,如果出现这种情况就会有问题。
package com.kaven.springboot.config;import lombok.ToString;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.ConstructorBinding;import java.util.List;import java.util.Map;import java.util.Set;@ConfigurationProperties(prefix = "user")@ToString@ConstructorBindingpublic class UserProperties {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E--> private String username; private String password; private Set<String> hobbies; private Map<String, Integer> scores; private List<UserToken> userToken; public UserProperties() {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->} public UserProperties(String username, String password, Set<String> hobbies, Map<String, Integer> scores, List<UserToken> userToken) {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E--> this.username = username; this.password = password; this.hobbies = hobbies; this.scores = scores; this.userToken = userToken; }}
因为Spring Boot
不知道调用哪个构造器。
可以将@ConstructorBinding
注解修饰在构造器上。
package com.kaven.springboot.config; import lombok.ToString; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConstructorBinding; import java.util.List; import java.util.Map; import java.util.Set; @ConfigurationProperties(prefix = "user") @ToString public class UserProperties { private String username; private String password; private Set<String> hobbies; private Map<String, Integer> scores; private List<UserToken> userToken; public UserProperties() {} @ConstructorBinding public UserProperties(String username, String password, Set<String> hobbies, Map<String, Integer> scores, List<UserToken> userToken) { this.username = username; this.password = password; this.hobbies = hobbies; this.scores = scores; this.userToken = userToken; } }
结合@PropertySource
SourceConfig
类:
package com.kaven.springboot.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource(value = "classpath:/static/user.properties") public class SourceConfig {}
效果符合预期,@ConfigurationProperties
实现配置自动绑定就介绍到这里,,更多相关SpringBoot @ConfigurationProperties 自动绑定内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
原文出处:https://blog.csdn.net/qq_37960603/article/details/122951157
相关文章
解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题
这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- 这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
- 这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
- 这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
- 这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
Spring Boot项目@RestController使用重定向redirect方式
这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02- 这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
springboot中使用@Transactional注解事物不生效的坑
这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26- 这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
详解SpringBoot之访问静态资源(webapp...)
这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14- 这篇文章主要介绍了springboot多模块包扫描问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解
这篇文章主要介绍了Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-18- 这篇文章主要介绍了Springboot实现多线程注入bean的工具类操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-27
Springboot+MDC+traceId日志中打印唯一traceId
本文主要介绍了Springboot+MDC+traceId日志中打印唯一traceId,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-17SpringBoot部署到Linux读取resources下的文件及遇到的坑
本文主要给大家介绍SpringBoot部署到Linux读取resources下的文件,在平时业务开发过程中,很多朋友在获取到文件内容乱码或者文件读取不到的问题,今天给大家分享小编遇到的坑及处理方案,感兴趣的朋友跟随小编一起看看吧...2021-06-21- 这篇文章主要介绍了springboot中nacos动态路由的配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-11
SpringBoot高版本修改为低版本时测试类报错的解决方案
这篇文章主要介绍了SpringBoot高版本修改为低版本时测试类报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-18解决Springboot整合shiro时静态资源被拦截的问题
这篇文章主要介绍了解决Springboot整合shiro时静态资源被拦截的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-26springboot配置多数据源后mybatis拦截器失效的解决
这篇文章主要介绍了springboot配置多数据源后mybatis拦截器失效的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-23