spring切换环境变量——@Profile注解的使用
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注解的使用的更多相关文章
- Spring Boot 环境变量读取 和 属性对象的绑定
网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...
- Ubuntu环境变量(.profile)加载顺序
Ubuntu下启动的时候的的加载环境变量的过程大致为: /etc/enviroment /etc/profile -->/etc/bash.bashrc --> /etc/prof ...
- Aix6.1下su命令不能切换环境变量的问题
su是Aix的通用命令,和linux系统下一样,用来切换当前用户.切换用户执行命令使用如下命令: su - $user -c "$command" su -命令区别于su的地方是它 ...
- spring boot: 一般注入说明(四) Profile配置,Environment环境配置 @Profile注解
1.通过设定Environment的ActiveProfile来设置当前context所需要的环境配置,在开发中使用@Profile注解类或方法,达到不同情况下选择实例化不同的Bean. 2.使用jv ...
- 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置
引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...
- Spring使用环境变量控制配置文件加载
项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...
- 十五、Spring Boot 环境变量读取 和 属性对象的绑定
凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @ ...
- Spring使用环境变量控制配置文件加载(转)
项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...
- 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069509 凡是被spring管理的类,实现接口EnvironmentAware 重写方法 ...
随机推荐
- 攻防世界--CGfsb238
测试文件:https://adworld.xctf.org.cn/media/task/attachments/5982010c172744c8a1c93c24b5200b21 1.格式化字符串漏洞 ...
- Trait讲解
<?php /** * Trait解决PHP单继承的一种方法,使开发人员在不同层次结构的类中复用属性和方法 * Trait无法实例化 * Trait不是类,不能被继承,所以不能再Trait中不能 ...
- JS 页面跳转,参数的传递
当我们通过location.replace()进行页面的跳转时,我们想进行参数的传递,当时学习的时候,以前在网上找过获取方法,已经忘记出处在哪里了.获取方法大概是这样的: 1.将参数通过拼接的方式拼接 ...
- Spring基础19——Spring中几种注解的区别
1.@Autowired:注解是用来装配bean的,需要注入的bean必须是已经被IOC容器创建的bean,这个注解是利用类型装配的,如果容器中出现一个以上要装配的类或其子类就需要用@Qualifie ...
- Python自动化学习--鼠标和键盘事件
from selenium import webdriver from selenium.webdriver import ActionChains import time driver = webd ...
- PAT Advanced 1001 A+B Format (20 分)
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...
- 05java基础
1.BigInteger和BigDecimal类 package cn.jxufe.java.chapter5.demo01; import java.math.BigInteger; public ...
- hdu4731 Minimum palindrome (找规律)
这道题找下规律,3个字母或者以上的时候就用abcabcabc....循环即可. 一个字母时,就是aaaaa.....; 当只有2个字母时!s[1][]=a"; s[2][]="ab ...
- 【NOIP2016提高A组8.12】礼物
题目 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生日礼物. 商店里一共有种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种礼物的喜悦值不能重复获得). 每次,店员会按照一定 ...
- 【NOIP2016提高A组8.12】总结
惨败!!!! 第一题是一道神奇的期望问题. 第二题,发现"如果两个部门可以直接或间接地相互传递消息(即能按照上述方法将信息由X传递到Y,同时能由Y传递到X),我们就可以忽略它们之间的花费&q ...