alibaba / druid

阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
https://github.com/alibaba/druid/wiki
Apache License 2.0
27.97k stars 8.58k forks source link

初始化数据源失败 #2018

Open ghost opened 7 years ago

ghost commented 7 years ago

springboot druid config start

spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/springbootdb_master?enconding=UTF-8 spring.datasource.druid.username=root spring.datasource.druid.password=guanglei spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver

springboot druid config end

package com.ekeyfund.framework.springboot.druid.configuration;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * Druid DataSource Configuration
 *
 * @author tony 18601767221@163.com
 * @create 2017-09-27-上午10:40
 * @see
 * @since JDK9
 */
@Configuration
public class DruidDataSourceConfiguration {

    private static final Logger logger = LogManager.getLogger();

    @Bean
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource druidDataSource(){

        logger.info("init druid datasource");

        DataSource druidDataSource= DruidDataSourceBuilder.create().build();

        try {
            logger.info("druid datasource  connection "+druidDataSource.getConnection());
        } catch (SQLException e) {
            logger.error("init drudid datasource exception "+e.getMessage());
        }
        return druidDataSource;
    }
}
wenshao commented 7 years ago

@lihengming 帮忙看一下

lihengming commented 7 years ago

在这里只是配置数据源,这时候数据源还没有被初始化呢,肯定报错了,你注入一个数据源再试试。另外,单数据源是默认进行自动配置的,不需要你自己再在这里配置,不知道你的用意是什么?

longxiaonan commented 6 years ago

@lihengming 大神怎么禁用自动配置啊, 我用的springcloud, 但是有些服务节点不需要连数据库

longxiaonan commented 6 years ago

我一个个试的,exclude了很多个类, 但是还是会自动配置,我的配置如下: `@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class, DataSourceTransactionManagerAutoConfiguration.class, DataSourceHealthIndicatorAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})//禁用mongo自动配置 //@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) @EnableDiscoveryClient public class TestApplication {

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

}`