Spring - Deploying resources in terms of scope (perthis-pertarget)

In my Spring web application, I'm trying to embed a resource in an aspect of AspectJ. Injection works well using the singleton aspect, but does not work using one of them. I tried using the @Autowire or xml <property> .

Here is my code.

Aspect (for @Configurable I tried both autowire=Autowire.BY_TYPE and autowire=Autowire.BY_NAME ):

 @Aspect( "perthis(org.mypackage.aop.aspects.TestAspect.participateAroundPointcut())") @Component @Scope("prototype") @Configurable() public class TestAspect { private static final Logger logger = LoggerFactory.getLogger(TestAspect.class); @Autowired private CommonService commonService; @Pointcut("execution(* org.mypackage.TestService.method(..))") public void participateAroundPointcut(){} @Around("participateAroundPointcut()") public void testAround(ProceedingJoinPoint joinPoint) throws Throwable{ logger.debug("Pre-execution; commonService==null: "+(commonService==null)+";"); joinPoint.proceed(); logger.debug("Post-execution"); } } 

CommonService:

 @Service @Scope("request") public class CommonService { private String value = "commonValue"; } 

and beans' xml:

 <context:component-scan base-package="org.mypackage" /> <aop:aspectj-autoproxy proxy-target-class="true"/> <context:spring-configured /> <bean id="testAspect" class="org.mypackage.aop.aspects.TestAspect" scope="prototype" factory-method="aspectOf"/> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 

And the output confirming that commonService is not auto-sensing:

 DEBUG: org.mypackage.aop.aspects.TestAspect - Pre-execution; commonService==null: true; DEBUG: org.mypackage.TestService - Executing method DEBUG: org.mypackage.aop.aspects.TestAspect - Post-execution 

Some configuration / annotation may be overloaded (for example, I use both @Scope("prototype") and scope="prototype" in xml) because I tried all the features.

However, by setting the Singleton scope and removing perthis (and making the CommonService singleton-scoped too), the field is correctly autodetected.

Any help would be appreciated.

EDIT:

Removing scope="prototype" from the TestAspect xml bean definition I get the following exception:

 java.lang.IllegalArgumentException: Bean with name 'testAspect' is a singleton, but aspect instantiation model is not singleton at org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder.buildAspectJAdvisors(BeanFactoryAspectJAdvisorsBuilder.java:121) at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:86) at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:107) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:275) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:894) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:866) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:451) 

Although the TestAspect class is annotated with @Scope("protptype") , the bean is recognized as a singleton, causing an error. It seems that the container does not respect the @Scope annotation, while it works for all other beans. Maybe @Configurable also not detected, causing an auto-device error?

+4
source share

All Articles