springboot项目初始化druid连接池时报错java.lang.NullPointerException: null

原创 2020-04-27 18:25 阅读(5510)次

新创建了一个springboot项目,一顿配置,以为能正常启动,结果打脸了,报错了:

2020-04-27 18:14:04.655 ERROR 18376 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} init error

java.lang.NullPointerException: null
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325) ~[na:1.8.0_191]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_191]
	at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:589) ~[druid-1.1.9.jar:1.1.9]
	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:815) [druid-1.1.9.jar:1.1.9]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1227) [druid-1.1.9.jar:1.1.9]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1223) [druid-1.1.9.jar:1.1.9]
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:90) [druid-1.1.9.jar:1.1.9]
	at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:612) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:669) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:700) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:712) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:763) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:844) [spring-jdbc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
	at com.tianlong.server.messageserver.dao.impl.BusinessConfigDaoImpl.getNeedRunBusinessConfigList(BusinessConfigDaoImpl.java:24) [classes/:na]

如上报错信息显示在com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:589)中创建连接数据库的驱动时空指针了,Debug了之后才发现配置类有问题。

配置类代码如下:

    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

prefix少了druid节点,正确配置为

@ConfigurationProperties(prefix="spring.datasource.druid")

贴上我的配置application.yml:

spring:
  application:
    name: tianlong
  datasource:
    name: tianlong
    type: com.alibaba.druid.pool.DruidDataSource
    #druid相关配置
    druid:
      #监控统计拦截的filters
      filters: stat
      #driver-class-name: com.mysql.cj.jdbc.Driver
      driver-class-name: org.postgresql.Driver
      #基本属性
      url: jdbc:postgresql://localhost:5432/postgres
      username: postgre
      password: 123456!@#
      #配置初始化大小/最小/最大
      initial-size: 1
      min-idle: 1000
      max-active: 5000
      #获取连接等待超时时间
      max-wait: 60000
      #间隔多久进行一次检测,检测需要关闭的空闲连接
      time-between-eviction-runs-millis: 60000
      #一个连接在池中最小生存的时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20
-------
-------

如果你还有其他问题,推荐你看看这本书
Java微服务架构实战(SpringBoot+SpringCloud+Docker+RabbitMQ)
名字好长啊,里面有讲到
spring boot集成druid,或者能帮到你