╰_ 2022-02-12 17:48 采纳率: 70%
浏览 794
已结题

spring+Mybatis+Druid 报错ERROR {dataSource-1} init error java.lang.NullPointerException: null

问题遇到的现象和发生背景

spring+Mybatis+Druid 项目 多个数据源 启动就报错了

img

img

问题相关代码,请勿粘贴截图
package coms.louis.springboot.demo.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import coms.louis.springboot.demo.config.dds.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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 org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Configuration
@MapperScan(basePackages = "coms.louis.springboot.**.dao")    // 扫描DAO
@Repository
@EnableConfigurationProperties({DruidDataSourceProperties.class})
public class MybatisConfig implements Filter {
    //Spring Boot:集成Druid数据源
//    @Resource
//    private DataSource dataSource;
//
//    @Bean
//    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setTypeAliasesPackage("com.louis.springboot.**.model");    // 扫描Model
//
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml"));    // 扫描映射文件
//
//        return sessionFactory.getObject();
//    }
     //Spring Boot:实现MyBatis动态数据源
    @Resource
    private DruidDataSourceProperties properties;
   // @Bean("master")
//    @Bean(name = "master",destroyMethod = "", initMethod = "")
//    @Primary
//    @ConditionalOnMissingBean
//    @ConfigurationProperties(prefix = "spring.datasource.druid.master")
//    public DataSource master() throws SQLException {
//        return DataSourceBuilder.create().build();
//
//    }


    @Bean("master")
    @ConditionalOnMissingBean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.master")
    public DataSource master() {
        //return DataSourceBuilder.create().build();
        //return new DruidDataSource();
                DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getDriverClassName());
        druidDataSource.setUrl(properties.getJdbcurl());
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());
        druidDataSource.setInitialSize(properties.getInitialSize());
        druidDataSource.setMinIdle(properties.getMinIdle());
        druidDataSource.setMaxActive(properties.getMaxActive());
        druidDataSource.setMaxWait(properties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(properties.getValidationQuery());
        druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(properties.isTestOnReturn());
        druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setTestOnBorrow(false);
        druidDataSource.setTestOnReturn(false);
        System.out.println(druidDataSource.getUrl());
        try {
            druidDataSource.setFilters(properties.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            System.err.println("druid configuration initialization filter: " + e);
            e.printStackTrace();
        }

        return druidDataSource;

    }

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


        @Bean("dynamicDataSource")
       public DataSource dynamicDataSource() throws SQLException {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> dataSourceMap = new HashMap<>(2);
        dataSourceMap.put("master", master());
       dataSourceMap.put("slave", slave());
        // 将 master 数据源作为默认指定的数据源
        dynamicDataSource.setDefaultDataSource(master());
        // 将 master 和 slave 数据源作为指定的数据源
        dynamicDataSource.setDataSources(dataSourceMap);
        return dynamicDataSource;
    }

    @Bean("sqlSessionFactoryBean")
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource作为数据源则不能实现切换
        sessionFactory.setDataSource(dynamicDataSource());
        sessionFactory.setTypeAliasesPackage("com.louis.**.model");    // 扫描Model
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml"));    // 扫描映射文件
        return sessionFactory;
    }

    @Bean("transactionManager")
    public PlatformTransactionManager transactionManager() throws SQLException {
        // 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
        return new DataSourceTransactionManager(dynamicDataSource());
    }

 //   private static final Logger logger = LoggerFactory.getLogger(MybatisConfig.class);
