在容器中如果存在同一类型的多个组件,也可以使用@Profile注解标识要获取的是哪一个bean,这在不同的环境使用不同的变量的情景特别有用。例如,开发环境、测试环境、生产环境使用不同的数据源,在不改变代码的情况下,可以使用这个注解来切换要连接的数据库。
 
 
步骤如下: 
1. 在bean上加@Profile注解,其value属性值为环境标识,可以自定义;
2. 使用无参构造方法创建容器
3. 设置容器环境,其值为第1步设置的环境标识
4. 设置容器的配置类
5. 刷新容器
 
注:2、4、5步其实是带参构造方法的步骤,相当于把带参构造方法拆开,在其中插入一条语句设置容器环境,详spring源码
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
this();
register(annotatedClasses);
refresh();
}
以下是在不同环境下不同的数据源组件的示例

/**
* 容器配置类
* 用于测试@Profile注解
*/
@Configuration
@PropertySource(value = {"classpath:/dbconfig.properties"})
public class ProfileBeanConfig implements EmbeddedValueResolverAware { //数据库连接用户名
@Value(value = "${jdbc.username}")
private String username;
//数据库连接密码
private String password; //开发环境数据源
@Bean(value = "dataSourceDev")
@Profile(value = "dev")
public DataSource dataSourceDev(@Value("${jdbc.driverClass}") String driverClass) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(this.username);
comboPooledDataSource.setPassword(this.password);
comboPooledDataSource.setDriverClass(driverClass);
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
return comboPooledDataSource;
} //测试环境数据源
@Bean(value = "dataSourceTest")
@Profile("test")
public DataSource dataSourceTest(@Value("${jdbc.driverClass}") String driverClass) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(this.username);
comboPooledDataSource.setPassword(this.password);
comboPooledDataSource.setDriverClass(driverClass);
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
return comboPooledDataSource;
} //生产环境数据源
@Bean(value = "dataSourceProduction")
@Profile("production")
public DataSource dataSourceProduction(@Value("${jdbc.driverClass}") String driverClass) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(this.username);
comboPooledDataSource.setPassword(this.password);
comboPooledDataSource.setDriverClass(driverClass);
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/production");
return comboPooledDataSource;
} //获取字符串解析器
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
//解析配置文件,然后对数据库连接密码进行赋值
this.password = resolver.resolveStringValue("jdbc.password");
}
}
以下是切换数据源示例

//创建匿名容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//设置环境,其值为@Profile注解的属性值
applicationContext.getEnvironment().setActiveProfiles("test");
//注册容器类
applicationContext.register(ProfileBeanConfig.class);
//刷新容器
applicationContext.refresh();

spring切换环境变量——@Profile注解的使用的更多相关文章

  1. Spring Boot 环境变量读取 和 属性对象的绑定

    网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...

  2. Ubuntu环境变量(.profile)加载顺序

    Ubuntu下启动的时候的的加载环境变量的过程大致为: /etc/enviroment /etc/profile   -->/etc/bash.bashrc   --> /etc/prof ...

  3. Aix6.1下su命令不能切换环境变量的问题

    su是Aix的通用命令,和linux系统下一样,用来切换当前用户.切换用户执行命令使用如下命令: su - $user -c "$command" su -命令区别于su的地方是它 ...

  4. spring boot: 一般注入说明(四) Profile配置,Environment环境配置 @Profile注解

    1.通过设定Environment的ActiveProfile来设置当前context所需要的环境配置,在开发中使用@Profile注解类或方法,达到不同情况下选择实例化不同的Bean. 2.使用jv ...

  5. 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置

    引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...

  6. Spring使用环境变量控制配置文件加载

    项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...

  7. 十五、Spring Boot 环境变量读取 和 属性对象的绑定

    凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @ ...

  8. Spring使用环境变量控制配置文件加载(转)

    项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...

  9. 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069509 凡是被spring管理的类,实现接口EnvironmentAware 重写方法 ...

随机推荐

  1. WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽

    1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...

  2. javascript 输入框监听事件

    <div class="coupon-exchange clearfix"> <div class="code-input"> < ...

  3. AFNetworking2.0源码解析<一>

    本篇先看看AFURLConnectionOperation,AFURLConnectionOperation继承自NSOperation,是一个封装好的任务单元,在这里构建了NSURLConnecti ...

  4. Centos6.6安装apache2.4

    安装apr请参考:   http://www.cnblogs.com/yuzhaokai0523/p/4382974.html 1安装httpd-2.4.10.tar.gz wget http://w ...

  5. Web基础之http协议

    第6章 Web基础之http协议 第6章 Web基础之http协议一.http协议介绍 1.1)什么是超文本 1.2)什么是URL 1.3)什么是超文本传输协议二.访问网站分析三.页面请求信息解析(仅 ...

  6. PHP程序员要看的书单

    想提升自己,还得多看书!多看书!多看书! 下面是我收集到的一些PHP程序员应该看得书单及在线教程,自己也没有全部看完.共勉吧! Github地址:https://github.com/52fhy/ph ...

  7. RPC协议解析

    RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.简言之,RPC使得程序能够像访问本地 ...

  8. [洛谷P3243] 菜肴制作

    问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1. 由于菜肴 ...

  9. jquery-时间轴滑动

    效果预览图: html: <div class="tim"> <div class="timdiv"> <div class=&q ...

  10. 举个例子去理解vuex(状态管理),通俗理解vuex原理,通过vue例子类比

    通俗理解vuex原理---通过vue例子类比   本文主要通过简单的理解来解释下vuex的基本流程,而这也是vuex难点之一. 首先我们先了解下vuex的作用vuex其实是集中的数据管理仓库,相当于数 ...