Spring配置设置:找不到依赖项

时间:2015-09-06 14:21:02

标签: spring maven dependency-injection

我对这个问题感到绝望。我一直在寻找近一个星期寻找我的例外的解决方案:我一直在寻找这个论坛,也在一个网站上提供了许多可能原因的清单: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。如果服务不依赖于持久性而是依赖于PersistenceAPI,这是一个问题吗? 弹簧配置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
{...}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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