SpringBoot中@ConfigurationProperties實(shí)現(xiàn)配置自動(dòng)綁定的方法
代碼
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>
配置類(lèi):
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類(lèi):
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"
啟動(dòng)類(lèi):
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);
}
}
下面這兩個(gè)注解都可以使得Spring Boot基于被@ConfigurationProperties注解修飾的類(lèi)創(chuàng)建bean,因此UserProperties實(shí)例可以自動(dòng)注入到控制器中。
@EnableConfigurationProperties(value = {UserProperties.class})
@ConfigurationPropertiesScan(basePackageClasses = {UserProperties.class})
而@ConfigurationPropertiesScan注解還可以指定要被掃描的包數(shù)組。
@ConfigurationPropertiesScan(basePackages = {"com.kaven.springboot.config"})
啟動(dòng)應(yīng)用,訪(fǎng)問(wèn)http://localhost:8080/config。

效果符合預(yù)期。
構(gòu)造器綁定
Spring Boot將配置文件中的配置自動(dòng)綁定到配置類(lèi),無(wú)非就是通過(guò)反射等手段,創(chuàng)建配置類(lèi)實(shí)例,而配置項(xiàng)需要綁定到配置類(lèi)實(shí)例的屬性,這一般通過(guò)屬性的set方法或者構(gòu)造器來(lái)實(shí)現(xiàn),上面的演示是通過(guò)set方法來(lái)進(jìn)行綁定,這是@Setter注解的效果。
@Setter
如果需要通過(guò)構(gòu)造器將配置項(xiàng)綁定到配置類(lèi)實(shí)例的屬性,可以使用@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注解修飾類(lèi)的問(wèn)題在于類(lèi)中可能有多個(gè)構(gòu)造器,如果出現(xiàn)這種情況就會(huì)有問(wèn)題。
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; }}因?yàn)?code>Spring Boot不知道調(diào)用哪個(gè)構(gòu)造器。

可以將@ConstructorBinding注解修飾在構(gòu)造器上。
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;
}
}

結(jié)合@PropertySource
SourceConfig類(lèi):
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 {}


效果符合預(yù)期,@ConfigurationProperties實(shí)現(xiàn)配置自動(dòng)綁定就介紹到這里,,更多相關(guān)SpringBoot @ConfigurationProperties 自動(dòng)綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC深入講解文件的上傳下載實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了springMVC實(shí)現(xiàn)文件上傳和下載的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
idea創(chuàng)建springboot項(xiàng)目和springcloud項(xiàng)目的詳細(xì)教程
這篇文章主要介紹了idea創(chuàng)建springboot項(xiàng)目和springcloud項(xiàng)目方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
java基本教程之多線(xiàn)程基本概念 java多線(xiàn)程教程
多線(xiàn)程是Java中不可避免的一個(gè)重要主體。下面是對(duì)“JDK中新增JUC包”之前的Java多線(xiàn)程內(nèi)容的講解,JUC包是由Java大師Doug Lea完成并在JDK1.5版本添加到Java中的2014-01-01
springboot手動(dòng)事務(wù)回滾的實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot手動(dòng)事務(wù)回滾的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Java8日期類(lèi)LocalDate、LocalTime和LocalDateTime使用方法詳解
這篇文章主要給大家介紹了關(guān)于Java8日期類(lèi)LocalDate、LocalTime和LocalDateTime使用方法的相關(guān)資料,LocalDateTime是JDK1.8出現(xiàn)的新特性,解決線(xiàn)程不安全的問(wèn)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
SpringBoot實(shí)現(xiàn)嵌入式 Servlet容器
傳統(tǒng)的Spring MVC工程部署時(shí)需要將WAR文件放置在servlet容器的文檔目錄內(nèi),而Spring Boot工程使用嵌入式servlet容器省去了這一步驟,本文就來(lái)設(shè)置一下相關(guān)配置,感興趣的可以了解一下2023-12-12
SpringBoot多種自定義錯(cuò)誤頁(yè)面方式小結(jié)
這篇文章主要介紹了SpringBoot多種自定義錯(cuò)誤頁(yè)面方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
springboot 多數(shù)據(jù)源配置不生效遇到的坑及解決
這篇文章主要介紹了springboot 多數(shù)據(jù)源配置不生效遇到的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
JavaWeb 文件的上傳和下載功能簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了JavaWeb 文件的上傳和下載功能簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-04-04

