spring boot 2.1.2(spring framework 5以上) 集成disconf

本文解决新版Spring中因PropertyPlaceholderConfigurer方法废弃导致的Disconf集成问题,提供代码重写及基于代码配置的解决方案,包括数据库连接参数的动态加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

本文解决了由于高版本的spring 废除了一个过时的方法,导致无法集成disconf的问题(参考spring文档,重写了这个方法)。
除此之外,本文还提供了一种使用代码来集成disconf的方式。

重写被废除的方法部分

由于 在新版中PropertyPlaceholderConfigurer过时的parseStringValue 已经被移除,所以需要重写disconf 源码的disconf-client模块的ReloadingPropertyPlaceholderConfigurerparseStringValue()方法的最后一部分,重写代码如下:

// 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;
    }
}

如果你觉得我的文章对你有所帮助的话,欢迎关注我的公众号。赞!我与风来
认认真真学习,做思想的产出者,而不是文字的搬运工。错误之处,还望指出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值