SpringBoot中@ConfigurationProperties实现配置自动绑定的方法
目录
- 代码
- 构造器绑定
- 结合@PropertySource
代码
pom.xml
:4.0.0 org.springframework.boot spring-boot-starter-parent2.6.2 jarcom.kaven springboot0.0.1-SNAPSHOT springboot springboot 1.8 org.springframework.boot spring-boot-starter-weborg.projectlombok lombokorg.springframework.boot spring-boot-maven-plugin
配置类:
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@ToStringpublic class UserProperties {private String username; private String password; private Set hobbies; private Map scores; private ListuserToken; }
UserToken
类:package com.kaven.springboot.config; import lombok.Setter; import lombok.ToString; @Setter@ToStringpublic 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; @RestControllerpublic class ConfigController {@Resourceprivate 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=145user.scores.english=80user.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 = https://www.it610.com/article/{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 = https://www.it610.com/article/{UserProperties.class})@ConfigurationPropertiesScan(basePackageClasses = {UserProperties.class})
而
@ConfigurationPropertiesScan
注解还可以指定要被扫描的包数组。@ConfigurationPropertiesScan(basePackages = {"com.kaven.springboot.config"})
启动应用,访问
http://localhost:8080/config
。文章图片
效果符合预期。
【SpringBoot中@ConfigurationProperties实现配置自动绑定的方法】
构造器绑定
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@ConstructorBindingpublic class UserProperties {private String username; private String password; private Set hobbies; private Map scores; private ListuserToken; public UserProperties(String username,String password,Set hobbies,Map scores,List 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 { private String username; private String password; private Set hobbies; private Map scores; private ListuserToken; public UserProperties() {} public UserProperties(String username, String password, Set hobbies, Map scores, List userToken) { 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")@ToStringpublic class UserProperties {private String username; private String password; private Set hobbies; private Map scores; private ListuserToken; public UserProperties() {}@ConstructorBindingpublic UserProperties(String username,String password,Set hobbies,Map scores,List 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 = "https://www.it610.com/article/classpath:/static/user.properties")public class SourceConfig {}
文章图片
文章图片
效果符合预期,
@ConfigurationProperties
实现配置自动绑定就介绍到这里,,更多相关SpringBoot @ConfigurationProperties 自动绑定内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!推荐阅读
- SpringBoot实现发送邮件、发送微信公众号推送功能
- Springboot从配置文件properties读取字符串乱码的解决
- 如何在|如何在 K3s 中启用 Traefik Dashborad
- Navicat Premium Mac/win(多连接数据库管理工具)v16.0.9中文版
- Sketch 84 mac(专业矢量绘图设计软件)中文版
- 如何在Spark Scala/Java应用中调用Python脚本
- 关于数据中台最常见的5个错误认知
- 将本地SpringBoot项目发布到云服务器的方法
- 谭中意(玩魔兽、爱做饭、看毛选,而我是个开源人)
- linux|linux vs 安装程序,在Linux中安装VSCodium(100%开源的Microsoft VS Code)