通过实例了解Spring中@Profile的作用
这篇文章主要介绍了通过实例了解Spring中@Profile的作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
根据系统环境的不同,Profile可以用来切换数据源。例如切换开发,测试,生产环境的数据源。
举个例子:
先创建配置类MainProfileConfig:
@Configuration
@PropertySource("classpath:/jdbc.properties")
publicclassMainProfileConfigimplementsEmbeddedValueResolverAware{
@Value("${db.user}")
privateStringuser;
privateStringdriverClass;
privateStringValueResolverstringValueResolver;
@Profile("test")
@Bean("testDataSource")
publicDataSourcegetTestDataSource(@Value("${db.password}")Stringpwd)throwsPropertyVetoException{
ComboPooledDataSourcecomboPooledDataSource=newComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
returncomboPooledDataSource;
}
@Profile("dev")
@Bean("devDataSource")
publicDataSourcegetDevDataSource(@Value("${db.password}")Stringpwd)throwsPropertyVetoException{
ComboPooledDataSourcecomboPooledDataSource=newComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
returncomboPooledDataSource;
}
@Profile("pro")
@Bean("proDataSource")
publicDataSourcegetproDataSource(@Value("${db.password}")Stringpwd)throwsPropertyVetoException{
ComboPooledDataSourcecomboPooledDataSource=newComboPooledDataSource();
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(pwd);
comboPooledDataSource.setDriverClass(driverClass);
returncomboPooledDataSource;
}
@Override
publicvoidsetEmbeddedValueResolver(StringValueResolverstringValueResolver){
this.stringValueResolver=stringValueResolver;
driverClass=stringValueResolver.resolveStringValue("${db.driverClass}");
}
}
这里使用@Value和StringValueResolver来给属性赋值
测试:运行的时候设置环境-Dspring.profiles.active=dev
publicclassProFileTest{
@Test
publicvoidtest(){
AnnotationConfigApplicationContextapplicationContext=newAnnotationConfigApplicationContext(MainProfileConfig.class);
String[]beanNamesForType=applicationContext.getBeanNamesForType(DataSource.class);
for(Stringname:beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
}
打印输出:
devDataSource
也可以使用代码的方式设置系统环境,创建容器的时候使用无参构造方法,然后设置属性。
@Test
publicvoidtest(){
//创建容器
AnnotationConfigApplicationContextapplicationContext=newAnnotationConfigApplicationContext();
//设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("test");
//设置主配置类
applicationContext.register(MainProfileConfig.class);
//启动刷新容器
applicationContext.refresh();
String[]beanNamesForType=applicationContext.getBeanNamesForType(DataSource.class);
for(Stringname:beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
打印输出:
testDataSource
setActiveProfiles设置环境的时候可以传入多个值,它的方法可以接受多个参数。
publicinterfaceConfigurableEnvironmentextendsEnvironment,ConfigurablePropertyResolver{
voidsetActiveProfiles(String...var1);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。