经过几天的尝试,然后在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);
}
}

最佳答案

一般来说,
包含更多类似问题的详细信息。
特别,
什么是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>

关于java - 为什么我会收到没有合格bean的错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44573871/

10-10 01:14