@Profile注解详解
@Profile注解详解
@Profile:
Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
开发环境develop、测试环境test、生产环境master
数据源:(/dev) (/test) (/master)
@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
- 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
- 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
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. 使用代码的方式激活某种环境;@Testpublic 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();}@Testpublic void test02() {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);System.out.println(Arrays.toString(beanNamesForType));applicationContext.close();}
}
————————————————
版权声明:本文为CSDN博主「码莎拉蒂」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ysl19910806/article/details/91646554
@Profile注解详解相关推荐
- @Configuration注解详解
@Configuration注解详解 Configuration代码简介 Configuration注释类表明其主要目的是作为bean定义的源:@Configuration类允许通过调用同一类中的其他@B ...
- 26.SpringBoot事务注解详解
转自:https://www.cnblogs.com/kesimin/p/9546225.html @Transactional spring 事务注解 1.简单开启事务管理 @EnableTrans ...
- mybatis注解详解
mybatis注解详解 首 先当然得下载mybatis-3.0.5.jar和mybatis-spring-1.0.1.jar两个JAR包,并放在WEB-INF的lib目录下 (如果你使用maven,则 ...
- 开启注解缓存_Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解
随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...
- spring-boot注解详解(一)
spring-boot注解详解(一) @SpringBootApplication @SpringBootApplication = (默认属性)@Configuration + @EnableAut ...
- Spring零配置之@Configuration注解详解
转载自 Spring零配置之@Configuration注解详解 @Configuration介绍 Spring3.0之前要使用Spring必须要有一个xml配置文件,这也是Spring的核心文件,而 ...
- Spring Boot注解详解
文章目录 使用注解的优势 注解详解(配备了完善的释义) 注解列表如下 JPA注解 springMVC相关注解 全局异常处理 项目中具体配置解析和使用环境 使用注解的优势 采用纯java代码,不在需要配 ...
- 【SpringBoot 】SpringBoot注解详解
[SpringBoot ]SpringBoot注解详解 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configura ...
- java method 注解_JAVA 注解详解及简单实例
JAVA 注解详解及简单实例 何为注解 注解(Annotation)又称为元数据,在JDK1.5后引入,它的作用是: 生成文档 这是注解的原始用途,可以通过注解生成JavaDoc文档 跟踪代码的依赖 ...
最新文章
- C++中operator关键字(重载操作符)
- 940m显卡 出现跳动的小点_启用4K 120Hz后NVIDIA安培显卡黑屏 原因找到:HDMI 2.1芯片BUG导致...
- java.lang.Thread 和 java.lang.Runnable的区别
- python不能安装pip_python – 无法安装pip:权限被拒绝错误
- 存储器间接寻址方式_8086微处理器的程序存储器寻址模式
- jenv java_mac 上使用jenv 管理的多个java 版本
- QTCreator2.8.0+Qt Open source 4.8.5环境配置(Win7x64)
- Java共现矩阵的构建(用于社交网络结构分析)
- 完成一个分析H264码流的工具
- python中字符串函数的作用_python 基础学习笔记(2)---字符串功能函数
- 设计模式16_策略模式
- 数据库:数据导入/数据导出
- Linux系统时间不同步问题
- ASP.NET2.0服务器控件之自定义状态管理
- Atitit 建设自己的财政体系 attilax总结 1.1. 收入理论	2 1.2. 收入分类	2 1.3. 	2 1.4. 非货币收入	2 1.5. 	2 1.6. 降低期望	2 1.7.
- 微星GE60有线网卡Qualcomm Atheros Bigfoot Killer E2200 ethernet card在ubuntu下无法找到驱动的解决办法...
- 个人邮箱怎么申请?个人外贸邮箱推荐
- max计算机什么函数,计算机max函数使用方法
- linux openoffice 安装字体,Linux服务器上安装openoffice,以及安装字体文件
- 基于HTML5移动app开发教程一