Spring boot 总结之配置文件处理

背景

在项目开发过程中,很多东西我们都需要做成可配置化,那么使用spring的时候我们都有哪些使用配置文件的方式呢?接下来我们就来讲一下。

定义配置文件

我们以最长使用的jdbc为例

jdbc.url=jdbc:mysql://localhost/test
jdbc.username=dbuser
jdbc.password=dbpass
jdbc.driver-class-name=com.mysql.jdbc.Driver

不使用spring boot之前我们怎么处理

XML方式配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:jdbc.properties"/>
</bean>

<bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
Java Bean Configuration
使用Environment
@Component
@PropertySource("classpath:jdbc.properties")
public class ConfigurationWithEnviroment {

    @Autowired
    private Environment env;

    @PostConstruct
    public void init() {
        System.out.println("jdbc.url: " + env.getProperty("jdbc.url"));
        // 控制台输出jdbc.url: jdbc:mysql://localhost/test
    }

}

记得在使用Environment引入配置文件配置Mybatis的时候,报出异常,Environment注入为空的异常,原因是BeanFactoryPostProcessor Bean创建得比较早。详细请看: https://stackoverflow.com/questions/19421092/autowired-environment-is-null https://jira.spring.io/browse/SPR-13683

使用EnvironmentAware
@Component
@PropertySource("classpath:jdbc.properties")
public class ConfigurationWithEnviroment implements EnvironmentAware{
    
    private Environment env;

    @Override
    public void setEnvironment(Environment env) {
        this.env = env;
    }
    
    @PostConstruct
    public void init() {
        System.out.println("jdbc.url: " + env.getProperty("jdbc.url"));
        //控制台输出jdbc.url: jdbc:mysql://localhost/test
    }

}
使用@Value注解

使用@Value注解我们需要配置占位符org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>

或使用Java Bean配置

public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

Spring boot中默认会配置该选项,详细代码PropertyPlaceholderAutoConfiguration类

Spring Boot里的标准配置文件application.[properties|yaml]

Spring Boot官方统一配置文件,可以配置所有的Spring Boot中的配置,从datasource到web到安全等等。官方支持properties和yaml两种文件格式。

配置文件里的替换符

Spring Boot的配置文件支持上下文依赖替换,怎么说呢,就是我们可以使用前面定义的属性来达到公用的目的。 例如:

server:
  port: ${random.int[8080,8088]}
  name: ${random.name}

placeholder: ${server.name} //使用前面定义的server.name
配置文件里的随机数
server:
  port: ${random.int[8080,8088]} //8080到8088之间的随机数
  name: ${random.name} //随机字符串
  lessTen: ${random.int(10)}// 小于10
  uuid: ${random.uuid} // uuid

@Autowired
private Environment env;

@PostConstruct
public void init() {
    System.out.println(env.getProperty("server.port"));
    System.out.println(env.getProperty("server.name"));
    System.out.println(env.getProperty("server.lessTen"));
    System.out.println(env.getProperty("server.uuid"));
}

输出

8085
adb4648465e5675aa27ba28d47f88015
7
f5286b15-b37c-4e0a-aa42-baef2c663e20

Spring Boot里的注解@ConfigurationProperties

my: 
  list: 
    - name: java
    - name: php
    - name: go


@ConfigurationProperties("my")
@Component
public class Configuration {

    private final List<String> list = new ArrayList<String>(); //对应配置文件里的my list name.

    public List<String> getList() {
        return list;
    }
}

评论

暂无评论