//    @Bean("druidDataSource")
//    @ConditionalOnMissingBean
//    public DataSource druidDataSource() {
//        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setDriverClassName(properties.getDriverClassName());
//        druidDataSource.setUrl(properties.getJdbcurl());
//        druidDataSource.setUsername(properties.getUsername());
//        druidDataSource.setPassword(properties.getPassword());
//        druidDataSource.setInitialSize(properties.getInitialSize());
//        druidDataSource.setMinIdle(properties.getMinIdle());
//        druidDataSource.setMaxActive(properties.getMaxActive());
//        druidDataSource.setMaxWait(properties.getMaxWait());
//        druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
//        druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
//        druidDataSource.setValidationQuery(properties.getValidationQuery());
//        druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
//        druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
//        druidDataSource.setTestOnReturn(properties.isTestOnReturn());
//        druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
//        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
//        druidDataSource.setTestWhileIdle(true);
//        druidDataSource.setTestOnBorrow(false);
//        druidDataSource.setTestOnReturn(false);
//        System.out.println(druidDataSource.getUrl());
//        try {
//            druidDataSource.setFilters(properties.getFilters());
//            //druidDataSource.init();
//        } catch (SQLException e) {
//            System.err.println("druid configuration initialization filter: " + e);
//            e.printStackTrace();
//        }
//
//        return druidDataSource;
//    }

    /**
     * 注册Servlet信息, 配置监控视图
     *
     * @return
     */
    @Bean("ServletRegistrationBean")
    @ConditionalOnMissingBean
    public ServletRegistrationBean<Servlet> druidServlet()   {
        ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        //白名单:
        servletRegistrationBean.addInitParameter("allow","192.168.1.195");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny","192.168.1.119");
        //登录查看信息的账号密码, 用于登录Druid监控后台
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "true");
        return servletRegistrationBean;

    }

    /**
     * 注册Filter信息, 监控拦截器
     *
     * @return
     */
    @Bean("FilterRegistrationBean")
    @ConditionalOnMissingBean
    public FilterRegistrationBean<Filter> filterRegistrationBean() {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<Filter>();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.html,,*.ico,/druid/*");
        filterRegistrationBean.getServletRegistrationBeans().contains(".css");
        if (filterRegistrationBean.getServletRegistrationBeans().contains(".css") || filterRegistrationBean.getServletRegistrationBeans().contains(".js")) {
        }
        return filterRegistrationBean;
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //    请求和响应强转为子类类型
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //    设置请求编码
        request.setCharacterEncoding("UTF-8");
        //     获取请求的URI
        String URI = request.getRequestURI() ;
        //System.out.println(URI);
        //    排除静态页面
        if (URI.contains(".css") || URI.contains(".js") || URI.contains(".png")) {
            chain.doFilter(request, response);
            return ;
        }
        response.setContentType("text/css;charset=utf-8");
        // response.setContentType("application/javascript");

        //    .放行
        chain.doFilter(request, response);
    }


}
package coms.louis.springboot.demo.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import coms.louis.springboot.demo.config.dds.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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 org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Configuration
@MapperScan(basePackages = "coms.louis.springboot.**.dao")    // 扫描DAO
@Repository
@EnableConfigurationProperties({DruidDataSourceProperties.class})
public class MybatisConfig implements Filter {
    //Spring Boot:集成Druid数据源
//    @Resource
//    private DataSource dataSource;
//
//    @Bean
//    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setTypeAliasesPackage("com.louis.springboot.**.model");    // 扫描Model
//
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml"));    // 扫描映射文件
//
//        return sessionFactory.getObject();
//    }
     //Spring Boot:实现MyBatis动态数据源
    @Resource
    private DruidDataSourceProperties properties;
   // @Bean("master")
//    @Bean(name = "master",destroyMethod = "", initMethod = "")
//    @Primary
//    @ConditionalOnMissingBean
//    @ConfigurationProperties(prefix = "spring.datasource.druid.master")
//    public DataSource master() throws SQLException {
//        return DataSourceBuilder.create().build();
//
//    }


    @Bean("master")
    @ConditionalOnMissingBean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.master")
    public DataSource master() {
        //return DataSourceBuilder.create().build();
        //return new DruidDataSource();
                DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getDriverClassName());
        druidDataSource.setUrl(properties.getJdbcurl());
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());
        druidDataSource.setInitialSize(properties.getInitialSize());
        druidDataSource.setMinIdle(properties.getMinIdle());
        druidDataSource.setMaxActive(properties.getMaxActive());
        druidDataSource.setMaxWait(properties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(properties.getValidationQuery());
        druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(properties.isTestOnReturn());
        druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setTestOnBorrow(false);
        druidDataSource.setTestOnReturn(false);
        System.out.println(druidDataSource.getUrl());
        try {
            druidDataSource.setFilters(properties.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            System.err.println("druid configuration initialization filter: " + e);
            e.printStackTrace();
        }

        return druidDataSource;

    }

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


        @Bean("dynamicDataSource")
       public DataSource dynamicDataSource() throws SQLException {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> dataSourceMap = new HashMap<>(2);
        dataSourceMap.put("master", master());
       dataSourceMap.put("slave", slave());
        // 将 master 数据源作为默认指定的数据源
        dynamicDataSource.setDefaultDataSource(master());
        // 将 master 和 slave 数据源作为指定的数据源
        dynamicDataSource.setDataSources(dataSourceMap);
        return dynamicDataSource;
    }

    @Bean("sqlSessionFactoryBean")
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource作为数据源则不能实现切换
        sessionFactory.setDataSource(dynamicDataSource());
        sessionFactory.setTypeAliasesPackage("com.louis.**.model");    // 扫描Model
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml"));    // 扫描映射文件
        return sessionFactory;
    }

    @Bean("transactionManager")
    public PlatformTransactionManager transactionManager() throws SQLException {
        // 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
        return new DataSourceTransactionManager(dynamicDataSource());
    }

 //   private static final Logger logger = LoggerFactory.getLogger(MybatisConfig.class);
//    @Bean("druidDataSource")
//    @ConditionalOnMissingBean
//    public DataSource druidDataSource() {
//        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setDriverClassName(properties.getDriverClassName());
//        druidDataSource.setUrl(properties.getJdbcurl());
//        druidDataSource.setUsername(properties.getUsername());
//        druidDataSource.setPassword(properties.getPassword());
//        druidDataSource.setInitialSize(properties.getInitialSize());
//        druidDataSource.setMinIdle(properties.getMinIdle());
//        druidDataSource.setMaxActive(properties.getMaxActive());
//        druidDataSource.setMaxWait(properties.getMaxWait());
//        druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
//        druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
//        druidDataSource.setValidationQuery(properties.getValidationQuery());
//        druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
//        druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
//        druidDataSource.setTestOnReturn(properties.isTestOnReturn());
//        druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
//        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
//        druidDataSource.setTestWhileIdle(true);
//        druidDataSource.setTestOnBorrow(false);
//        druidDataSource.setTestOnReturn(false);
//        System.out.println(druidDataSource.getUrl());
//        try {
//            druidDataSource.setFilters(properties.getFilters());
//            //druidDataSource.init();
//        } catch (SQLException e) {
//            System.err.println("druid configuration initialization filter: " + e);
//            e.printStackTrace();
//        }
//
//        return druidDataSource;
//    }

    /**
     * 注册Servlet信息, 配置监控视图
     *
     * @return
     */
    @Bean("ServletRegistrationBean")
    @ConditionalOnMissingBean
    public ServletRegistrationBean<Servlet> druidServlet()   {
        ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        //白名单:
        servletRegistrationBean.addInitParameter("allow","192.168.1.195");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny","192.168.1.119");
        //登录查看信息的账号密码, 用于登录Druid监控后台
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "true");
        return servletRegistrationBean;

    }

    /**
     * 注册Filter信息, 监控拦截器
     *
     * @return
     */
    @Bean("FilterRegistrationBean")
    @ConditionalOnMissingBean
    public FilterRegistrationBean<Filter> filterRegistrationBean() {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<Filter>();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.html,,*.ico,/druid/*");
        filterRegistrationBean.getServletRegistrationBeans().contains(".css");
        if (filterRegistrationBean.getServletRegistrationBeans().contains(".css") || filterRegistrationBean.getServletRegistrationBeans().contains(".js")) {
        }
        return filterRegistrationBean;
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //    请求和响应强转为子类类型
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //    设置请求编码
        request.setCharacterEncoding("UTF-8");
        //     获取请求的URI
        String URI = request.getRequestURI() ;
        //System.out.println(URI);
        //    排除静态页面
        if (URI.contains(".css") || URI.contains(".js") || URI.contains(".png")) {
            chain.doFilter(request, response);
            return ;
        }
        response.setContentType("text/css;charset=utf-8");
        // response.setContentType("application/javascript");

        //    .放行
        chain.doFilter(request, response);
    }


}
===================
#server.port=8443
# DataSource
#  http://localhost:8080/druid/login.html
#  http://localhost:8080/swagger-ui.html
spring.main.allow-circular-references = true
#master
spring.datasource.druid.master.jdbcurl=jdbc:mysql://localhost:3306/master?characterEncoding=utf8&userSSL=false&serverTimezone=GMT%2B8

spring.datasource.druid.master.username=root
spring.datasource.druid.master.password=123456
spring.datasource.druid.master.driver-class-name=com.mysql.jdbc.Driver


#第二个数据源
spring.datasource.druid.slave.jdbcurl=jdbc:mysql://localhost:3306/slave?characterEncoding=utf8&useSSL=true
spring.datasource.druid.slave.username=root
spring.datasource.druid.slave.password=123456
spring.datasource.druid.slave.driver-class-name=com.mysql.jdbc.Driver

#spring.datasource.name=druidDataSource
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/white_jotter?characterEncoding=UTF-8
#spring.datasource.druid.username=root
#spring.datasource.druid.password=123456
spring.datasource.druid.db-type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.filters= stat,wall,log4j,config,wall
spring.datasource.druid.max-active= 100
spring.datasource.druid.initial-size= 2
spring.datasource.druid.max-wait=60000
spring.datasource.druid.min-idle= 1
spring.datasource.druid.time-between-eviction-runs-millis= 60000
spring.datasource.druid.min-evictable-idle-time-millis= 300000
spring.datasource.druid.validation-query= select 'x'
spring.datasource.druid.test-while-idle= true
spring.datasource.druid.test-on-borrow= false
spring.datasource.druid.test-on-return= false
spring.datasource.druid.pool-prepared-statements= true
spring.datasource.druid.max-open-prepared-statements= 50
spring.datasource.druid.max-pool-prepared-statement-per-connection-size= 20
spring.datasource.druid.destroy-scheduler="close"
spring.datasource.druid.init-exception-throw=true
##### druid 监控 ####
# WebStatFilter配置
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.html,*.ico,/druid/*
spring.datasource.druid.web-stat-filter.session-stat-enable=false
spring.datasource.druid.web-stat-filter.session-stat-max-count=1000
spring.datasource.druid.web-stat-filter.principal-session-name=admin
spring.datasource.druid.web-stat-filter.principal-cookie-name=admin
spring.datasource.druid.web-stat-filter.profile-enable=true
# StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.deny=""





#deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。
#如果allow没有配置或者为空,则允许所有访问
spring.datasource.druid.stat-view-servlet.allow=localhost 
server.port=8080
mybatis.mapper-locations=classpath:mapping/*.xml
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/white_jotter?characterEncoding=UTF-8
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.hibernate.ddl-auto =
#但对于屎山项目,可能这样的要求对于开发者会很痛苦。所以,你也可以通过下面的配置,放开不允许循环依赖的要求:
#静态文件必需配
spring.mvc.static-path-pattern=/**
spring.mvc.pathmatch.matching-strategy= ANT_PATH_MATCHER
spring.web.resources.static-locations=classpath:/static/
# pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql


运行结果及报错内容
我的解答思路和尝试过的方法

看网上的 直接 DruidDataSource druidDataSource = new DruidDataSource(); 不知道是配置问题 还是 config问题 。。之前可以但是druid 也会展示

我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 编号灬9527 2022-02-12 18:37
    关注

    循环依赖了,既然你自定义了datasource,
    启动类移除掉数据源的自动装配,@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    或者dynamicDataSource方法加DependsOn注解,把依赖的真实物理数据源先创建

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月14日
  • 创建了问题 2月12日

悬赏问题

  • ¥15 VB6可以成功读取的文件,用C#读不了
  • ¥15 如何使用micpyhon解析Modbus RTU返回指定站号的湿度值,并确保正确?
  • ¥15 C++ 句柄后台鼠标拖动如何实现
  • ¥15 有人会SIRIUS 5.8.0这个软件吗
  • ¥30 comsol仿真等离激元
  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO