Spring Boot实现多环境下配置切换

2019-05-05  本文已影响0人  Xiewb

应用程序在不同的环境下,会有不同的配置信息。例如数据库连接信息,开发、测试和生产每个环境都有自己的配置信息。使用Spring Boot的Profile,可以帮助我们实现多场景下的配置切换,方便开发中进行测试和部署生产环境。下面是数据库连接信息多配置切换的例子。

添加datasource-dev.properties文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/devdb

添加datasource-prod.properties文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=654321
jdbc.url=jdbc:mysql://localhost:3306/proddb

编写配置类

上一篇集成Mybatis中,使用的@Value来获取配置文件信息,这次从环境中获取。

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

public class AbstractMybatisConfig implements EnvironmentAware {
    @Bean
    public DataSource dataSource() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("username",this.env.getProperty("jdbc.username"));
        prop.setProperty("password",this.env.getProperty("jdbc.password"));
        prop.setProperty("url",this.env.getProperty("jdbc.url"));
        prop.setProperty("driverClassName",this.env.getProperty("jdbc.driverClassName"));
        return DruidDataSourceFactory.createDataSource(prop);
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        ResourcePatternResolver res = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(
                res.getResources("classpath:com/**/dao/*.xml"));
        return sqlSessionFactory;
    }

    private Environment env;

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}

开发环境配置类

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;

@Configuration
@Profile("dev")
@MapperScan(basePackages="com.**.dao")
@PropertySource("classpath:datasource-dev.properties")
public class MybatisDevConfig extends AbstractMybatisConfig {
}

生产环境配置类

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;

@Configuration
@Profile("prod")
@MapperScan(basePackages="com.**.dao")
@PropertySource("classpath:datasource-prod.properties")
public class MybatisProdConfig extends AbstractMybatisConfig{
}

启动或部署应用

# application.yml文件或application.properties文件
# 表示使用开发环境的配置
spring:
  profiles:
    active: dev
# 表示使用生产环境的配置
java -jar xxx.jar --spring.profiles.active=prod
上一篇下一篇

猜你喜欢

热点阅读