Spring @Profile注解详解

 更新时间:2019年08月16日 09:27:13   作者:码莎拉蒂  
这篇文章主要介绍了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();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java的Integer缓存池用法

    Java的Integer缓存池用法

    Java的Integer缓存池主要为了提升性能和节省内存,它缓存了-128到127范围内的Integer对象,因此这些对象在比较时会直接比较引用,而不是值,其他包装类如Byte、Short、Character也有类似的缓存池
    2025-02-02
  • Java对称与非对称加密算法原理详细讲解

    Java对称与非对称加密算法原理详细讲解

    对称加密算法指加密和解密使用相同密钥的加密算法。对称加密算法用来对敏感数据等信息进行加密,非对称加密算法指加密和解密使用不同密钥的加密算法,也称为公私钥加密
    2022-11-11
  • SpringBoot业务逻辑异常的处理方法介绍

    SpringBoot业务逻辑异常的处理方法介绍

    本篇文章为大家展示了如何在SpringBoot中统一处理逻辑异常,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获
    2022-09-09
  • JVM进程缓存Caffeine的使用

    JVM进程缓存Caffeine的使用

    本文主要介绍了JVM进程缓存Caffeine的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • Java函数式编程(十二):监控文件修改

    Java函数式编程(十二):监控文件修改

    这篇文章主要介绍了Java函数式编程(十二):监控文件修改,本文是系列文章的第12篇,其它文章请参阅本文底部的相关文章,需要的朋友可以参考下
    2014-09-09
  • 使用SpringDataJpa创建中间表

    使用SpringDataJpa创建中间表

    这篇文章主要介绍了使用SpringDataJpa创建中间表,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Java中Exception和Error的区别详解

    Java中Exception和Error的区别详解

    这篇文章主要介绍了Java中Exception和Error的区别详解,通过类的关系分析两者的区别与应用场景,包含代码实例,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • java通过方向键控制小球移动的小游戏

    java通过方向键控制小球移动的小游戏

    这篇文章主要为大家详细介绍了java通过方向键控制小球移动的小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • springboot中设置定时任务的三种方法小结

    springboot中设置定时任务的三种方法小结

    在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容,本文介绍了springboot中设置定时任务的三种方法,主要包括@Scheduled注解,Quartz框架和xxl-job框架的实现,感兴趣的可以了解一下
    2023-12-12
  • Android图片转换器代码分享

    Android图片转换器代码分享

    本文给大家总结了下在安卓程序中进行图片转换的方法,非常的实用,小伙伴们可以参考下。
    2015-10-10

最新评论