In the use of spring boot, you can assign a value to a class by @configurationproperties and @Value two annotations, but two are used differently, and the following is an official description and an introduction to your own use.
1. According to their comparison, you can simply understand:
2. Actual operating Instructions
2.1Relaxed binding Untied
A.YML configuration file
#自定义 - 数据源配置 myconfig.dbconfig: needUpdateScheme: true dataUpdateType: prod #数据初始化类型 prod:只更新prod类型的init数据,test:更新所有类型的数据 databases: db0: url: jdbc:mysql://172.168.0.31:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=utf-8 username: xxxx password: xxxx
B repositoryproperties class is the object class of the configuration file, Databaseproperty is the object class of databases
@ConfigurationProperties (prefix = "Myconfig.dbconfig") public class Repositoryproperties {//whether the database structure needs to be updated Private String Needupdatescheme = "false"; Data source private map<string, databaseproperty> databases = new hashmap<> (); Data initialization type PROD: Update only the init data of the PROD type, test: Update all types of data private String Dataupdatetype = "prod"; Public String Getdataupdatetype () {return dataupdatetype; } public map<string, Databaseproperty> getdatabases () {return databases; } public String Getneedupdatescheme () {return needupdatescheme; } public void Setneedupdatescheme (String needupdatescheme) {this.needupdatescheme = Needupdatescheme; } public void Setdatabases (map<string, databaseproperty> databases) {this.databases = databases; } public void Setdataupdatetype (String dataupdatetype) {this.dataupdatetype = Dataupdatetype; }}
public class Databaseproperty {//connection string @NotNull//---private String url; Database account private String username; Password private String password; Public String GetUrl () {return URL; } public void SetUrl (String url) {this.url = URL; } public String GetUserName () {return username; } public void Setusername (String username) {this.username = username; } public String GetPassword () {return password; } public void SetPassword (String password) {this.password = password; }}
2.2 Meta-data metadata, the Spring Boot jar contains a metadata file that provides detailed information on all supported configuration properties. These files are designed to allow IDE developers to provide contextual help and "code completion" when users use Application.properties or application.yml files. To improve the user experience and further assist the user in configuring the given properties, you can provide additional metadata, create Meta-inf folders under Resource, Then set up file Additional-spring-configuration-metadata.json, format specification reference Http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE /reference/htmlsingle/#configuration-metadata. "can refer to 70342023, this way is not further in-depth, interested children shoes can try to do a custom Meta-data" 2.3 Spring Expression Language (spel)
public class RepositoryProperties { //是否需要更新数据库结构 @Value("${myconfig.dbconfig.needUpdateScheme}") private String needUpdateScheme = "false"; ......
2.4 @ConfigurationProperties also supports JSR303 for configuration file values and validation
@ConfigurationProperties(prefix = "myconfig.dbconfig")public class RepositoryProperties { @NotNull //JSR303进行配置文件值及校验 private String needUpdateScheme = "false"; .... }
Spring Boot @ConfigurationProperties vs @Value