搜尋

首頁  >  問答  >  主體

java - 使用Spring Boot配置Druid時dataSource無法被autowired

配置好了dataSource之後,想測試一下有沒有設定成功

package com.yang.Controller;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Yang on 2017/6/6.
 */
@RestController
public class TestController {

    @Autowired
    private DruidDataSource dataSource;

    @RequestMapping("/hello")
    public String index() {
        String sql = "select * from reader;";
        String str = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = dataSource.getConnection().prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                str = rs.getString(2);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return str;
    }

}

這個TestController是可以跑成功的,我想把dataSource.getConnection()再封裝一下

package com.yang.Utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;


/**
 * Created by Yang on 2017/6/6.
 */
public class ConnectionManager {

    private Logger logger = LoggerFactory.getLogger(ConnectionManager.class);

    @Autowired
    private DruidDataSource dataSource;

    private static volatile ConnectionManager connectionManager;

    private ConnectionManager(){}

    public static ConnectionManager getInstance(){
        if(connectionManager == null){
            synchronized (ConnectionManager.class){
                if(connectionManager == null){
                    connectionManager = new ConnectionManager();
                }
            }
        }
        return connectionManager;
    }

    public DruidPooledConnection getConn(){
        DruidPooledConnection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            logger.error("get connection error,the connection is null", e);
            e.printStackTrace();
        }
        return conn;
    }

}

寫了這個ConnectionManager,但在這個類別裡面,dataSource是null,不知道怎麼回事,想請教大家。
另外還有一個問題,在使用Spring Boot的時候,如何進行單元測試,我開始用單元測試測試一個ConnectionManager,但一直不成功

