Spring注解驱动开发 第十六节@Profile的使用
要使用@Profile注解,首先需要搭建一下适应的环境。
@SuppressWarnings("all")
@Configuration
@PropertySource("classpath:/DBSource.properties")
//@ComponentScan({"com.meng.beanfactory"})
public class MainConfig6 implements EmbeddedValueResolverAware {
// @Primary
// @Bean("bookDao2")
// public BookDao bookDao(){
// BookDao bookDao = new BookDao();
// bookDao.setLabel("2");
// return bookDao;
// }
private StringValueResolver resolver;
@Bean
public DataSource test(){
ComboPooledDataSource pool = null;
try {
pool = new ComboPooledDataSource();
pool.setUser("${username}");
pool.setPassword("");
pool.setJdbcUrl("jdbc:mysql://localhost:3306/ibatis");
pool.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return pool;
}
@Bean
public DataSource dev(){
ComboPooledDataSource pool = null;
try {
pool = new ComboPooledDataSource();
pool.setUser("${username}");
pool.setPassword("");
pool.setJdbcUrl("jdbc:mysql://localhost:3306/test");
pool.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return pool;
}
@Bean
public DataSource prod(){
ComboPooledDataSource pool = null;
try {
pool = new ComboPooledDataSource();
pool.setUser("${username}");
pool.setPassword("");
pool.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
pool.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return pool;
}
private String driverClass;
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.resolver = resolver;
driverClass = resolver.resolveStringValue("${driverClass}");
}
}
配置文件DBSource.properties
driverClass=com.mysql.jdbc.Driver
username=root
url=jdbc:mysql://localhost:3306/test
这里我采用了多种方式获取值,顺便复习一下,我在配置文件里建立了三个bean,都是连接数据库的,但是引入值的方式我用了两种,setUser我是用的是配置文件的方式引入,driverClass我使用spring的核心组件获取字符串占位符组件进行操作,其实第二种方式也是从配置文件中取值。
接下来我们使用@Profile注解
@SuppressWarnings("all")
@Configuration
@PropertySource("classpath:/DBSource.properties")
//@ComponentScan({"com.meng.beanfactory"})
public class MainConfig6 implements EmbeddedValueResolverAware {
// @Primary
// @Bean("bookDao2")
// public BookDao bookDao(){
// BookDao bookDao = new BookDao();
// bookDao.setLabel("2");
// return bookDao;
// }
private StringValueResolver resolver;
//加了环境标识的bean,只有这个环境被激活的时候才能注入到容器中,默认是default
@Profile("test")
@Bean
public DataSource test(){
ComboPooledDataSource pool = null;
try {
pool = new ComboPooledDataSource();
pool.setUser("${username}");
pool.setPassword("");
pool.setJdbcUrl("jdbc:mysql://localhost:3306/ibatis");
pool.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return pool;
}
@Profile("dev")
@Bean
public DataSource dev(){
ComboPooledDataSource pool = null;
try {
pool = new ComboPooledDataSource();
pool.setUser("${username}");
pool.setPassword("");
pool.setJdbcUrl("jdbc:mysql://localhost:3306/test");
pool.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return pool;
}
@Profile("prod")
@Bean
public DataSource prod(){
ComboPooledDataSource pool = null;
try {
pool = new ComboPooledDataSource();
pool.setUser("${username}");
pool.setPassword("");
pool.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
pool.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return pool;
}
private String driverClass;
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.resolver = resolver;
driverClass = resolver.resolveStringValue("${driverClass}");
}
}
我们在三个组件上面加了@Profile注解,并标注了他们的名称,表示他们在允许的情况下被加载到spring容器中。
给jvm设置参数,指定当前环境是test,查看打印结果。
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig6
test
Process finished with exit code 0
test被加载进来了,下面我们指定dev
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig6
dev
Process finished with exit code 0
dev又被加载进来了。
第一种是修改jvm虚拟机参数修改环境,现在我们要在代码中修改环境。
@Test
public void test03(){
//启动spring
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//设定当前环境
context.getEnvironment().setActiveProfiles("test","dev");
//加载配置文件
context.register(MainConfig6.class);
//刷新spring容器
context.refresh();
String[] names = context.getBeanDefinitionNames();
for(String name:names){
System.out.println(name);
}
}
上面代码已经详细说明了他们的功能,我们查看打印结果:
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig6
test
dev
Process finished with exit code 0
可以看到test和dev注入进来了。