SpringBoot2.x——多环境配置
程序员文章站
2022-03-05 11:54:45
...
方式一:创建多个环境的配置文件,然后在application.yml
中加上一个spring.profiles.active=xxx
,分别指定配置文件的后缀就可以使用对应环境的配置了
https://blog.csdn.net/husong_/article/details/79784265
方式二:使用spring
为我们提供的Profile
配置类:
//@Profile("test") 只有是test环境的时候,整个配置类里面的所有配置才能开始生效
@PropertySource("classpath:/config/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware {
@Value("${jdbc.username}")
private String user;
private StringValueResolver valueResolver;
private String driverClass;
// @Profile("test") 没有标注环境标识的bean,在任何环境下都是加载的
// @Bean
// public Yellow yellow(){
// return new Yellow();
// }
@Profile("test") //加了环境标识的bean,只有这个环境被**的时候才能注册到容器中
@Bean("testDataSource")
public DataSource dataSourceTest(@Value("${jdbc.password}") String pwd) throws Exception{
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setUsername(user);
hikariDataSource.setPassword(pwd);
hikariDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/taotao?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
hikariDataSource.setDriverClassName(driverClass);
return hikariDataSource;
}
@Profile("dev")
@Bean("DevDataSource")
public DataSource dataSourceDev(@Value("${jdbc.password}") String pwd) throws Exception{
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setUsername(user);
hikariDataSource.setPassword(pwd);
hikariDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
hikariDataSource.setDriverClassName(driverClass);
return hikariDataSource;
}
@Profile("prod")
@Bean("ProdDataSource")
public DataSource dataSourceProd(@Value("${jdbc.password}") String pwd) throws Exception{
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setUsername(user);
hikariDataSource.setPassword(pwd);
hikariDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
hikariDataSource.setDriverClassName(driverClass);
return hikariDataSource;
}
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.valueResolver=resolver;
driverClass= valueResolver.resolveStringValue("${jdbc.driver}");
}
}
dbconfig.properties
配置文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
application.properties
:设置使用哪个环境
spring.profiles.active=prod
测试:
@Autowired
private HikariDataSource source;
@Test
public void showDataSource()throws Exception{
DatabaseMetaData metaData = source.getConnection().getMetaData();
System.out.println(metaData.getURL());
}
//spring.profiles.active=prod
jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8&useSSL=false
//spring.profiles.active=dev
jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8&useSSL=false
//spring.profiles.active=test
jdbc:mysql://localhost:3306/taotao?useUnicode=true&characterEncoding=UTF-8&useSSL=false
上一篇: C语言文件操作总结
下一篇: SpringBoot2.X 静态文件配置