DruidDataSource

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Configuration
public class DruidConfig {

    @Bean
    @Qualifier("configDSPro")
    public Properties configProperties() {
        Properties properties = new Properties();
        try {
            FileInputStream is = new FileInputStream("conf/conf.properties");
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;

    }

    @Bean(name = "druidDataSource")
    public DataSource dataSource(@Autowired @Qualifier("configDSPro") Properties properties) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(properties.getProperty("url"));
        datasource.setDriverClassName(properties.getProperty("driverClassName"));
        datasource.setUsername(properties.getProperty("username"));
        datasource.setPassword(properties.getProperty("password"));
        datasource.setMaxActive(Integer.valueOf(properties.getProperty("maxActive")));
        datasource.setMaxWait(Integer.valueOf(properties.getProperty("maxWait")));
        datasource.setValidationQuery("SELECT 1");
        datasource.setTestWhileIdle(true);
        datasource.setTestOnBorrow(false);
        datasource.setTestOnReturn(false);
        return datasource;
    }

}
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>

5.1.48

 

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?Unicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.connectionTimeout=20000

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.max-wait=10000
spring.datasource.druid.max-active=30
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.validation-query=SELECT 1
spring.datasource.druid.validation-interval=30000

 

spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
spring.datasource.druid.stat-view-servlet.loginUsername=admin
spring.datasource.druid.stat-view-servlet.loginPassword=123456

 

posted @ 2021-09-05 23:58  tonggc1668  阅读(309)  评论(0编辑  收藏  举报