简体   繁体   中英

Spring 3.2 - Project fails to load when @EnableAsync is added to @Configuration class

I need a method of my bean be be executed asynchronously. For that, I added the @Async annotation to my bean method and @EnableAsync on @Configuration annotated class but since then, my project has stopped loading with the following error:

ERROR 2014-12-10 17:03:26 org.springframework.web.context.ContextLoader:331 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.softech.dms.service.DetailService com.softech.dms.config.WebSecurityConfig.detailService; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'detailService': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.softech.dms.service.UserService com.softech.dms.service.DetailService.userService; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.softech.dms.service.CustomerService com.softech.dms.service.impl.UserServiceImpl.customerService; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerServiceImpl': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.softech.dms.service.FolderServicecom.softech.dms.service.impl.CustomerServiceImpl.folderService; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'folderServiceImpl': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.softech.dms.service.BookmarkService com.softech.dms.service.impl.FolderServiceImpl.bookmarkFolderService; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookmarkServiceImpl': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.softech.dms.service.DocumentService com.softech.dms.service.impl.BookmarkServiceImpl.documentService; nested exception is
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'documentServiceImpl': Bean with name 'documentServiceImpl' has been injected into other beans
        [documentHistoryServiceImpl,sharedDocumentServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use
        the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

When I remove @EnableAsync annotation, everything works perfectly again. Please suggest if I am missing something here.

Try putting lazy annotation to the object where you are autowiring. It solved for me

@Autowired
@Lazy(value=true)
MyService servObj;

Try to add default-lazy-init="true" to your xml configuration file .

This article give some directions about the circular dependency problem:

http://java.dzone.com/articles/resolve-circular-dependency

don't use @Lazy(value=true).

There are two important cases for implement @Async annotation.

  1. you should use @Async for public methods.
  2. You should call the public method from another class. You shouldn't call the public method from same class of the public method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM