RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
通过实例了解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

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

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

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


文章标题:通过实例了解Spring中@Profile的作用
URL标题:http://scyingshan.cn/article/pdeodi.html