Spring Boot是一个非常方便的框架,可以快速地构建应用程序,同时,它也提供了很多配置文件以供我们使用。在本文中,我们将会详解如何使用Spring Boot的配置文件。
一、常规配置文件
Spring Boot中的常规配置文件只需要在src/main/resources目录下创建以application为前缀的配置文件即可,如下:
application.properties application.yml
其中application.properties是以键值对的形式组织的,如:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=root spring.datasource.password=root
而application.yml则是以缩进的方式组织的,如:
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/test
    username: root
    password: root
在这样的配置文件中,我们可以对各种配置项进行配置。其中spring代表要配置的模块,后面跟着的datasource代表数据源模块,而driver-class-name、url、username、password则是datasource模块下的具体配置项。
二、多环境配置
有时我们需要根据不同的环境来配置不同的配置项,比如开发环境、测试环境和生产环境等。这时就需要使用Spring Boot的多环境配置功能了。
在Spring Boot中,我们可以创建以application-{profile}.properties或application-{profile}.yml的文件来配置不同的环境。其中,{profile}代表不同的环境,如:
application-dev.properties # 开发环境 application-test.properties # 测试环境 application-prod.properties # 生产环境 application-dev.yml application-test.yml application-prod.yml
在application-test.yml中,可以这样配置数据源:
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/test
    username: root
    password: root
logging:
  level:
    root: debug
    org:
      springframework: debug
这样,在测试环境中,我们可以将日志级别设置成debug,以便更好地进行调试工作。
三、配置优先级
Spring Boot使用一种特定的顺序来加载配置文件,以应对多个配置来源的情况。具体优先级如下:
1.命令行参数
2.来自java:comp/env的JNDI属性
3.Java System属性(SpringApplication.setDefaultProperties)指定的系统属性
4.操作系统环境变量
5./WEB-INF/classes/application.properties或application.yml(应用程序根路径下)
6.来自@PropertySource注解的属性Source
7.默认属性(org.springframework.boot.autoconfigure.core.DefaultProperties)
即,命令行参数的优先级最高,而默认属性的优先级最低。
四、使用@ConfigurationProperties
Spring Boot中的@ConfigurationProperties注解可以将配置文件中的属性映射到Java类中的属性上。需要注意的是,这个类必须被@Bean注解,才能保证Spring Boot将会把这个类的属性注入到Bean中。
以读取数据源配置为例,我们可以这样创建一个DataSourceConfig类:
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceConfig {
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    // getters and setters
}
然后我们就可以在其他需要用到数据源的地方将DataSourceConfig注入进去,如:
@RestController
public class UserController {
    @Autowired
    private DataSourceConfig dataSourceConfig;
    @GetMapping("/user")
    public String getUser() {
        // 使用dataSourceConfig获取数据源相关信息
        return "user info";
    }
}
五、使用@Value注解
除了@ConfigurationProperties注解外,我们还可以使用@Value注解来获取配置文件中的属性值。需要注意的是,使用@Value注解只能读取单个属性值。
以读取日志级别配置为例,我们可以这样创建一个LoggingConfig类:
@Component
public class LoggingConfig {
    @Value("${logging.level.root}")
    private String rootLogLevel;
    @Value("${logging.level.org.springframework}")
    private String springLogLevel;
    // getters and setters
}
然后我们就可以在其他需要用到日志级别的地方将LoggingConfig注入进去,如:
@RestController
public class UserController {
    @Autowired
    private LoggingConfig loggingConfig;
    @GetMapping("/user")
    public String getUser() {
        // 使用loggingConfig获取日志级别相关信息
        return "user info";
    }
}
六、使用PropertySources
有时,我们可能需要使用多个配置文件,或者使用其他文件来配置应用程序。这时,我们可以使用Spring Boot的PropertySources来读取这些属性。
以读取外部配置文件为例,我们可以这样创建一个ExternalConfig类:
@Configuration
@PropertySources({
        @PropertySource("file:/path/to/config.properties"),
        @PropertySource(value = "classpath:default.properties", ignoreResourceNotFound = true)
})
public class ExternalConfig {
    @Value("${external.property}")
    private String externalProperty;
    // getters and setters
}
在这个配置文件中,我们用@PropertySources注解指定了读取外部配置文件的路径,同时还可以指定一个默认的配置文件,当外部配置文件不存在时,将会使用默认的配置文件。
七、结语
Spring Boot的配置文件功能非常强大,可以很好地满足我们的各种需求。本文介绍了Spring Boot的常规配置文件、多环境配置、配置优先级、使用@ConfigurationProperties注解、使用@Value注解和使用PropertySources,希望对大家有所帮助。
原创文章,作者:VYWGL,如若转载,请注明出处:https://www.506064.com/n/368490.html
 
 微信扫一扫
微信扫一扫  支付宝扫一扫
支付宝扫一扫 