return dataSource;
}
// 生产环境
// @Profile(“pro”)
@Bean(“proDataSource”)
public DataSource dataSourcePro() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(“root”);
dataSource.setPassword(“806188”);
dataSource.setJdbcUrl(“jdbc:mysql://localhost:3306/vhr”);
dataSource.setDriverClass(“com.mysql.cj.jdbc.Driver”);
return dataSource;
}
}
可以看到,在配置类中,有不同的数据源配置,分别是测试环境、开发环境、生产环境,我们可以通过@Profile注解来指定注入哪种环境,当不指定则任何环境都能注册这个组件,也就是上面代码,运行测试类,输出结果如下,三个数据源组价都进行了注入:
将数据源使用@Profile注解进行标识,也就是将上面代码三个@Profile注解打开,此时因为没有激活注册环境,所以这个三个数据源都不能被注入。下面来进行激活。
【2】激活注册环境
-
default默认环境:
@Profile("default")
-
使用命令行动态参数:
-Dspring.profiles.active=dev
-
使用代码手动激活指定环境:要使用无参构造方法
-
配置在配置类上:只有在指定环境的时候,整个配置类的所有配置才能生效
- default默认环境,使用
@Profile("default")
,标识