Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource

项目结构:

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第1张图片

DruidConfig:

package com.lucifer.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

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

    //配置Druid的监控
    //1、配置一个管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map initParams = new HashMap<>();

        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","123456");
        initParams.put("allow","");//默认就是允许所有访问
        //initParams.put("deny","192.168.15.21");

        bean.setInitParameters(initParams);
        return bean;
    }


    //2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);

        bean.setUrlPatterns(Arrays.asList("/*"));

        return  bean;
    }
}

pom.xml:



    4.0.0

    com.lucifer
    spring-boot-data-jdbc
    0.0.1-SNAPSHOT
    jar

    spring-boot-data-jdbc
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
        

        
        
        
            com.alibaba
            druid
            1.1.8
        

        
            org.springframework.boot
            spring-boot-starter-web
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



SpringbootMybatisApplication:启动类

package com.lucifer.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }
}

(ps:如果pom.xml中加了这个runtime,application.yml中配置的driver-class-name可能会报红。)

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第2张图片

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第3张图片

application.yml:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/jdbc
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#    schema:
#      - classpath:department.sql

启动项目,控制台报错:

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第4张图片

查看报错信息: Reason: org/apache/log4j/Logger,提示原因在于log4j。

 注:  在springboot1.5的版本中,使用的是spring4.x,而springboot2.x使用的是spring5.x,在spring5.x后,官方弃用了log4j,而建议并使用log4j2,因此要么降低版本使用spring4.x的spring boot版本,要么额外去引入log4j的jar包。

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第5张图片

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第6张图片

解决办法:

(1)办法一:增加log4j的包


	log4j
	log4j
	1.2.17

(2)办法二:将spring-boot-starter-parent版本2.0.5改成1.5.15,改低一些。

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第7张图片

更改后,再次启动,控制台无报错信息。 

Springboot启动报错:Failed to bind properties under spring.datasource to javax.sql.DataSource_第8张图片 

你可能感兴趣的:(SpringBoot技术篇)