前辈,请先登录以便查看更新内容。

#1 2019-01-19 14:27:18

ujued
管理员
所在地: 山东·济南
注册时间: 2019-01-14
帖子: 83

BUG:5.0.0.RC1: LogDbProperties强依赖DataSourceProperties

BUG: com.codingapi.txlcn.logger.db.LogDbProperties在实例化对象时强依赖springBoot的数据库配置信息。配置文件如下:

spring.datasource.driver-class-name=
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

5.0.0.RC1 设计错误:TxLogger设计来用户自定义日志存储位置,同时给DataSourceProperties配置信息作为默认值。但未考虑到DataSourceProperties也未配置时,会有NullPointerException。

解决方案:考虑用户未配置DataSourceProperties的情况,从而避免NullPointerException。

源码:
5.0.0.RC1

public class LogDbProperties extends HikariConfig {
    private boolean enabled = false;

    @Autowired
    public LogDbProperties(DataSourceProperties dataSourceProperties){
        this.setDriverClassName(dataSourceProperties.getDriverClassName());
        this.setJdbcUrl(dataSourceProperties.getUrl());
        this.setUsername(dataSourceProperties.getUsername());
        this.setPassword(dataSourceProperties.getPassword());
    }
}

5.0.0.RC2

public class LogDbProperties extends HikariConfig {

    private boolean enabled = false;

    @Autowired(required = false)
    public LogDbProperties(DataSourceProperties dataSourceProperties) {
       if (Objects.isNull(dataSourceProperties) ||
                Objects.isNull(dataSourceProperties.getDriverClassName()) ||
                Objects.isNull(dataSourceProperties.getUrl())) {
            log.info("TxLogger used user's config.");
            return;
        }
        this.setDriverClassName(dataSourceProperties.getDriverClassName());
        this.setJdbcUrl(dataSourceProperties.getUrl());
        this.setUsername(dataSourceProperties.getUsername());
        this.setPassword(dataSourceProperties.getPassword());
    }
}

5.0.0.RC2发布之后----------------------
真的十分抱歉,很惭愧,rc2的解决方案,仍然不能很好的解决这个问题,dataSourceProperties Bean 未定义时(比去你去掉了SpringBoot JDBC相关的自动配置),这里却不像预期的那样注入null,而是出现了required bean not found 类似的异常。

源码修改方案:

public class LogDbProperties extends HikariConfig {

    private boolean enabled = false;

    public LogDbProperties(@Autowired(required = false) DataSourceProperties dataSourceProperties) {
       if (Objects.isNull(dataSourceProperties) ||
                Objects.isNull(dataSourceProperties.getDriverClassName()) ||
                Objects.isNull(dataSourceProperties.getUrl())) {
            log.info("TxLogger used user's config.");
            return;
        }
        this.setDriverClassName(dataSourceProperties.getDriverClassName());
        this.setJdbcUrl(dataSourceProperties.getUrl());
        this.setUsername(dataSourceProperties.getUsername());
        this.setPassword(dataSourceProperties.getPassword());
    }
}

最近编辑记录 ujued (2019-01-22 22:48:50)

离线

页脚