【问题标题】:Spring configuration set up : No qualifying bean of type [] found for dependencySpring配置设置:没有为依赖找到[]类型的合格bean
【发布时间】:2023-04-06 11:18:01
【问题描述】:

我对这个问题感到绝望。我一直在四处寻找解决我的异常的方法:我一直在寻找这个论坛以及一个提供许多可能原因清单的网站:http://www.baeldung.com/spring-nosuchbeandefinitionexception

似乎没有什么与我的配置相匹配。 我一定是错过了什么。

源异常是:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fr.bnp.paiement.persistence.api.idaos.IDaoCompte] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
    ... 15 more

如果我在 Spring 配置中明确给出 bean 位置而不是组件扫描,我会得到相同的异常。

我想知道这是否不是因为我的 Maven 项目中的模块的依赖链:

Services --> ServicesAPI
Services --> PersistenceAPI
Persistence --> PersistenceAPI

但我正在尝试将 Persistence bean 注入到 Services 中。如果 Services 不依赖于 Persistence 而是依赖于 PersistenceAPI 是否有问题? spring 配置 xml 文件遵循相同的导入链。

Services imports ServicesAPI
Services imports PersistenceAPI
Persistence imports PersistenceAPI

Service 模块如何查看 Persistence 模块中的 bean?也许通过 PersistenceAPI(这是我注射中持久性的类型)? 使用 EJB,这个依赖链可以正常工作(可能是因为没有 xml 导入要做)。

我尝试直接从服务导入持久性,但如果我不更改依赖链,它就不起作用:它只是找不到 xml 文件。 如果我不费心拆分 API 和实现,一切都会很好地工作,但这就是我想要实现的目标。

我正在尝试建立一个干净的 Maven 多模块项目。 代码在这里:https://github.com/ASolidGrasp/springTest.git 使其按原样工作的先决条件:安装 MySQL。

在 WSBanqueServices 模块中运行 fr.bnp.paiement.service.test.Main.main() 时出现错误。

对于那些喜欢在这里阅读代码的人:

在 WSBanqueServices 模块\fr.bnp.paiement.service.test 包中

public class Main
{
    public static void main(String[] args)
    {
        BeanFactory bF = new ClassPathXmlApplicationContext("classpath:springServices.xml");
        IServiceClient iServiceClient = (IServiceClient) bF.getBean("serviceClient");
    }
}

在 WSBanqueServices 模块\fr.bnp.paiement.service.ServiceClient

@Service
@Transactional
public class ServiceClient implements IServiceClient {

    @Inject
    private IDaoCompte iDaoCompte;
    @Inject
    private IDaoCarte iDaoCarte;

    ...

在 WSBanqueServices 模块\src/main/resources/springServices.xml

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
    <import resource="classpath:springServicesAPI.xml" />
    <import resource="classpath:springPersistenceAPI.xml" />

    <aop:aspectj-autoproxy />

    <context:component-scan base-package="fr.bnp.paiement.service" />

</beans>

在 WSBanqueServicesAPI 模块\src/main/resources/springServicesAPI.xml

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">

    <import resource="classpath:springEntities.xml" />
    <import resource="classpath:springServicesExceptions.xml" />

    <tx:annotation-driven transaction-manager="txManager" />

    <aop:aspectj-autoproxy />

    <context:component-scan base-package="fr.bnp.paiement.service.api.iservice" />

</beans>

在 WSBanquePersistenceAPI 模块\src/main/resources/springPersistenceAPI.xml

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">

    <import resource="classpath:springEntities.xml" />
    <import resource="classpath:springPersistenceExceptions.xml" />

    <tx:annotation-driven transaction-manager="txManager" />

    <aop:aspectj-autoproxy />

    <context:component-scan base-package="fr.bnp.paiement.persistence.api.idaos" />

</beans>

在 WSBanquePersistence 模块\src/main/resources/springPersistence.xml

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">

    <import resource="classpath:springPersistenceAPI.xml" />

    <aop:aspectj-autoproxy />

    <...datasource & co + transactional + persistenceContext...>

    <bean
        name="daoCarte"
        class="fr.bnp.paiement.persistence.daos.DaoCarte" />
    <bean
        name="daoCompte"
        class="fr.bnp.paiement.persistence.daos.DaoCompte" />

    <context:component-scan base-package="fr.bnp.paiement.persistence.daos" />

</beans>

在WSBanquePersistence模块\fr.bnp.paiement.persistence.daos.DaoCarte

@Transactional
@Repository("daoCarte")
public class DaoCarte implements IDaoCarte
{...}

在 WSBanquePersistence 模块\fr.bnp.paiement.persistence.daos.DaoCompte

@Repository("daoCompte")
@Transactional
public class DaoCompte implements IDaoCompte
{...}

提前感谢您的帮助。

【问题讨论】:

  • 如果您没有发布相关代码或异常堆栈跟踪的确切错误消息,我们将无法提供帮助。
  • 我认为代码太长而无法阅读,最好将链接提供给我的 github repo 和 projet。你真的更喜欢这里的代码吗?感谢您对我的问题感兴趣。
  • 相关代码必须在问题中。这就是这里的规则。要知道相关代码是什么,至少从发布异常的完整和准确的堆栈跟踪开始。

标签: spring maven dependency-injection


【解决方案1】:

springPersistence.xml 中定义并扫描了您的 bean(不知道为什么要同时进行这两项操作)。并且此文件不会直接或间接地由您在主类中加载的 springServices.xml 文件导入。

【讨论】:

  • 我两个都做了,因为我认为一个可能行不通,但最初只有包扫描。
  • 嗨,我尝试将 springPersistence.xml 文件直接导入到 springServices.xml 中,但没有找到它,因为没有人依赖 Persistence 模块。如果我合并 API 和实现,我猜我不会有这个问题,但我想要实现的是让它们分开。使用 EJB 我没有这个问题。
  • 如果你的类路径中没有实现DAO接口的类,你怎么会有一个实现DAO接口的bean呢?
  • 对不起,我不明白你的意思。我有实现 IDaoCompte 的 DaoCompte 和实现 IDaoCarte 的 DaoCarte。你是说 IDaoCarte 和 IDaoCompte 在类路径中是因为它们是通过 向 Spring 声明的,而实现不在类路径中,因为它们没有被导入?如果我想将接口和实现保留在 2 个不同的模块中,避免依赖于实现的任何事情,我该怎么办?感谢您的耐心等待。
  • 没有。我的意思是您必须在类路径中实现 bean,并且您必须导入扫描或声明 DAO bean 的 xml 文件。如果类路径中只有接口,则没有要执行的实际代码。
猜你喜欢
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 2016-12-31
  • 2018-12-02
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多