D:\Java\jdk_8u92\bin\java -Dspring.output.ansi.enabled=always -Didea.launcher.port=7532 "-Didea.launcher.bin.path=D:\develop\idea\IntelliJ IDEA 2016.2.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\environment\Java\jdk_8u92\jre\lib\charsets.jar;D:\environment\Java\jdk_8u92\jre\lib\deploy.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\access-bridge-64.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\cldrdata.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\dnsns.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\jaccess.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\jfxrt.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\localedata.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\nashorn.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\sunec.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\sunjce_provider.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\sunmscapi.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\sunpkcs11.jar;D:\environment\Java\jdk_8u92\jre\lib\ext\zipfs.jar;D:\environment\Java\jdk_8u92\jre\lib\javaws.jar;D:\environment\Java\jdk_8u92\jre\lib\jce.jar;D:\environment\Java\jdk_8u92\jre\lib\jfr.jar;D:\environment\Java\jdk_8u92\jre\lib\jfxswt.jar;D:\environment\Java\jdk_8u92\jre\lib\jsse.jar;D:\environment\Java\jdk_8u92\jre\lib\management-agent.jar;D:\environment\Java\jdk_8u92\jre\lib\plugin.jar;D:\environment\Java\jdk_8u92\jre\lib\resources.jar;D:\environment\Java\jdk_8u92\jre\lib\rt.jar;E:\myJava\LibraryManager\target\classes;D:\environment\maven\repository\org\springframework\boot\spring-boot-starter.4.1.RELEASE\spring-boot-starter-1.4.1.RELEASE.jar;D:\environment\maven\repository\org\springframework\boot\spring-boot.4.1.RELEASE\spring-boot-1.4.1.RELEASE.jar;D:\environment\maven\repository\org\springframework\spring-core.3.3.RELEASE\spring-core-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\springframework\spring-context.3.3.RELEASE\spring-context-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\springframework\spring-aop.3.3.RELEASE\spring-aop-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\springframework\spring-beans.3.3.RELEASE\spring-beans-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\springframework\spring-expression.3.3.RELEASE\spring-expression-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\springframework\boot\spring-boot-autoconfigure.4.1.RELEASE\spring-boot-autoconfigure-1.4.1.RELEASE.jar;D:\environment\maven\repository\org\springframework\boot\spring-boot-starter-logging.4.1.RELEASE\spring-boot-starter-logging-1.4.1.RELEASE.jar;D:\environment\maven\repository\ch\qos\logback\logback-classic.1.7\logback-classic-1.1.7.jar;D:\environment\maven\repository\ch\qos\logback\logback-core.1.7\logback-core-1.1.7.jar;D:\environment\maven\repository\org\slf4j\slf4j-api.7.21\slf4j-api-1.7.21.jar;D:\environment\maven\repository\org\slf4j\jcl-over-slf4j.7.21\jcl-over-slf4j-1.7.21.jar;D:\environment\maven\repository\org\slf4j\jul-to-slf4j.7.21\jul-to-slf4j-1.7.21.jar;D:\environment\maven\repository\org\slf4j\log4j-over-slf4j.7.21\log4j-over-slf4j-1.7.21.jar;D:\environment\maven\repository\org\yaml\snakeyaml.17\snakeyaml-1.17.jar;D:\environment\maven\repository\junit\junit.10\junit-4.10.jar;D:\environment\maven\repository\org\hamcrest\hamcrest-core.3\hamcrest-core-1.3.jar;D:\environment\maven\repository\org\springframework\boot\spring-boot-starter-web.4.1.RELEASE\spring-boot-starter-web-1.4.1.RELEASE.jar;D:\environment\maven\repository\org\springframework\boot\spring-boot-starter-tomcat.4.1.RELEASE\spring-boot-starter-tomcat-1.4.1.RELEASE.jar;D:\environment\maven\repository\org\apache\tomcat\embed\tomcat-embed-core.5.5\tomcat-embed-core-8.5.5.jar;D:\environment\maven\repository\org\apache\tomcat\embed\tomcat-embed-el.5.5\tomcat-embed-el-8.5.5.jar;D:\environment\maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket.5.5\tomcat-embed-websocket-8.5.5.jar;D:\environment\maven\repository\org\hibernate\hibernate-validator.2.4.Final\hibernate-validator-5.2.4.Final.jar;D:\environment\maven\repository\javax\validation\validation-api.1.0.Final\validation-api-1.1.0.Final.jar;D:\environment\maven\repository\org\jboss\logging\jboss-logging.3.0.Final\jboss-logging-3.3.0.Final.jar;D:\environment\maven\repository\com\fasterxml\classmate.3.1\classmate-1.3.1.jar;D:\environment\maven\repository\com\fasterxml\jackson\core\jackson-databind.8.3\jackson-databind-2.8.3.jar;D:\environment\maven\repository\com\fasterxml\jackson\core\jackson-annotations.8.3\jackson-annotations-2.8.3.jar;D:\environment\maven\repository\com\fasterxml\jackson\core\jackson-core.8.3\jackson-core-2.8.3.jar;D:\environment\maven\repository\org\springframework\spring-web.3.3.RELEASE\spring-web-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\springframework\spring-webmvc.3.3.RELEASE\spring-webmvc-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter.1.1\mybatis-spring-boot-starter-1.1.1.jar;D:\environment\maven\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure.1.1\mybatis-spring-boot-autoconfigure-1.1.1.jar;D:\environment\maven\repository\org\mybatis\mybatis.4.0\mybatis-3.4.0.jar;D:\environment\maven\repository\org\mybatis\mybatis-spring.3.0\mybatis-spring-1.3.0.jar;D:\environment\maven\repository\org\springframework\boot\spring-boot-starter-jdbc.4.1.RELEASE\spring-boot-starter-jdbc-1.4.1.RELEASE.jar;D:\environment\maven\repository\org\apache\tomcat\tomcat-jdbc.5.5\tomcat-jdbc-8.5.5.jar;D:\environment\maven\repository\org\apache\tomcat\tomcat-juli.5.5\tomcat-juli-8.5.5.jar;D:\environment\maven\repository\org\springframework\spring-jdbc.3.3.RELEASE\spring-jdbc-4.3.3.RELEASE.jar;D:\environment\maven\repository\org\springframework\spring-tx.3.3.RELEASE\spring-tx-4.3.3.RELEASE.jar;D:\environment\maven\repository\mysql\mysql-connector-java.1.39\mysql-connector-java-5.1.39.jar;D:\environment\maven\repository\org\springframework\boot\spring-boot-devtools.4.1.RELEASE\spring-boot-devtools-1.4.1.RELEASE.jar;D:\environment\maven\repository\com\alibaba\druid.0.29\druid-1.0.29.jar;D:\environment\Java\jdk_8u92\jre\..\lib\jconsole.jar;D:\environment\Java\jdk_8u92\jre\..\lib\tools.jar;D:\develop\idea\IntelliJ IDEA 2016.2.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.yang.Application

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.1.RELEASE)

