【发布时间】: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