为什么我得到No qualifying bean错误?

时间:2017-06-15 17:50:39

标签: java spring

在SpringSource论坛上尝试和等待答案几天后, 我会在这里试试。 运行我的应用程序会导致以下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException:
  Error creating bean with name 'loginDAOImpl':
  Unsatisfied dependency expressed through method 'setDataSource' parameter 0:
  No qualifying bean of type [org.apache.tomcat.dbcp.dbcp.BasicDataSource]
    found for dependency [org.apache.tomcat.dbcp.dbcp.BasicDataSource]:
  expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.apache.tomcat.dbcp.dbcp.BasicDataSource] found for dependency [org.apache.tomcat.dbcp.dbcp.BasicDataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:648) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at com.example.demo.DemoApplication.main(DemoApplication.java:10) 

这是相关代码

应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


<context:annotation-config/>

<!-- Scans within the base package of the application for @Component classes to configure as beans -->
<context:component-scan base-package="com.example.demo" />

<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/atata"/>
    <property name="username" value="user"/>
    <property name="password" value="pass"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>

类LoginDAOImpl:

@Repository
public class LoginDAOImpl implements LoginDAO {

private JdbcTemplate jdbcTemplateObject;

@Autowired
public void setDataSource(BasicDataSource dataSource) {

    this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
...
}

主:

@SpringBootApplication
public class DemoApplication {

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

1 个答案:

答案 0 :(得分:0)

一般来说, 包含更多细节与这样的问题。 特别, 什么是LoginDAOImpl类包? LoginDAOImpl类中BasicDataSource类的导入是什么?

首先猜测,我怀疑BasicDataSource导入将是org.apache.commons.dbcp.BasicDataSourceorg.apache.tomcat.dbcp.dbcp2.BasicDataSource

与您在dataSource bean定义中使用的类不同。

<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
  destroy-method="close">

可能性二:

您在dataSource bean配置中使用了错误的用户名和密码值格式。如果user和pass是属性值,则应使用${user}${pass}。像这样:

<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
  destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/atata"/>
    <property name="username" value="${user}"/>
    <property name="password" value="${pass}"/>
</bean>