4
头图

[toc]

Spring - read configuration

Overview

In Spring, dynamic configuration can be placed in the configuration center or profile file, and there are many ways to read the configuration. I commonly use the following two:

  • The configuration is read through the @Value("${property-name}") annotation. The advantage of this method is that it is simple and direct. Which configuration needs to be read, just add a member variable to the required class and add this annotation. The disadvantage is that Add a member variable to each class that needs to use this configuration. Of course, the @Value annotation also supports some minor operations through EL expressions, and the function is still relatively powerful.
  • The configuration class is read through the @ConfigurationProperties annotation. In this way, the configuration is converted into an entity class. If other classes want to use it, it can be used directly by automatic injection.

Read configuration via @Value annotation

Configuration in configuration file or configuration center
 user:
  name: k
Read and apply configuration
 // 读取配置
@Value("${user.name}")
private String userName;

public void func() {
    // 使用
    System.out.println(userName);
}

Read configuration via @ConfigurationProperties configuration class

Configuration in configuration file or configuration center
 prop:
  user: "k"
  password: "***"
  owns:
    - name: "apple"
      size: 20
    - name: "orange"
      size: 10
configuration class
 import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import lombok.Data;

/**
 * 用户配置类:从配置文件中读取配置方便使用
 */
@Data
@Component
@ConfigurationProperties(prefix = "prop")
public class SpringConfig {
    private String user;

    private String password;

    private List<Owns> owns;

    @Data
    private static class Owns {
        private String name;

        private Integer size;
    }
}
Using a configuration class: Simple example, get the bean of the configuration class, then read the configuration. The configuration can also be read by means of automatic injection.
 /**
     * 根据类名读取配置
     * 例如:"com.kaesar.getup.config.SpringConfig"
     * 
     * @param propertyClassName
     * @return
     * @throws ClassNotFoundException
     */
    @ApiOperation(value = "读取配置", tags = {"根据配置名读取配置"})
    @GetMapping("readProperties/{propertyClassName}")
    public String readProperties(@ApiParam(name = "propertyClassName", value = "配置key", required = true) @PathVariable String propertyClassName) throws ClassNotFoundException {
        Map<String, ?> propertyClass = applicationContext.getBeansOfType(Class.forName(propertyClassName));

        return propertyClass.toString();
    }

Summarize

Currently, Spring also has some other ways to read configuration, such as reading system parameters, reading through configuration files, etc. The specific method used depends on which one is more convenient.

$1.01^{365} ≈ 37.7834343329$
$0.99^{365} ≈ 0.02551796445$
Believe in the power of persistence!


醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!