Spring组件扫描程序无法从applicationContext.xml运行

时间:2013-01-13 13:50:37

标签: spring spring-mvc

问题是,当我将组件扫描放在applicationContext.xml中时,我无法重新开始,但是如果我将组件扫描放在dispatcher-servlet.xml中,则工作正常

环境:

  

Spring 3.1.3

Web.xml中

 <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/applicationContext.xml 
            /WEB-INF/config/shiro-security.xml
        </param-value>
</context-param>
<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

的applicationContext.xml

<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.keype.hawk" />

调度-servlet.xml中

   <!-- Annotation driven programming model -->
   <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name="objectMapper" ref="customJacksonMapper" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>   

我可以看到弹簧启动时所有的豆子都已被挑选但最终失败了。 在日志中:

INFO :org.springframework.web.context.support.XmlWebApplicationContext[postProcessAfterInitialization]:Bean 'securityManager' of type [class org.apache.shiro.web.mgt.DefaultWebSecurityManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO :org.springframework.web.context.support.XmlWebApplicationContext[postProcessAfterInitialization]:Bean 'authorizationAttributeSourceAdvisor' of type [class org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO :org.springframework.web.context.support.XmlWebApplicationContext[postProcessAfterInitialization]:Bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' of type [class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO :org.springframework.web.context.support.XmlWebApplicationContext[postProcessAfterInitialization]:Bean 'org.springframework.transaction.config.internalTransactionAdvisor' of type [class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO :org.springframework.beans.factory.support.DefaultListableBeanFactory[preInstantiateSingletons]:Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@125e8982: defining beans [org.springframework.aop.config.internalAutoProxyCreator,warehouseStockLevelDaoImpl,transactionsDaoImpl,trackingDaoImpl,warehouseTypeDaoImpl,documentTypeDaoImpl,warehouseProductStockDaoImpl,unitDaoImpl,documentItemDaoImpl,pricingTemplateDaoImpl,documentItemSerialNumbersDaoImpl,supplierContractItemDaoImpl,.. [Huge list. Deleting the rest]org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
INFO :org.springframework.beans.factory.support.DefaultListableBeanFactory[destroySingletons]:Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@125e8982: defining beans [org.springframework.aop.config.internalAutoProxyCreator,warehouseStockLevelDaoImpl,transactionsDaoImpl,trackingDaoImpl,warehouseTypeDaoImpl,documentTypeDaoImpl,warehouseProductStockDaoImpl,unitDaoImpl,documentItemDaoImpl,pricingTemplateDaoImpl,documentItemSerialNumbersDaoImpl,supplierContractItemDaoImpl,productSetDaoImpl,productTypeDaoImpl,productDaoImpl,productPriceListDaoImpl,supplierContractDaoImpl,productBrandDaoImpl,productCategoryDaoImpl,warehouseDa [Huge list. Deleting the rest] org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
INFO :org.springframework.cache.ehcache.EhCacheManagerFactoryBean[destroy]:Shutting down EHCache CacheManager
ERROR:org.springframework.web.context.ContextLoader[initWebApplicationContext]:Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'documentItemService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.keype.hawk.inventory.service.InventoryProductService com.keype.hawk.inventory.service.DocumentItemService.inventoryProductService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inventoryProductService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.keype.hawk.inventory.service.DocumentItemService com.keype.hawk.inventory.service.InventoryProductService.documentItemService; nested exception is java.lang.IllegalArgumentException: Can not set com.keype.hawk.inventory.service.DocumentItemService field com.keype.hawk.inventory.service.InventoryProductService.documentItemService to $Proxy25
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

Spring [preInstantiateSingletons]然后销毁它。为了进行调试,我将以下行移到了dispatcher-servlet.xml

<aop:aspectj-autoproxy proxy-target-class="true"/>
    <context:component-scan base-package="com.keype.hawk" />

问题已经消失,春天正常启动。当组件扫描程序放在applicationContext.xml中时,如何解决上述问题?

- 更新: 完整的调试日志,applicationContext.xml和dispatcher-servlet.xml是here

1 个答案:

答案 0 :(得分:0)

所有服务组件都应该是applicationContext.xml组件扫描的一部分,而控制器应该属于dispatcher-servlet.xml。似乎存在多个相同类型的bean。在这种情况下,自动装配不适合。您可以通过显式声明冲突类型的bean并在xml中自己连接来解决冲突。看看here