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自动 ...
随机推荐
- hadoop基本文件配置
[学习笔记] 5)hadoop基本文件配置:hadoop配置文件位于:/etc/hadoop下(etc即:“etcetera”(附加物))core-site.xml:<configuration ...
- PAT甲级 二叉查找树 相关题_C++题解
二叉查找树 PAT (Advanced Level) Practice 二叉查找树 相关题 目录 <算法笔记> 重点摘要 1099 Build A Binary Search Tree ( ...
- 利用Python进行数据分析_Pandas_数据结构
申明:本系列文章是自己在学习<利用Python进行数据分析>这本书的过程中,为了方便后期自己巩固知识而整理. 首先,需要导入pandas库的Series和DataFrame In [21] ...
- Django dumpdata and loaddata
目录 dumpdata 命令 dumpdata 基本数据库的转存 dumpdata 备份特定的 app dumpdata 备份特定的表 dumpdata (--exclude) dumpdata (- ...
- idea jetty:run 启动
1.首先pom 文件 <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> ...
- 虚拟机Vmware使用记录
一直使用的是docker for windows,但是总会出现能打包,能打tag,但是push超时,所以想着弄个虚拟机来实现. 第一步: 安装VMware,安装一个ubantu最新的系统. 第二步: ...
- 联想U310 安装系统后无法识别机械硬盘处理
过程: 原30G的固态更换成250G的 mSATA固态,去掉机械硬盘,开始在固态里安装系统, 系统用PE登录,安装正版Win7 64B 专业版, 安装结束,接上机械硬盘, *PE下,可以正常识别2块硬 ...
- Sublime Text 添加java环境
jre/bin/ 目录下添加 runJava.bat @ECHO OFF cd %~dp1 ECHO Compiling %~nx1....... IF EXIST %~n1.class ( DEL ...
- ant Windows下环境变量配置 安装 编译
下载 官网:[http://ant.apache.org/] 其他版本:[http://archive.apache.org/dist/ant/binaries/] 点击这个进入下载页面 Window ...
- Windows下使用批处理文件.bat删除旧文件
本文教大家写一个批处理文件.bat删除旧文件,供大家参考,具体内容如下 1. 批处理文件 del_old_file.bat rem 删除D:\temp目录下7天前的文件Forfiles /p E:\b ...