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环境搭建的更多相关文章

  1. 25、自动装配-@Profile根据环境注册bean

    25.自动装配-@Profile根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的 ...

  2. Spring注解开发系列Ⅴ --- 自动装配&Profile

    自动装配: spring利用依赖注入和DI完成对IOC容器中各个组件的依赖关系赋值.自动装配的优点有: 自动装配可以大大地减少属性和构造器参数的指派. 自动装配也可以在解析对象时更新配置. 自动装配的 ...

  3. Hadoop2.2.0 自动切换HA环境搭建

    自动切换的HA,比手动切换HA集群多了一个zookeeper集群 机器分配: zookeeper:hadoop4,hadoop5,hadoop6 namenode:hadoop4,hadoop5 da ...

  4. Linux下环境搭建(四)——jenkins+gitlab+jmeter实践

    经过前三篇博文的介绍,jenkins+gitlab+jmeter接口自动化的框架就搭建成功了,详细可见 Linux下环境搭建(一)——java.tomcat配置 Linux下环境搭建(二)——jenk ...

  5. Jenkins环境搭建(7)-集成钉钉消息推送

    在去年的时候,搭建了一套Jenkins环境,基本功能已实现,可以通过如下地址查阅. Jenkins环境搭建(1)-下载与安装 Jenkins环境搭建(2)-搭建jmeter+ant+jenkins自动 ...

  6. 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)

    Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...

  7. bean的自动装配,使用注解开发,使用java的方式配置Spring

    bean的自动装配 自动装配是Spring满足bean依赖一种方式! Spring会在上下文中自动寻找,并自动给bean装配属性! 在Spring中有三种装配的方式 在xml中显示的配置 在java中 ...

  8. 记录使用gogs,drone搭建自动部署测试环境

    使用gogs,drone,docker搭建自动部署测试环境 Gogs是一个使用go语言开发的自助git服务,支持所有平台 Docker是使用go开发的开源容器引擎 Drone是一个基于容器技术的持续集 ...

  9. 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建

    使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 目录 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 使用jenkins自动 ...

随机推荐

  1. 【51nod】1776 路径计数

    [51nod]1776 路径计数 我们先把前两种数给排好,排好之后会有\(a + b + 1\)个空隙可以填数,我们计算有\(k\)个空隙两端都是相同字母的方案数 可以用枚举把第二种数分成几段插进去来 ...

  2. XDomainRequest IE8&amp;IE9 cors 跨域通讯的处理方法

       版权声明:避免百度一下通片同一篇文章,未经博主允许不得转载.本博客作为笔记使用,正确性请自行验证. https://blog.csdn.net/u014071104/article/detail ...

  3. logback日志输出至数据库

    我们知道将数据库输出到不同的地方需要使用不同的appender表示,那下面了解一下输出到数据库的DBAppender. 1.DBAppender 由于DBAppender会把记录写到数据库中,因此我们 ...

  4. VC++类型转换

    一.其他数据类型转换为字符串 短整型(int) itoa(i,temp,10):///将i转换为字符串放入temp中,最后一个数便是十进制 itoa(i,temp,2):///按二进制方式转换 长整型 ...

  5. 安装Docker step by step

    1. 系统要求 centos7以上   使用cat /etc/redhat-release查看系统版本,我的Centos 7.6 centos-extra 仓库 enable,默认是打开的 2.安装d ...

  6. 基于Docker的Kafka部署

    一 准备 1.1 安装docker-dompose #部署 sudo curl -L "https://github.com/docker/compose/releases/download ...

  7. Ocelot + Consul的demo(二)集群部署

    把服务A和服务B接口分别部署在两个ip地址上 修改 services.json文件, { "encrypt": "7TnJPB4lKtjEcCWWjN6jSA==&quo ...

  8. JFinal(2)JFinal 学习资料

    JFinal 学习资料 :http://pan.baidu.com/s/1hsOcQ0G 密码:7lq3 , jfinal社区收费会员内部部分资料,博主网络搜集而来

  9. JS/js是什么?

    JavaScript 是一种专为与网页交互而设计的脚本语言,由下列三个不同的部分组成: ECMAScript,由 ECMA-262 定义,提供核心语言功能; 文档对象模型(DOM),提供访问和操作网页 ...

  10. 图片预加载的插件使用-jquery.imgpreload.min.js

    使用方法: //图片预加载 var the_images = [];//新建一个数组,然后将要加载的图片地址推入这个数组中: the_images.push( 'bg.jpg' ); var load ...