Autowired does not happen for bean using @Async method

I try an Autowire bean that contains the @Async method, but it did not pass with the exception Msgstr "Injection of autodetected dependencies failed"

My servlet.xml contains an ad

<task:annotation-driven executor="executor" /> <task:executor id="executor" pool-size="7"/> 

Can I get some help why autwire fails?

--- UPDATE --- Here is the stop error code.

"cancelAppointment" is the bean identifier where Autowire runs for the MailService bean

 2013-08-14 17:06:46,488|main|DEBUG|org.springframework.beans.factory.support.DisposableBeanAdapter|Invoking destroy() on bean with name 'org.springframework.scheduling.annotation.internalScheduledAnnotationProcessor' 2013-08-14 17:06:46,488|main|DEBUG|org.springframework.beans.factory.support.DisposableBeanAdapter|Invoking destroy() on bean with name 'executor' 2013-08-14 17:06:46,488|main|INFO|org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor|Shutting down ExecutorService 2013-08-14 17:06:46,492|main|ERROR|org.springframework.web.context.ContextLoader|Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cancelAppointment': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ge.appl.service.fs.manager.email.MailService com.ge.appl.service.fs.manager.cancel.consumer.CancelAppointmentImpl.mailService; nested exception is java.lang.IllegalArgumentException: Can not set com.ge.appl.service.fs.manager.email.MailService field com.ge.appl.service.fs.manager.cancel.consumer.CancelAppointmentImpl.mailService to $Proxy35 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) 

------ UPDATE 2 ------------ Here are a few more details of the stack trace

 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ge.appl.service.fs.manager.email.MailService com.ge.appl.service.fs.manager.cancel.consumer.CancelAppointmentImpl.mailService; nested exception is java.lang.IllegalArgumentException: Can not set com.ge.appl.service.fs.manager.email.MailService field com.ge.appl.service.fs.manager.cancel.consumer.CancelAppointmentImpl.mailService to $Proxy35 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282) ... 28 more Caused by: java.lang.IllegalArgumentException: Can not set com.ge.appl.service.fs.manager.email.MailService field com.ge.appl.service.fs.manager.cancel.consumer.CancelAppointmentImpl.mailService to $Proxy35 at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63) at java.lang.reflect.Field.set(Field.java:657) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:498) ... 30 more 

The class in which autwire is running looks like

 @Component("cancelAppointment") public class CancelAppointmentImpl extends AbstractCancelAppointment implements CancelAppointment { @Autowired private MailService mailService; @Override public CancelAppointmentCriteria cancelAppointment( final CancelAppointmentCriteria criteria) throws Exception { // do something try { mailService.sendMail(); } catch (Exception e) { // handle exception } 

}}

+4
source share
1 answer

I ran into the same problem, I think that @EnableAsync converts any component using @Async to a proxy server that implements the same interfaces used by this class of components, so when autwire with a particular class encounters a type conflict between that particular class and proxy, since working around you can use autwire on the interface and use @Qualifier to specify a bean.

+5
source

Source: https://habr.com/ru/post/1496999/


All Articles