0

Issue is, I cannot start spring when I put the component-scanning inside the applicationContext.xml, but works fine if I put component-scanning in the dispatcher-servlet.xml

Environment:

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" />

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>   

I can see all the beans have been picked when the spring boots up but eventually fails. In the log:

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] and then destroys it. For debugging, I moved the below lines to dispatcher-servlet.xml

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

The problem is gone and spring boots up normally. How can I fix the above issue when the component scanner is put in the applicationContext.xml?

--Update: Full debug logs, applicationContext.xml and dispatcher-servlet.xml is here

Firdous Amir
  • 1,297
  • 5
  • 21
  • 39

1 Answers1

0

All the Service components should be part of applicationContext.xml's component scan whereas the controllers should belong to dispatcher-servlet.xml. It seems that more than one bean of the same type exist. In such cases, Autowiring is not suitable. You can resolve the conflict by explicitly declare a bean of the conflicting type and wire it by yourself in the xml. Have a look here.

Community
  • 1
  • 1
Harshal Waghmare
  • 1,944
  • 2
  • 20
  • 21
  • Good info and thanks for the link. However, I don't use interfaces here. I simply use classes as proxy, and no duplicates. Also, how the code if the I put into the dispatcher configuration file? Any idea? – Firdous Amir Jan 14 '13 at 00:01
  • Since spring is destroying singletons, there might be some error occurring in the start-up which is not getting printed in logs. Make your logging to debug level to find any potential problem. Although why there is no problem when scan is in dispatcher-servlet.xml is baffling even to me. – Harshal Waghmare Jan 14 '13 at 16:14
  • Thanks Harshal. Will definitely follow your direction. I'm not really proficient in reading Spring debugs, If I post it, would you be able to help and see what's going on ? – Firdous Amir Jan 14 '13 at 21:32
  • Please post the logs. Of course cant guarantee a solution but this issue is getting interesting for me too. – Harshal Waghmare Jan 15 '13 at 07:46
  • Thanks. I have uploaded the logs and configuration files. Please have a look. The beans in trouble [DocumentItemService and InventoryProductService] are autowired between each other. That could be a possibility. I tried refactoring by remove the @Autowire DocumentItemService from InventoryProductService, still no luck. Yet again, still it doesn't answer "WHY" it works in the dispatcher config !! – Firdous Amir Jan 15 '13 at 09:25
  • Sorry for late response. I went through the logs but it's very difficult to pin-point the problem but have a relook at auditLogDao bean and its dependencies. That bean seems problematic in the logs. In general, the applicationContext.xml is the parent context to dispatcher-servlet. And all the beans defined in the parent can be accessed by the child. So both the context's aren't strictly equivalent. I know this is not the anwser you've been looking for. Hope you already found the problem. If it is so please share :) – Harshal Waghmare Jan 18 '13 at 19:14
  • You are very kind. The problem is still open and I'll sure follow your guidelines here. Will let you know when I the fix. Thank you very much Harshal for your time :-) – Firdous Amir Jan 18 '13 at 22:24