24、自动装配-@Profile环境搭建
24、自动装配-@Profile环境搭建
- Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能。
- 开发环境、测试环境、正式环境 数据源切换
24.1 添加 数据源和jdbc驱动 pom 依赖
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
24.2 添加MainConfigOfProfile
package com.hw.springannotation.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.aop.target.CommonsPool2TargetSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
/**
* @Description Profile
* @Author hw
* @Date 2018/11/29 19:25
* @Version 1.0
*/
@Component
@PropertySource(value = {"classpath:/datasource.properties"})
public class MainConfigOfProfile implements EmbeddedValueResolverAware {
@Value("${db.username}")
private String username;
private String driveClassName;
private StringValueResolver resolver;
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.resolver = resolver;
this.driveClassName = this.resolver.resolveStringValue("${db.driveClassName}");
}
@Bean
public DataSource dataSourceTest(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setDriverClass(driveClassName);
return dataSource;
}
@Bean
public DataSource dataSourceDev(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
dataSource.setDriverClass(driveClassName);
return dataSource;
}
@Bean
public DataSource dataSourceProd(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/qm_dmp");
dataSource.setDriverClass(driveClassName);
return dataSource;
}
}
24.3 添加配置文件datasource.properties
db.username=root
db.password=root
db.driveClassName=com.mysql.jdbc.Driver
24.4 测试用例Test_ofProfile
package com.hw.springannotation.test;
import com.hw.springannotation.config.MainConfigOfProfile;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Description TODO
* @Author hw
* @Date 2018/11/29 20:14
* @Version 1.0
*/
public class Test_ofProfile {
@Test
public void test() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}

24、自动装配-@Profile环境搭建的更多相关文章
- 25、自动装配-@Profile根据环境注册bean
25.自动装配-@Profile根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的 ...
- Spring注解开发系列Ⅴ --- 自动装配&Profile
自动装配: spring利用依赖注入和DI完成对IOC容器中各个组件的依赖关系赋值.自动装配的优点有: 自动装配可以大大地减少属性和构造器参数的指派. 自动装配也可以在解析对象时更新配置. 自动装配的 ...
- Hadoop2.2.0 自动切换HA环境搭建
自动切换的HA,比手动切换HA集群多了一个zookeeper集群 机器分配: zookeeper:hadoop4,hadoop5,hadoop6 namenode:hadoop4,hadoop5 da ...
- Linux下环境搭建(四)——jenkins+gitlab+jmeter实践
经过前三篇博文的介绍,jenkins+gitlab+jmeter接口自动化的框架就搭建成功了,详细可见 Linux下环境搭建(一)——java.tomcat配置 Linux下环境搭建(二)——jenk ...
- Jenkins环境搭建(7)-集成钉钉消息推送
在去年的时候,搭建了一套Jenkins环境,基本功能已实现,可以通过如下地址查阅. Jenkins环境搭建(1)-下载与安装 Jenkins环境搭建(2)-搭建jmeter+ant+jenkins自动 ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- bean的自动装配,使用注解开发,使用java的方式配置Spring
bean的自动装配 自动装配是Spring满足bean依赖一种方式! Spring会在上下文中自动寻找,并自动给bean装配属性! 在Spring中有三种装配的方式 在xml中显示的配置 在java中 ...
- 记录使用gogs,drone搭建自动部署测试环境
使用gogs,drone,docker搭建自动部署测试环境 Gogs是一个使用go语言开发的自助git服务,支持所有平台 Docker是使用go开发的开源容器引擎 Drone是一个基于容器技术的持续集 ...
- 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建
使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 目录 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 使用jenkins自动 ...
随机推荐
- win10系统,jdk环境变量配置,编辑系统变量窗口显示旧版单行和新版列表问题
大家好,今天说一下我在配置jdk环境变量时遇到的编辑系统变量窗口显示问题. 首先我们说一下如何配置jdk环境变量. 右击此电脑,点击属性. 跳出如下窗口,点击高级系统设置. 跳出如下窗口,点击环境变量 ...
- Java的设计模式(4)--抽象工厂模式
提供一个创建一系列或相互依赖对象的接口,而无须指定他们具体的类.例如某些系统可能需要为用户提供一系列相关对象,但系统不希望用户直接使用new运算符实例化这些对象,而是应当由系统来控制这些对象的创建,否 ...
- 【C++札记】构造函数与析构函数
构造函数(constructor) 1.构造函数是种特殊的类成员函数,遵循如下规则: a.函数名与类名必须相同. b.没有返回值 例如: class Obj { ... public: Obj() { ...
- Spring Boot系列教程十:Spring boot集成MyBatis
一.创建项目 项目名称为 "springboot_mybatis_demo",创建过程中勾选 "Web","MyBatis" ...
- GB2312、GBK、GB18030 这几种字符集的主要区别
1 GB2312-80 GB 2312 或 GB 2312-80 是中国国家标准简体中文字符集,全称<信息交换用汉字编码字符集·基本集>,又称 GB 0,由中国国家标准总局发布,1981 ...
- 20191011-构建我们公司自己的自动化接口测试框架-Util的getTestSuite模块
getTestSuite主要是用于在testData里面获取测试集以及对应的测试数据,包括2个主要的方法,一个是获取测试集,一个是获取测试集里面要执行的测试用例 获取测试集方法: from Util. ...
- Unity中的Character Controller
Unity中默认提供了一个Character Controller的组件用于实现角色控制,一个3D的游戏物体,可以直接添加.Character Controller会自动模拟出Capsule Coll ...
- vue elment table根据返回值修改样式
今天在写vue项目的时候,查询出的数据库的数据想根据条件修改显示.查询资料有一个 :formatter,可以实现这个效果,废话不多说,这个是我的例子: <el-table-column prop ...
- 分布式事务(ACID特性、CAP定律)
普通事务和分布式事务的区别: 普通事务就是一般所说的数据库事务,事务是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成.当事务被提交给了DBMS(数据库管理系统),则DBMS(数 ...
- MiniUI学习笔记一【转】
MiniUI Api文档:http://miniui.com/docs/api/index.html 1.取组件值 传递form data,load发送 请求加载数据 <script type= ...