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();
 }
}

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

相关文章

  • Spring Boot 使用WebAsyncTask异步返回结果

    Spring Boot 使用WebAsyncTask异步返回结果

    这篇文章主要介绍了Spring Boot 使用WebAsyncTask异步返回结果的相关资料,需要的朋友可以参考下
    2018-02-02
  • Elasticsearch中FST与前缀搜索应用实战解析

    Elasticsearch中FST与前缀搜索应用实战解析

    这篇文章主要为大家介绍了Elasticsearch中FST与前缀搜索应用实战解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • SpringBoot整合SQLite数据库全过程

    SpringBoot整合SQLite数据库全过程

    sqlite是一个很轻量级的数据库,可以满足日常sql的需求,下面这篇文章主要给大家介绍了关于SpringBoot整合SQLite数据库的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • 浅谈Java中是否直接可以使用enum进行传输

    浅谈Java中是否直接可以使用enum进行传输

    这篇文章主要介绍了浅谈Java中是否直接可以使用enum进行传输,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 解析Java设计模式编程中命令模式的使用

    解析Java设计模式编程中命令模式的使用

    这篇文章主要介绍了Java设计模式编程中命令模式的使用,在一些处理请求响应的场合经常可以用到命令模式的编程思路,需要的朋友可以参考下
    2016-02-02
  • Java Servlet简单实例分享(文件上传下载demo)

    Java Servlet简单实例分享(文件上传下载demo)

    下面小编就为大家带来一篇Java Servlet简单实例分享(文件上传下载demo)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 基于SpringBoot的Docker部署详解

    基于SpringBoot的Docker部署详解

    这篇文章主要为大家介绍了基于SpringBoot的Docker部署过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • Java的stream流多个字段排序的实现

    Java的stream流多个字段排序的实现

    本文主要介绍了Java的stream流多个字段排序的实现,主要是两种方法,第一种是固定多个字段排序和第二种动态字段进行排序,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • Java集合删除元素ArrayList实例详解

    Java集合删除元素ArrayList实例详解

    这篇文章主要介绍了Java集合删除元素ArrayList实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • java性能优化四种常见垃圾收集器汇总

    java性能优化四种常见垃圾收集器汇总

    这篇文章主要介绍了java性能优化四种常见垃圾收集器汇总,每种垃圾收集器都有其不同的算法实现和步骤,下面我们简单描述下我们常见的四种垃圾收集器的算法过程,感兴趣的同学们最好先看下以下的两篇文章去增加理解
    2022-07-07

最新评论