gpt4 book ai didi

java - 为什么我收到“无合格 bean”错误?

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:58 26 4
gpt4 key购买 nike

经过几天的尝试并在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com