spring中@Profile的作用
根据系统环境的不同,Profile可以用来切换数据源。例如切换开发,测试,生产环境的数据源。
举个例子:
先创建配置类MainProfileConfig:
@Configuration
@PropertySource("classpath:/jdbc.properties")
public class MainProfileConfig implements EmbeddedValueResolverAware { @Value("${db.user}")
private String user; private String driverClass; private StringValueResolver stringValueResolver; @Profile("test")
@Bean("testDataSource")
public DataSource getTestDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
} @Profile("dev")
@Bean("devDataSource")
public DataSource getDevDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
} @Profile("pro")
@Bean("proDataSource")
public DataSource getproDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
} @Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
this.stringValueResolver = stringValueResolver;
driverClass = stringValueResolver.resolveStringValue("${db.driverClass}");
}
}
这里使用@Value和StringValueResolver来给属性赋值
测试:运行的时候设置环境 -Dspring.profiles.active=dev
public class ProFileTest { @Test
public void test(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainProfileConfig.class); String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
}
打印输出:
devDataSource
也可以使用代码的方式设置系统环境,创建容器的时候使用无参构造方法,然后设置属性。
@Test
public void test(){
//创建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("test");
//设置主配置类
applicationContext.register(MainProfileConfig.class);
//启动刷新容器
applicationContext.refresh(); String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
打印输出:
testDataSource
setActiveProfiles设置环境的时候可以传入多个值,它的方法可以接受多个参数。
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
void setActiveProfiles(String... var1);
spring中@Profile的作用的更多相关文章
- Spring 中bean的作用、定义
Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...
- Spring中FactoryBean的作用和实现原理
BeanFactory与FactoryBean,相信很多刚翻看Spring源码的同学跟我一样很好奇这俩货怎么长得这么像,分别都是干啥用的.BeanFactory是Spring中Bean工厂的顶层接口, ...
- spring中的Log4jConfigListener作用和webapp.root的设置
转:http://blog.sina.com.cn/s/blog_7bbf356c01016wld.html 使用spring中的Log4jConfigListener有如如下好处: 1. 动 ...
- Spring中ApplicationContextAware的作用
ApplicationContextAware 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法. ...
- Spring中depends-on的作用是什么?
spring的IOC容器负责bean的管理,当实例化一个bean是,spring保证该Bean所依赖的其他bean已经初始化.一般情况下,用<ref>元素建立对其他bean的依赖关系. 比 ...
- Spring 中context.start作用
我们经常会看到 如下代码 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath. ...
- @Transacitonal注解不生效之spring中expose-proxy的作用与原理
几年前记得整理过,@Transacitonal注解的方法被另外一个方法调用的时候,事务是不生效的. 如果大量代码已经这么写了,这个时候抽取出去不现实,怎么办呢? 答案就是在<aop:aspect ...
- Spring中@Component的作用
今天在写程序的时候看见一个以前没有见过的注解(@Component),在网上查找过后,经过实践,决定把它记录下来. 1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中 ...
- Spring中的TransactionProxyFactoryBean作用及配置(转)
问: 原文链接 http://blog.csdn.net/cpp_lzth/article/details/6551703 看AOP的时候发现spring中有个org.springframework. ...
随机推荐
- 富文本编辑器复制word
这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...
- CF940F Machine Learning 带修改莫队
题意:支持两种操作:$1.$ 查询 $[l,r]$ 每个数字出现次数的 $mex$,$2.$ 单点修改某一位置的值. 这里复习一下带修改莫队. 普通的莫队中,以左端点所在块编号为第一关键字,右端点大小 ...
- bzoj3694
/* * 对于不在最短路树上的边(x, y) * 1 * | * | * t * / \ * / \ * x-----y * 考虑这样一种形态的图, ‘-’ 标记为非最短路树的边 * 对于边集(x, ...
- 将.mat文件中的数据转换成图片
%% 如何将.mat文件中的数据转换成图片 clc;clear all; addpath F_data/MAT;load('D:\face.mat') for i=1:q img=uint8(re ...
- Ubuntu14.04 打开关闭图形桌面
使用 Ctrl+Alt+t 打开命令行界面 #关闭桌面 sudo service lightdm stop #恢复桌面 sudo service lightdm start Ctrl+Alt+F1 可 ...
- WGAN实验环境搭建
"TensorFlow在Windows上支持Python 3.5.x和3.6.x." 因此,您无法在Windows上使用Python 2.7的tensorflow windows+ ...
- POI的XWPFTableCell的方法
1. XWPFParagraph addParagraph() 在这个表格单元格中添加一个段落 2. void addParagraph(XWPFParagraph p) 给这个表格加一段 3. ja ...
- Java并发指南1:并发基础与Java多线程
本文转载自互联网,侵删 什么是并发 在过去单CPU时代,单任务在一个时间点只能执行单一程序.之后发展到多任务阶段,计算机能在同一时间点并行执行多任务或多进程.虽然并不是真正意义上的“同一时间点”,而是 ...
- 第一个smarty例子--分页显示数据
模板页index.tpl: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- android ONVIF 组播探测在线摄像机
http://blog.csdn.net/ghostyu/article/details/8182516 Android的Wifi,默认情况下是不接受组播的,见:http://developer.an ...