Detailed explanation of N methods for SpringBoot to read configuration files

  • 2021-08-17 00:07:12
  • OfStack

We often use configuration information in project development, such as database connection account, password, etc., and in order to facilitate maintenance, we usually put these information into configuration files. When this configuration information is needed, it can be obtained through code. Let's take a look at the methods for obtaining configuration information in Spring.

PropertiesLoaderUtils Read

Load configuration file resources through ClassPathResource and read them in combination with PropertiesLoaderUtils class. The source code is as follows:


ClassPathResource resource = new ClassPathResource("application.properties");  
try {    
 Properties properties = PropertiesLoaderUtils.loadProperties(resource);       String account = properties.getProperty("jdbc.account");        
} catch (IOException e) {    
  ...  
}

@ Value annotation reads the specified property

Configuration information


jdbc:
 account: zhangsan
 pwd: 123456

Read method


@Component
public class JdbcService {
  @Value("${jdbc.account}")
  private String account;
  @Value("${jdbc.pwd}")
  private String pwd;

  public void connectDb() {
    System.out.println("Database has connected, jdbc account is "
      + account + ", password is " + pwd);
  }
}

The @ Value annotation obtains the value value corresponding to key in the configuration file through ${key}, assigns the value value to the corresponding variable, and then obtains the configuration information just like using normal variable 1.

@ ConfigurationProperties reads 1 set of configuration information

The @ ConfigurationProperties annotation is used to read a set of configuration information with the specified prefix and bind it to bean. The specific configuration attribute will be bound to the member attribute of bean, that is, the prefix name + member attribute name is equal to key in the configuration file. You can then use the bean and read the configuration information just like other bean1.

Configuration information


user:
 name: zhangsan
 sex:  Male 
 homeUrl: www.xxx.com

Bind bean


@Component
@Data
@ConfigurationProperties(prefix = "user")
public class User {
  private String name;
  private String sex;
  private String homeUrl;
}

Using bean


@Service
public class UserService {
  @Autowired
  User user;

  public void getUserInfo() {
    System.out.println(user.toString());
  }
}

@ Value is suitable for scenarios with a small amount of configuration information. In one more complex scenario (cumbersome business and many configuration items), it is necessary to consider encapsulating one set of configuration information into one or more configuration information classes, and then @ ConfigurationProperties can be used.

@ PropertySource reads the specified configuration file

When our project is large and has more configuration information, if all the configuration information is placed in one configuration file, it will appear bloated and difficult to understand and maintain. At this point, we can split the configuration file as needed and use the @ PropertySource annotation with @ Value or @ ConfigurationProperties to read the configuration information in the specified configuration file. Assume that the configuration file where we store data connection information is jdbc. properties, which reads as follows:


jdbc:
 account: zhangsan
 pwd: 123456

@Component
@Data
@PropertySource(value = {"classpath:jdbc.properties"})
@ConfigurationProperties(prefix = "jdbc")
public class JdbcCfg {
  private String account;
  private String pwd;

  public void connectDb() {
    System.out.println("Database has connected, jdbc account is "
      + account + ", password is " + pwd);
  }
}

It can also be used with @ Value.


@Component
@PropertySource(value = {"classpath:jdbc.properties"})
public class JdbcCfg {
 @Value("${jdbc.account}")
  private String account;
  @Value("${jdbc.pwd}")
  private String pwd;
  
  public void connectDb() {
   System.out.println("Database has connected, jdbc account is "
     + account + ", password is " + pwd);
}

Specify the configuration file for the project

In actual development, we will have at least two environments-development environment and online environment, and there may be test environment, which often use different configuration information, such as port, database link address and so on. If we deploy the project to the online environment, it will be troublesome to change the configuration information item 1 to item 1 corresponding to the online environment. A more scientific solution is that the online environment corresponds to one set of configuration information, and the development environment corresponds to one set. When we are in the development environment, we specify the configuration of reading development, and when we are online, we specify the configuration of reading line.

Assume that the configuration file corresponding to the development environment is application-d. yml, and the configuration file corresponding to the online environment is application-d. yml. Then configure which configuration file to use in the application. yml file, such as online environment can be specified as follows.


spring:
 profiles:
  active:
  - p

Reading complex configuration information

Look at the following configuration file


jdbc:
 account: zhangsan
 pwd: 123456
0

The above configuration file is to support message transmission of different protocols, and there are variables in key of this configuration. How to read such configuration information?


jdbc:
 account: zhangsan
 pwd: 123456
1

The key point is private Map < String, MsgInfo > tranprocol; , http and fix are automatically mapped to key of tranprocol, and orderid and count are mapped to member attributes of MsgInfo.

If you use variables in key of the configuration file, you need to define map in the corresponding bean. The corresponding rules of the configuration file and map are as follows:


jdbc:
 account: zhangsan
 pwd: 123456
2

Related articles: