通过实例了解Spring中@Profile的作用

 更新时间:2019年11月20日 10:21:35   作者:闻窗  
这篇文章主要介绍了通过实例了解Spring中@Profile的作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了通过实例了解Spring中@Profile的作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

根据系统环境的不同,Profile可以用来切换数据源。例如切换开发,测试,生产环境的数据源。

举个例子:

先创建配置类MainProfileConfig:

@Configuration
@PropertySource("classpath:/jdbc.properties")
public class MainProfileConfig implements EmbeddedValueResolverAware {

  @Value("${db.user}")
  private String user;

  private String driverClass;

  private StringValueResolver stringValueResolver;

  @Profile("test")
  @Bean("testDataSource")
  public DataSource getTestDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setUser(user);
    comboPooledDataSource.setPassword(pwd);
    comboPooledDataSource.setDriverClass(driverClass);
    return comboPooledDataSource;
  }

  @Profile("dev")
  @Bean("devDataSource")
  public DataSource getDevDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setUser(user);
    comboPooledDataSource.setPassword(pwd);
    comboPooledDataSource.setDriverClass(driverClass);
    return comboPooledDataSource;
  }

  @Profile("pro")
  @Bean("proDataSource")
  public DataSource getproDataSource(@Value("${db.password}") String pwd) throws PropertyVetoException {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setUser(user);
    comboPooledDataSource.setPassword(pwd);
    comboPooledDataSource.setDriverClass(driverClass);
    return comboPooledDataSource;
  }

  @Override
  public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
    this.stringValueResolver = stringValueResolver;
    driverClass = stringValueResolver.resolveStringValue("${db.driverClass}");
  }
}

这里使用@Value和StringValueResolver来给属性赋值

测试:运行的时候设置环境 -Dspring.profiles.active=dev

public class ProFileTest {

  @Test
  public void test(){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainProfileConfig.class);

    String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
    for (String name : beanNamesForType){
      System.out.println(name);
    }
    applicationContext.close();
  }
}

打印输出:

devDataSource

也可以使用代码的方式设置系统环境,创建容器的时候使用无参构造方法,然后设置属性。

@Test
  public void test(){
    //创建容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    //设置需要激活的环境
    applicationContext.getEnvironment().setActiveProfiles("test");
    //设置主配置类
    applicationContext.register(MainProfileConfig.class);
    //启动刷新容器
    applicationContext.refresh();

    String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
    for (String name : beanNamesForType){
      System.out.println(name);
    }
    applicationContext.close();
  }

打印输出:

testDataSource

setActiveProfiles设置环境的时候可以传入多个值,它的方法可以接受多个参数。

public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
  void setActiveProfiles(String... var1);

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

相关文章

  • maven 打包项目的几种方式

    maven 打包项目的几种方式

    maven目前在web上面的使用方式很普遍,而打包的方式也存在很多方式,本文就详细的介绍了三种方式,具有一定的参考价值,感兴趣的可以了解下
    2021-06-06
  • Springboot jar主清单属性丢失解决方案

    Springboot jar主清单属性丢失解决方案

    这篇文章主要介绍了Springboot jar主清单属性丢失解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • SpringBoot中实现数据脱敏处理的方法详解

    SpringBoot中实现数据脱敏处理的方法详解

    项目开发中,在处理敏感信息时,数据脱敏是一项重要的安全措施,本文主要为大家介绍了如何在SpringBoot项目中进行数据脱敏处理,有需要的可以了解下
    2025-03-03
  • 解决springboot错误:找不到或无法加载主类(配置编码或者Maven)

    解决springboot错误:找不到或无法加载主类(配置编码或者Maven)

    这篇文章主要介绍了解决springboot错误:找不到或无法加载主类(配置编码或者Maven)问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 浅析Java如何优雅的避免那无处不在的空指针异常

    浅析Java如何优雅的避免那无处不在的空指针异常

    在Java编程语言中,NullPointerException(简称NPE)是一种常见的运行时异常,本文主要来和大家讲讲如何优雅的避免这些空指针异常,需要的可以参考下
    2024-03-03
  • IDEA连接MySQL后管理数据库的操作指南

    IDEA连接MySQL后管理数据库的操作指南

    本节就来教大家如何在IDEA连接MySQL后管理数据库(创建/修改/删除数据库、创建/修改/删除表、插入/更新/删除/查询表记录),文中通过图文结合的方式给大家讲解的非常详细,需要的朋友可以参考下
    2024-05-05
  • 如何在logback日志配置里获取服务器ip和端口

    如何在logback日志配置里获取服务器ip和端口

    这篇文章主要介绍了如何在logback日志配置里获取服务器ip和端口的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java使用强大的Elastisearch搜索引擎实例代码

    Java使用强大的Elastisearch搜索引擎实例代码

    本篇文章主要介绍了Java使用强大的Elastisearch搜索引擎实例代码,具有一定的参考价值,有兴趣的可以了解一下
    2017-05-05
  • jdbc连接oracle数据库功能示例

    jdbc连接oracle数据库功能示例

    这篇文章主要介绍了jdbc连接oracle数据库功能,结合实例形式详细分析了java基于jdbc连接Oracle数据库的具体操作步骤与相关实现技巧,需要的朋友可以参考下
    2017-01-01
  • Java中的 BigDecimal 和 String 的相互转换问题

    Java中的 BigDecimal 和 String 的相互转换问题

    这篇文章主要介绍了Java中的 BigDecimal 和 String 的相互转换问题,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05

最新评论