Spring Boot(四)Spring Boot @ConfigurationProperties实例

Spring Boot @ConfigurationProperties实例

一 、 ConfigurationProperties的使用

  1. 通常,我们使用@Value注释来逐个注入.properties值,这对于小而简单的结构.properties文件很有用。

    文件:global.properties
    
    [email protected]
    thread-pool=12
    
  2. 使用@Value

    @Component
    @PropertySource("classpath:global.properties")
    public class GlobalProperties {
    
        @Value("${thread-pool}")
        private int threadPool;
    
        @Value("${email}")
        private String email;
    
        //getters and setters
    
    }
    
  3. 使用@ConfigurationProperties

    @Component
    @PropertySource("classpath:global.properties")
    @ConfigurationProperties
    public class GlobalProperties {
    
        private int threadPool;
        private String email;
    
        //getters and setters
    
    }
    

猜你喜欢

转载自blog.csdn.net/chou_out_man/article/details/80005789