0

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

环境:

春天 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>

应用程序上下文.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 在这里

4

1 回答 1

0

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

于 2013-01-13T18:58:04.957 回答