2013-01-13 54 views
0

問題是,當我將組件掃描放到applicationContext.xml中時,我無法啓動Spring,但如果將組件掃描放在dispatcher-servlet.xml中,則工作正常Spring Application Component Scanner not application from applicationContext.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> 

的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.s[email protected]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.s[email protected]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) 

春天[的preInstantiateSingletons],然後摧毀它。爲了調試,我將下面的代碼移到了調度程序-servlet.xml中

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

問題沒有了,春天正常啓動。如何將組件掃描程序放入applicationContext.xml中時解決上述問題?

--update: 全調試日誌applicationContext.xml中和調度員servlet.xml中是here

回答

0

所有服務組件應該是applicationContext.xml的部件掃描的一部分,而控制器應該屬於dispatcher-servlet.xml。似乎存在多於一個相同類型的bean。在這種情況下,Autowiring不適合。你可以通過顯式聲明一個衝突類型的bean來解決這個衝突,並通過你自己在xml中進行連接。看看here

+0

良好的信息和感謝您的鏈接。但是,我不在這裏使用接口。我只是使用類作爲代理,並沒有重複。另外,如果我將代碼放入調度程序配置文件,代碼如何?任何想法? –

+0

由於春季正在破壞單身人士,因此在啓動時可能會出現一些錯誤,這些錯誤並未在日誌中打印。使您的日誌記錄調試級別以查找任何潛在的問題。雖然在dispatcher-servlet.xml中進行掃描時,爲什麼沒有問題,甚至對我來說也是莫名其妙。 –

+0

謝謝Harshal。一定會按照你的方向。我不太熟練閱讀Spring調試,如果我發佈它,你是否能夠幫助並看看發生了什麼? –

相關問題