DruidDBConfiguration.java 4 KB
package com.viontech.configuration;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration  
public class DruidDBConfiguration {  
    private Logger logger = LoggerFactory.getLogger(DruidDBConfiguration.class);

    @Value("${spring.datasource.url:127.0.0.1}")  
    private String dbUrl;  
      
    @Value("${spring.datasource.username:postgresql}")  
    private String username;  
      
    @Value("${spring.datasource.password:password}")  
    private String password;  
      
    @Value("${spring.datasource.driverClassName:driverClassName}")  
    private String driverClassName;  
      
    @Value("${spring.datasource.initialSize:0}")  
    private int initialSize;  
      
    @Value("${spring.datasource.minIdle:0}")  
    private int minIdle;  
      
    @Value("${spring.datasource.maxActive:1}")  
    private int maxActive;  
      
    @Value("${spring.datasource.maxWait:0}")  
    private int maxWait;  
      
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis:0}")  
    private int timeBetweenEvictionRunsMillis;  
      
    @Value("${spring.datasource.minEvictableIdleTimeMillis:0}")  
    private int minEvictableIdleTimeMillis;  
      
    @Value("${spring.datasource.validationQuery:0}")  
    private String validationQuery;  
      
    @Value("${spring.datasource.testWhileIdle:false}")  
    private boolean testWhileIdle;  
      
    @Value("${spring.datasource.testOnBorrow:false}")  
    private boolean testOnBorrow;  
      
    @Value("${spring.datasource.testOnReturn:false}")  
    private boolean testOnReturn;  
      
    @Value("${spring.datasource.poolPreparedStatements:false}")  
    private boolean poolPreparedStatements;  
      
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize:0}")  
    private int maxPoolPreparedStatementPerConnectionSize;  
    private String filters;  
      
    @Value("${spring.datasource.connectionProperties:connectionProperties}")  
    private String connectionProperties;  
      
    @Bean     //声明其为Bean实例  
    @Primary  //在同样的DataSource中,首先使用被标注的DataSource
    @ConditionalOnExpression("#{'${spring.datasource.driverClassName:false}'!='false'}")
    public DataSource dataSource(){  
    	DruidDataSource datasource = new DruidDataSource();  
    	if("driverClassName".equals(driverClassName)){
    		return null;
    	}

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);  
        datasource.setPassword(password);  
        datasource.setDriverClassName(driverClassName);  
          
        //configuration  
        datasource.setInitialSize(initialSize);  
        datasource.setMinIdle(minIdle);  
        datasource.setMaxActive(maxActive);  
        datasource.setMaxWait(maxWait);  
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
        datasource.setValidationQuery(validationQuery);  
        datasource.setTestWhileIdle(testWhileIdle);  
        datasource.setTestOnBorrow(testOnBorrow);  
        datasource.setTestOnReturn(testOnReturn);  
        datasource.setPoolPreparedStatements(poolPreparedStatements);  
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);  
        try {  
            datasource.setFilters(filters);  
        } catch (SQLException e) {  
            logger.error("druid configuration initialization filter", e);  
        }  
        datasource.setConnectionProperties(connectionProperties);  
          
        return datasource;  
    }  
}