简介
本文解决了由于高版本的spring 废除了一个过时的方法,导致无法集成disconf的问题(参考spring文档,重写了这个方法)。
除此之外,本文还提供了一种使用代码来集成disconf的方式。
重写被废除的方法部分
由于 在新版中PropertyPlaceholderConfigurer过时的parseStringValue 已经被移除,所以需要重写disconf 源码的disconf-client模块的ReloadingPropertyPlaceholderConfigurer
的parseStringValue()
方法的最后一部分,重写代码如下:
// then, business as usual. no recursive reloading placeholders please.、
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
return helper.replacePlaceholders(strVal,props);
提供已重写好的disconf jar包。
补充 : 这里可以不修改源码,在使用disconf的项目中重写该类,然后修改掉过时的部分,其它的代码不要改动。在需要配置ReloadingPropertyPlaceholderConfigurer
的地方换上自己重写的这个类。
提供基于代码的配置
配置的代码如下,可以发现,和xml 配置的bean 无异
@Configuration
public class DisconfConfig {
@Bean(destroyMethod = "destroy")
@AutoConfigureOrder
public DisconfMgrBean disconfMgrBean(){
DisconfMgrBean disconfMgrBean = new DisconfMgrBean();
disconfMgrBean.setScanPackage("com.joker");
return disconfMgrBean;
}
@Bean(initMethod = "init",destroyMethod = "destroy")
public DisconfMgrBeanSecond disconfMgrBeanSecond(){
return new DisconfMgrBeanSecond();
}
@Bean("propertiesFactoryBean")
public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean(){
ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean = new ReloadablePropertiesFactoryBean();
List<String> locations = new ArrayList<>();
locations.add("dubbo.properties");
locations.add("file.properties");
locations.add("mongodb.properties");
locations.add("mysql.properties");
locations.add("rabbitmq-config.properties");
locations.add("redis.properties");
reloadablePropertiesFactoryBean.setLocations(locations);
return reloadablePropertiesFactoryBean;
}
@Bean("propertyPlaceholderConfigurer")
public ReloadingPropertyPlaceholderConfigurer reloadingPropertyPlaceholderConfigurer(@Qualifier("propertiesFactoryBean") ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean){
ReloadingPropertyPlaceholderConfigurer reloadingPropertyPlaceholderConfigurer = new ReloadingPropertyPlaceholderConfigurer();
reloadingPropertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
reloadingPropertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
try {
reloadingPropertyPlaceholderConfigurer.setProperties(reloadablePropertiesFactoryBean.getObject());
} catch (IOException e) {
e.printStackTrace();
}
return reloadingPropertyPlaceholderConfigurer;
}
}
通过disconf 的入参连接数据库
这里代码就显得啰嗦了,我也不知道各位有没有更好的方法,如果有的话,还请告知。
@Configuration()
@EnableTransactionManagement()
// 这里配置了两个数据源
public class CenterDataSourceConfig {
@Primary
@Bean("centerDataSource")
// $ 中的字符串是对应的disconf 中上传的配置文件的key
public DataSource centerDataSource(@Value("${mysql.url.basiccenter}")String jdbcUrl,@Value("${mysql.centerusername}")String username,@Value("${mysql.centerpassword}")String password){
return getDataSource(jdbcUrl, username, password);
}
@Bean("baseDataSource")
public DataSource baseDataSource(@Value("${mysql.url.basicdata}")String jdbcUrl,@Value("${mysql.basedatausername}")String username,@Value("${mysql.basedatapassword}")String password){
return getDataSource(jdbcUrl, username, password);
}
@Bean("centerJdbcTemplate")
public JdbcTemplate centerJdbcTemplate(@Qualifier("centerDataSource")DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean("baseJdbcTemplate")
public JdbcTemplate baseJdbcTemplate(@Qualifier("baseDataSource")DataSource dataSource){
return new JdbcTemplate(dataSource);
}
private DataSource getDataSource(String jdbcUrl,String username,String password){
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(username);
hikariDataSource.setPassword(password);
return hikariDataSource;
}
}
如果你觉得我的文章对你有所帮助的话,欢迎关注我的公众号。赞!
认认真真学习,做思想的产出者,而不是文字的搬运工。错误之处,还望指出!