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

网友投稿 378 2023-07-10

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

通过实例了解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(drsJJXTMbBrWiverClass);

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 applicationCsJJXTMbBrWontext = 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 Configuhttp://rableEnvironment extends Environment, ConfigurablePropertyResolver {

void setActiveProfiles(String... var1);

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring实战之注入嵌套Bean操作示例
下一篇:Spring中的事务管理如何配置
相关文章

 发表评论

暂时没有评论,来抢沙发吧~