实际情况
平时工作由于需要切换生产环境,测试环境,本地环境,数据库(Mybatis,Redis),thrift等等的配置均有变化,非常不方便,经常会在切换环境的时候遗漏某个配置的修改,导致需要重新打包或在本地修改了某个环境的数据库数据,因此想到了使用@Profile来解决Java端这方面的烦恼。
我的使用
由于我现在只是区分线上和本地的环境,线上因为指定了类的名字,故我本地新建了一个测试的主类,不需要配置环境变量。配置环境变量的相关内容可以看码农一枚的文章
数据库相关的在.properties文件声明的一些参数值
# 测试环境
spring.datasource.url=XXXXXX
spring.datasource.username=XXXXXX
spring.datasource.password=XXXXXX
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 本地环境
local.spring.datasource.url= jdbc:mysql://localhost:3306/flight_center?useUnicode=true&characterEncoding=utf-8
local.spring.datasource.username=root
local.spring.datasource.password=root
local.spring.datasource.driver-class-name=com.mysql.jdbc.Driver
@Profile("production")
@Bean(name = "dataSource",initMethod = "init",destroyMethod = "close")
public DruidDataSource dataSource(Environment environment) throws Exception{
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setName("masterDataSource");
druidDataSource.setUsername(environment.getProperty("spring.datasource.username"));
druidDataSource.setPassword(environment.getProperty("spring.datasource.password"));
druidDataSource.setUrl(environment.getProperty("spring.datasource.url"));
druidSettings(druidDataSource);
return druidDataSource;
}
@Profile("local")
@Bean(name = "dataSource",initMethod = "init",destroyMethod = "close")
public DruidDataSource dataSourceTest(Environment environment) throws Exception{
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setName("localDataSource");
druidDataSource.setUsername(environment.getProperty("local.spring.datasource.username"));
druidDataSource.setPassword(environment.getProperty("local.spring.datasource.password"));
druidDataSource.setUrl(environment.getProperty("local.spring.datasource.url"));
druidSettings(druidDataSource);
return druidDataSource;
}
在使用environment的时候 需要记得配置
@Configuration
@PropertySource({"classpath:application.properties","classpath:redis.properties"})
@ComponentScan("com.XX.XX")
public class SpringConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
thrfit相关的一些配置 * 因为thrift使用的是同事写的jar包,保留了XML配置
<beans profile="thriftLocal">
<bean id="server" class="com.XX.XX.server.server.Server">
<!-- setServer_port -->
<!--<property name="serverPort" value="8080"></property>-->
<property name="serverPort" value="8901"></property>
<property name="processorName" value="airPortServiceImpl"></property>
</bean>
</beans>
<beans profile="thriftProduction">
<bean id="server" class="com.XX.XX.server.server.Server">
<!-- setServer_port -->
<property name="serverPort" value="8080"></property>
<!--<property name="serverPort" value="8901"></property>-->
<property name="processorName" value="airPortServiceImpl"></property>
</bean>
</beans>
线上主类
public class Main {
public static void main( String[] args ) throws InterruptedException {
String[] strings = {"production", "thriftProduction"};
ApplicationContextUtils.createContext(strings);
}
}
本地的主类
public class LocalMain {
public static void main(String[] args) throws InterruptedException {
String[] strings = {"local", "thriftLocal"};
ApplicationContextUtils.createContext(strings);
}
}
启动的核心代码
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.getEnvironment().setActiveProfiles(profileName);
context.setValidating(false);
context.load( "classpath:spring.xml", "classpath:mybatis.xml");
context.refresh();
while (true) {
System.out.println(Dictionary.START_MESSAGE);
Thread.sleep(1000000);
}
总结
笨拙的使用,简单的记录,方便平时的开发。