在Spring Boot项目中从配置文件读取配置信息到类中使用通常有两种方式:@ConfigurationProperties 和 @Value。本文主要学习@ConfigurationProperties的使用,我把它们归结为以下三种使用方式。

第一种方式

使用注解@Component 与@ConfigurationProperties组合把配置信息自动封装成实体类,在@Service@Componet@Controller@RestController等注解的类中使用。

  • 配置文件

    1
    2
    3
    4
    5
    6
    7
    
    oauth:
    token:
        access-validity-seconds: 7200     # 2 hours
        refresh-validity-seconds: 604800   # 7 days
        clientId: ${oauth.client.clientId}
        clientSecret: ${oauth.client.clientSecret}
    appCode: 2
  • 配置信息自动封装成实体类

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    @Data
    @Component
    @ConfigurationProperties(prefix = "oauth")
    public class OauthProperties {
    private TokenProperties token;
    private String appCode;
    
    }
    
    @Data
    @ConfigurationProperties(prefix = "oauth.token")
    @Component
    public class TokenProperties {
    private Integer accessValiditySeconds;
    private Integer refreshValiditySeconds;
    private String clientId;
    private String clientSecret;
    
    }
  • 实体使用

    1
    2
    3
    4
    5
    6
    7
    8
    
    @RestController
    public class AuthController {
    	@Autowired
    private WtcpOauthProperties wtcpOauthProperties;
    
    
    
    }

第二种方式

使用@ConfigurationProperties把配置信息自动封装成实体类,@Configuration与@EnableConfigurationProperties组合使用实体。

  • 配置文件

    1
    2
    3
    4
    5
    6
    
    oauth:
    token:
        access-validity-seconds: 7200     # 2 hours
        refresh-validity-seconds: 604800   # 7 days
        clientId: ${oauth.client.clientId}
        clientSecret: ${oauth.client.clientSecret}
  • 配置信息自动封装成实体类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    @Data
    @ConfigurationProperties(prefix = "oauth.token")
    public class TokenProperties {
    private Integer accessValiditySeconds;
    private Integer refreshValiditySeconds;
    private String clientId;
    private String clientSecret;
    
    }
  • 实体使用

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    @Configuration
    @EnableConfigurationProperties({TokenProperties.class})
    public class Oauth2Config {
    
    @Autowired
    private TokenProperties tokenProperties;
    
    
    
    }

第三种方式

使用@Bean与@ConfigurationProperties把配置信息自动封装成实体类,在@Service@Componet@Controller@RestController等注解的类中使用。

  • 配置文件

    1
    2
    3
    4
    
    connection:
    username: admin
    password: kyjufskifas2jsfs
    remoteAddress: 192.168.1.1
  • 配置信息自动封装成实体类

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    @Data
    public class ConnectionSettings {
    private String username;
    private String remoteAddress;
    private String password ;
    }
     
    @SpringBootApplication
    public class DemoApplication{
    
    @Bean
    @ConfigurationProperties(prefix = "connection")
    public ConnectionSettings connectionSettings(){
        return new ConnectionSettings();
    
    }
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    }
  • 实体使用

    1
    2
    3
    4
    5
    6
    7
    8
    
    @RestController
    public class DcController {
    		@Autowired
    private ConnectionSettings connectionSettings;
    
    
    
    }