2017-06-06 23:18:44.851  INFO 52496 --- [  restartedMain] com.yang.Application                     : Starting Application on Yang with PID 52496 (E:\myJava\LibraryManager\target\classes started by Yang in E:\myJava\LibraryManager)
2017-06-06 23:18:44.856  INFO 52496 --- [  restartedMain] com.yang.Application                     : No active profile set, falling back to default profiles: default
2017-06-06 23:18:45.257  INFO 52496 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@46a9c0: startup date [Tue Jun 06 23:18:45 CST 2017]; root of context hierarchy
2017-06-06 23:18:47.356  WARN 52496 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.yang.Mapper]' package. Please check your configuration.
2017-06-06 23:18:47.674  WARN 52496 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.yang]' package. Please check your configuration.
2017-06-06 23:18:48.316  INFO 52496 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$d3489f7] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-06-06 23:18:49.813  INFO 52496 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-06 23:18:49.835  INFO 52496 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-06-06 23:18:49.837  INFO 52496 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-06-06 23:18:50.072  INFO 52496 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-06-06 23:18:50.072  INFO 52496 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4821 ms
2017-06-06 23:18:50.448  INFO 52496 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-06 23:18:50.456  INFO 52496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-06 23:18:50.457  INFO 52496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-06 23:18:50.457  INFO 52496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-06 23:18:50.457  INFO 52496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-06 23:18:50.609  WARN 52496 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'connectionManager': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.alibaba.druid.pool.DruidDataSource]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-06-06 23:18:50.616  INFO 52496 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-06-06 23:18:50.650  WARN 52496 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2017-06-06 23:18:50.685  INFO 52496 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-06 23:18:50.984 ERROR 52496 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field dataSource in com.yang.Utils.ConnectionManager required a bean of type 'com.alibaba.druid.pool.DruidDataSource' that could not be found.


Action:

Consider defining a bean of type 'com.alibaba.druid.pool.DruidDataSource' in your configuration.


Process finished with exit code 0
package com.yang.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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 java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Yang on 2017/6/6.
 */
@Configuration
@EnableAutoConfiguration
public class DruidDBConfig {

    private Logger logger = LoggerFactory.getLogger(DruidDBConfig.class);

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("{spring.datasource.connectionProperties}")
    private String connectionProperties;

    @Bean     //声明其为Bean实例
    @Primary  //在同样的DataSource中,首先使用被标注的DataSource
    public DruidDataSource dataSource(){
        DruidDataSource datasource = new DruidDataSource();

        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;
    }

    @Bean
    public ServletRegistrationBean druidServletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new StatViewServlet());
        servletRegistrationBean.addUrlMappings("/druid/*");
        return servletRegistrationBean;
    }

    /**
     * 注册DruidFilter拦截
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean duridFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<String, String>();
        //设置忽略请求
        initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.setInitParameters(initParams);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

}
某草草某草草2740 天前1930

全部回覆(2)我來回復

  • 某草草

    某草草2017-06-12 09:24:20

    ConnectionManager 這個類別沒有@Component聲明,spring無法管理這個類,也無法注入dataSource

    回覆
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:24:20

    注入錯誤,前面的是正解,沒有Component聲明,需要透過設定文件,讓Spring管理這個bean,後續才能正確注入。
    接著測試,可以使用spring-boot-starter-test,裡麵包含junit,mock,assertJ等,spring-boot的官方文件對於測試又很詳細的說明,可以看一下。提供了許多方便的註解,大體上首先要在測試類別上加上兩個:
    @RunWith(SpringRunner.class)
    @SpringBootTest
    其次還有載入不同設定檔的註解,建議看文檔

    回覆
    0
  • 取消回覆