Skip to content

将配置换个地方

以下熟悉的配置,在springboot项目开发过程中,有许多约定俗成且常年不会变动的配置,这些配置使得配置文件看起来很多,但是实际上里面的大部分配置直到项目结束,再到下一个项目开始,都不会有人修改。所以我想通过架构设计的思想,把这些配置隐藏到基础平台的相应模块中,同时以配置文件优先级更高,意思就是如果配置文件中配置的相应的信息,则以配置文件为准,配置文件没有配置则使用平台模块的默认配置。不光自己开发的模块可以使用,引入的第三方模块中的配置也可以使用这种方式。

yaml
spring:
  datasource:
    hikari:
      pool-name: HikariCP
      minimum-idle: 5
      maximum-pool-size: 20
      auto-commit: true
      idle-timeout: 30000
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1
      validation-timeout: 5000
      leak-detection-threshold: 60000

mybatis:
    mapper-locations: classpath*:/mapper/**/*Mapper.xml

logging:
  level:
    com.example.mapper: DEBUG
    org.mybatis: DEBUG
    java.sql: DEBUG
    java.sql.Statement: DEBUG
    java.sql.PreparedStatement: DEBUG

下面以mybatis为例

在项目中创建一个模块,名称可以自定义。

添加mybatis依赖

xml
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

创建MybatisEnvironmentPostProcessor类

java

/**
 * @author Gnerv LiGen
 * 动态为mybatis添加配置 可以不在配置文件中添加了 如果配置文件中有 则使用配置文件中的
 */
@Slf4j
@Service
public class MybatisEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private static final String NAME = "PluginMybatisProperties";
    private static final Map<Object, Object> ENVIRONMENT = new ConcurrentHashMap<>();

    static {
        ENVIRONMENT.put("mybatis.mapper-locations", "classpath*:/mapper/**/*Mapper.xml");
        // 字段名自动转小驼峰
        ENVIRONMENT.put("mybatis.configuration.map-underscore-to-camel-case", true);
    }

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Properties properties = new Properties();
        properties.putAll(ENVIRONMENT);
        PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(NAME, properties);
        environment.getPropertySources().addLast(propertiesPropertySource);
        log.info("Mybatis environment post process finished");
    }
}

在resource目录中创建META-INF文件夹,在META-INF文件夹中创建spring.factories文件.

java
org.springframework.boot.env.EnvironmentPostProcessor=com.gnerv.sylvanas.framework.plugin.mybatis.autoconfigure.MybatisEnvironmentPostProcessor

我们的项目使用mybatis时,依赖我们这个模块就可以了。这样项目的的配置文件就可以删除mybatis相关的配置。

yaml
spring:
  datasource:
    hikari:
      pool-name: HikariCP
      minimum-idle: 5
      maximum-pool-size: 20
      auto-commit: true
      idle-timeout: 30000
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1
      validation-timeout: 5000
      leak-detection-threshold: 60000

logging:
  level:
    com.example.mapper: DEBUG
    org.mybatis: DEBUG
    java.sql: DEBUG
    java.sql.Statement: DEBUG
    java.sql.PreparedStatement: DEBUG

这样项目的配置文件中可以只留下需要变动的配置

这里使用了springboot的自动装配特性,在项目启动加载完成配置文件信息后,会执行上面的类方法,将定义好的默认配置添加到配置类对象的末尾,这里的配置类对象是一个有序集合,而springboot读取配置信息的优先级是从前往后读取,我们添加的配置是在配置文件之后,所有如果配置文件中存在,就不会使用我们的这里定义的,只有前面的都没有的时候才会使用我们的配置。

Released under the MIT License.