标签:public his pass 数据库连接 string drive jdbc vlc icm
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) { this(); register(annotatedClasses); refresh(); }
/** * 容器配置类 * 用于测试@Profile注解 */ @Configuration @PropertySource(value = {"classpath:/dbconfig.properties"}) public class ProfileBeanConfig implements EmbeddedValueResolverAware { //数据库连接用户名 @Value(value = "${jdbc.username}") private String username; //数据库连接密码 private String password; //开发环境数据源 @Bean(value = "dataSourceDev") @Profile(value = "dev") public DataSource dataSourceDev(@Value("${jdbc.driverClass}") String driverClass) throws PropertyVetoException { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setUser(this.username); comboPooledDataSource.setPassword(this.password); comboPooledDataSource.setDriverClass(driverClass); comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev"); return comboPooledDataSource; } //测试环境数据源 @Bean(value = "dataSourceTest") @Profile("test") public DataSource dataSourceTest(@Value("${jdbc.driverClass}") String driverClass) throws PropertyVetoException { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setUser(this.username); comboPooledDataSource.setPassword(this.password); comboPooledDataSource.setDriverClass(driverClass); comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); return comboPooledDataSource; } //生产环境数据源 @Bean(value = "dataSourceProduction") @Profile("production") public DataSource dataSourceProduction(@Value("${jdbc.driverClass}") String driverClass) throws PropertyVetoException { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setUser(this.username); comboPooledDataSource.setPassword(this.password); comboPooledDataSource.setDriverClass(driverClass); comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/production"); return comboPooledDataSource; } //获取字符串解析器 @Override public void setEmbeddedValueResolver(StringValueResolver resolver) { //解析配置文件,然后对数据库连接密码进行赋值 this.password = resolver.resolveStringValue("jdbc.password"); } }
//创建匿名容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); //设置环境,其值为@Profile注解的属性值 applicationContext.getEnvironment().setActiveProfiles("test"); //注册容器类 applicationContext.register(ProfileBeanConfig.class); //刷新容器 applicationContext.refresh();
标签:public his pass 数据库连接 string drive jdbc vlc icm
原文地址:https://www.cnblogs.com/dubhlinn/p/10708240.html