Spring @Profile注解详解
作者:码莎拉蒂 发布时间:2023-04-20 06:26:16
标签:Spring,@Profile注解
@Profile注解详解
@Profile:Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
开发环境develop、测试环境test、生产环境master
数据源:(/dev) (/test) (/master)
@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
1) 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
2) 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
package com.spring.config;
import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* Profile:
* Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
*
* 开发环境develop、测试环境test、生产环境master
* 数据源:(/dev) (/test) (/master)
*
* @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
*
* 1) 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
* 2) 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
*
*/
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware{
@Value("${db.user}")
private String user;
private String driverClass;
@Profile("default")
@Bean("test")
public DataSource testDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("dev")
@Bean("dev")
public DataSource devDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("master")
@Bean("master")
public DataSource masterDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setDriverClass(driverClass);
return dataSource;
}
public void setEmbeddedValueResolver(StringValueResolver resolver) {
String driverClass = resolver.resolveStringValue("${db.driverClass}");
this.driverClass = driverClass;
}
}
package com.spring.test;
import java.util.Arrays;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.spring.config.MainConfigOfProfile;
public class IOCTestProfile {
//1. 使用命令行动态参数:在虚拟机参数位置加载 -Dspring.profiles.active=test
//2. 使用代码的方式激活某种环境;
@Test
public void test01() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
//1. 创建一个applicationContext
//2. 设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("dev","master");
//3. 注册主配置类
applicationContext.register(MainConfigOfProfile.class);
//4. 启动刷新容器
applicationContext.refresh();
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
System.out.println(Arrays.toString(beanNamesForType));
applicationContext.close();
}
@Test
public void test02() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
System.out.println(Arrays.toString(beanNamesForType));
applicationContext.close();
}
}
来源:https://blog.csdn.net/ysl19910806/article/details/91646554


猜你喜欢
- 将方形的图像映射到正方形上似乎并没有什么难度,所以接下来要做的是把图像映射到球面上。而球的参数方程为x=rcosϕcos&theta
- 本文实例讲述了C#获取两个时间的时间差并去除周末的方法。分享给大家供大家参考。具体分析如下:一般来说取时间差的代码很多,但是能够只取工作日的
- 简介上一篇我们讲了简单的动态BroadCast,今天我们通过手工来发送一条BroadCast进一步来了解BroadCast。在上一篇里我们使
- 本文实例为大家分享了Java手写线程池的实现代码,供大家参考,具体内容如下1.线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在
- 参考 java查找无向连通图中两点间所有路径的算法,对代码进行了部分修改,并编写了测试用例。算法要求:1. 在一个无向连通图中求出
- 先给大家展示下效果图:1、验证码生成类:import java.util.Random;import java.awt.imag
- 前言本文简单介绍了设计模式的一种——职责链模式 一、职责链模式的定义与特点定义:为了避免请求发送者与多个请求处理者耦合在一起,于是
- SpringBoot配置文件的替换使用spring.profiles.active在工作中,测试或上线的时候一定会遇到的问题就是修改配置。一
- 前言在做项目的时候,遇到一个需求,需要我对Chart图标标记数据正在运行,实现数据可视化,因为我们的表格是隐藏Y轴的刻度是看不到数据值的,于
- 引言我已经一个多星期没碰过电脑了,今日上班,打开电脑的第一件事就是想着写点什么。反正大家都还沉浸在节后的喜悦中,还没进入工作状态,与其浪费时
- 引言这一篇文章我们就通过介绍滑动冲突的规则和一个实例来更加深入的学习View的事件分发机制。1、外部滑动方向和内部滑动方向不一致考虑这样一种
- ListView 控件可使用四种不同视图显示项目。通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本。可使用 ListV
- 一、整合原理activiti的配置文件本身就是一个spring的配置文件,但默认情况下只讲ProcessEngineConfiguratio
- 上一篇文章自定义viewgroup(1)地址:https://www.jb51.net/article/100608.htm这里直接代码:p
- 伤害数字显示HUD游戏中收到伤害掉血,会有飘动的伤害数值;可以使用OnGUI中GUI.Label来实现;可自定义字体,颜色,大小等;如果需要
- 说明: 操作系统:deepin20.1一、下载eclipse_2021-03下载jdk-16.0.1下载,选下图所示: 二、安装2
- 前言本文主要学习函数的相关内容。1、函数是什么? * 中对函数的定义:子程序在计算机科学中,子程序(英语:Subroutine, proc
- JetBrains 系列产品(IDEA、Pycharm 等)使用本站破解教程 (opens new window),在输入激活码时,部分小伙
- 首先,建立图片与鼠标的对应关系。class MouseStyle{ [DllImport("user32.dll&qu
- private void btnSave_Click(object sender, RoutedEventArgs e)