关于Spring的@Profile注解的使用记录

实际情况

平时工作由于需要切换生产环境,测试环境,本地环境,数据库(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);
        }

总结

笨拙的使用,简单的记录,方便平时的开发。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值