SpringBoot configuration class-use ConfigurationProperties to dynamically obtain configuration file information

Spring configuration class-use ConfigurationProperties to dynamically obtain configuration file information

Usage scenario: Sometimes it is necessary to dynamically obtain the custom value in the configuration file properties in the configuration class Configuration

Original method: read using ClassLoader

Disadvantages: This method can only read the configuration file under the classpath

**Case:** Create a thread pool with custom parameters, the parameters are obtained from the configuration file

@Configuration
public class MyThreadConfig {
    
    
    @Bean
    public ThreadPoolExecutor threadPool(){
    
    
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = MyThreadConfig.class.getClassLoader().getResourceAsStream("config.properties");
        // 使用properties对象加载输入流
        try {
    
    
            properties.load(in);
            return new ThreadPoolExecutor(new Integer(properties.getProperty("coreSize")),
                    new Integer(properties.getProperty("maxSize")),
                    new Long(properties.getProperty("keepAliveTime")),
                    TimeUnit.SECONDS,
                    new LinkedBlockingDeque<>(100000),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.AbortPolicy());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        return null;
    }
}

Use ConfigurationProperties

Import auxiliary dependencies

This dependency will only generate a prompt when encoding, and it can be imported without import

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

Write configuration parameter class ThreadPoolConfigPorperties

  • Use @ConfigurationProperties to bind the custom configuration in application.properties
  • prefix defines which external properties will be bound to the fields of the class
  • @Component adds the object to the spring container to facilitate the use of the Config class
  • The fields of the class must have public setter methods
//绑定配置文件中与gulimail.thread 有关的配置
@ConfigurationProperties(prefix ="gulimail.thread" )
@Component
@Data
public class ThreadPoolConfigProperties {
    
    
    private Integer coreSize;
    private Integer maxSize;
    private Integer keepAliveTime;
}

Write configuration class MyThreadConfig

  • Using ThreadPoolConfigProperties as method parameters, Spring will automatically rely on injection objects
  • Call the get method to get the value
@Configuration
public class MyThreadConfig {
    
    
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties properties){
    
    
        return new ThreadPoolExecutor(properties.getCoreSize(),
                properties.getMaxSize(),
                properties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}

Guess you like

Origin blog.csdn.net/weixin_44634197/article/details/108376950