实战之shardingjdbc引入报错Cannot invoke “Object.toString()“ because the return value of “java.util.Map.get(

目录

 配置文件

见源码


实战之shardingjdbc引入报错Cannot invoke “Object.toString()“ because the return value of “java.util.Map.get(_第1张图片

 Initialization of bean failed; nested exception is java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "java.util.Map.get(Object)" is null

 配置文件

spring:
  shardingsphere:
    datasource:
      names: test
      test:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbcUrl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
        username: root
        password: root
    sharding:
      # 配置分表策略:根据split_no作为分片的依据(分片键)
      tables:
        t_sequence:
          # groovy脚本->{}
          actual-data-nodes: test.t_sequence_$->{0..1}
          table-strategy:
            inline:
              sharding-column: split_no
              algorithm-expression: t_sequence_$->{split_no%2}
    props:
      sql:
        show: true

 以上是报错内容,以上是我的配置文件。本次调试过程是通过断点解决的问题。

解决方式

数据源配置上 type: com.zaxxer.hikari.HikariDataSource 就好了

见源码

 报错地点在这里,创建数据源的时候就报错了,可以定位到是datasource里面配置的有问题了,继续进行深入断点。

    @Bean
    @Conditional({ShardingRuleCondition.class})
    public DataSource shardingDataSource() throws SQLException {
        return ShardingDataSourceFactory.createDataSource(this.dataSourceMap, (new ShardingRuleConfigurationYamlSwapper()).swap(this.shardingRule), this.props.getProps());
    }

    public final void setEnvironment(Environment environment) {
        String prefix = "spring.shardingsphere.datasource.";
        Iterator var3 = this.getDataSourceNames(environment, prefix).iterator();

        while(var3.hasNext()) {
            String each = (String)var3.next();

            try {
                this.dataSourceMap.put(each, this.getDataSource(environment, prefix, each));
            } catch (ReflectiveOperationException var6) {
                throw new ShardingSphereException("Can't find datasource type!", var6);
            } catch (NamingException var7) {
                throw new ShardingSphereException("Can't find JNDI datasource!", var7);
            }
        }

    }

    private List getDataSourceNames(Environment environment, String prefix) {
        StandardEnvironment standardEnv = (StandardEnvironment)environment;
        standardEnv.setIgnoreUnresolvableNestedPlaceholders(true);
        return null == standardEnv.getProperty(prefix + "name") ? (new InlineExpressionParser(standardEnv.getProperty(prefix + "names"))).splitAndEvaluate() : Collections.singletonList(standardEnv.getProperty(prefix + "name"));
    }

    private DataSource getDataSource(Environment environment, String prefix, String dataSourceName) throws ReflectiveOperationException, NamingException {
        Map dataSourceProps = (Map)PropertyUtil.handle(environment, prefix + dataSourceName.trim(), Map.class);
        Preconditions.checkState(!dataSourceProps.isEmpty(), "Wrong datasource properties!");
        if (dataSourceProps.containsKey("jndi-name")) {
            return this.getJndiDataSource(dataSourceProps.get("jndi-name").toString());
        } else {
            DataSource result = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
            DataSourcePropertiesSetterHolder.getDataSourcePropertiesSetterByType(dataSourceProps.get("type").toString()).ifPresent((dataSourcePropertiesSetter) -> {
                dataSourcePropertiesSetter.propertiesSet(environment, prefix, dataSourceName, result);
            });
            return result;
        }
    }

在运行setEnvironment的时候可以断点看到getDataSourceNames成功了,进入到while之后,进入getDataSource方法,然后我就发现了type,DataSource result = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);

看到这里问题就解决了,无声叹气( ╯□╰ )

实战代码见springboot+shardingjdbc(只分表、不分库)_spring cloud 只分表不分库-CSDN博客

你可能感兴趣的:(实战问题,java,android,开发语言)