Spring Boot @ConfigurationProperties vs @Value

Source: Internet
Author: User

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:
    • 1 untie the set:

      @ConfigurationProperties the use of each word must not be consistent with the key in the configuration, @Value have to be consistent "can be understood as @configurationproperties can be bulk injection of the configuration file properties, @ Value can only be specified "

    • 2 Data source Support:

      The @ConfigurationProperties also supports spring metadata, and you can create Meta-inf folders under Resource. Then create the file Additional-spring-configuration-metadata.json. The data format must meet the spring metadata format http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#configuration-metadata

    • 3 Spring Expression Language (spel):

      @Value support Sqel, simple understanding must be a single parameter assignment, that is, strong binding.

    • 4 @ConfigurationProperties also support JSR303 for configuration file values and validation "personal new, not reflected in the official website"
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.