“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”


Related articles

Java with Notes: Java with Notes


  • To simplify reading configuration values from properties files, Spring supports the @Value annotation, which greatly simplifies project configuration and improves flexibility in the business.

    1. Two ways to use it
    • 1) @ Value (” # {configProperties [‘ key ‘]} “)
    • 2) @ Value (” ${key} “)
    1. Example configuration file
    • FTP: FTPLP: 10.2.23.89 ftpPort: 21 ftpUser: uftp ftpPwd: 12345678 ftpRemotePath: /homeCopy the code

Note: The above is the information in the configuration file, mainly some account passwords and other information.

  1. Utility class for reading yML configuration files
package com.dbright.dataprediction.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:ftpconfig.yml") @ConfigurationProperties(prefix = "ftp") public class FtpProperties { @Value("${ftplp}") public String ftplp; @Value("${ftpPort}") public String ftpPort; @Value("${ftpUser}") public String ftpUser; @Value("${ftpPwd}") public String ftpPwd; @Value("${ftpRemotePath}") public String ftpRemotePath; public String getFtplp() { return ftplp; } public void setFtplp(String ftplp) { this.ftplp = ftplp; } public String getFtpPort() { return ftpPort; } public void setFtpPort(String ftpPort) { this.ftpPort = ftpPort; } public String getFtpUser() { return ftpUser; } public void setFtpUser(String ftpUser) { this.ftpUser = ftpUser; } public String getFtpPwd() { return ftpPwd; } public void setFtpPwd(String ftpPwd) { this.ftpPwd = ftpPwd; } public String getFtpRemotePath() { return ftpRemotePath; } public void setFtpRemotePath(String ftpRemotePath) { this.ftpRemotePath = ftpRemotePath; }}Copy the code
  • Note: This is an example of code that uses the @value annotation to read a YML configuration file

    • 1) @component — instantiate a normal POJO into the Spring container, equivalent to a configuration file<bean id="" class=""/>
      1. @propertysource (“classpath:ftpconfig.yml”) — Sets the path of the YML file for easy scanning. We usually put our configuration files under Resources. So we only need the name of the configuration file that classpath+ needs to read.
    • 3) @configurationProperties (prefix = “FTP “) — this doesn’t need to be explained too much.
    • 4) @value (“${FTPLP}”) — this is the configuration information we need to read, dollar sign +{field name} can be specified
    • 5) The following defines a string to receive the read configuration information.
    • 6) Write set and GET methods to facilitate external class calls.
  1. Demo: The renderings are as follows
  • As you can see, we got the value we wanted.
  1. The second option is similar to this one: change the $outside {} to # and specify the information + field in the configuration file. Much the same, I won’t post the code up.

The above content can be optimized to dynamically read the contents of configuration files in different environments! The optimization content is as follows:

@Component @PropertySource("classpath:application-${spring.profiles.active}.yml") @ConfigurationProperties(prefix = "entid") public class AplicationUtils { @Value("${entName}") public String ftplp; public String getFtplp() { return ftplp; } public void setFtplp(String ftplp) { this.ftplp = ftplp; }}Copy the code
  • In real development, we typically have three environments:

    • The development environment
    • The test environment
    • Online environment.
    • Different environments may read different things, for example, the account and password are different, so we need to dynamically read the contents of the configuration file.

  • The configuration file with @propertysource (” CLASspath :application-${spring.profiles.active}.yml”) indicates that the currently selected environment is reading its configuration file information.

  • I’m too busy to write today. This is an article from a few years ago.


The road ahead is long, I see no end, I will search high and low

If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah