問題は、applicationContext.xml 内に component-scanning を配置すると Spring を開始できないことですが、dispatcher-servlet.xml に component-scanning を配置すると正常に動作します。
環境:
春 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" />
dispatcher-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 はこちら