4

私の Spring Web アプリケーションでは、perthis スコープの AspectJ アスペクトにリソースを注入しようとしています。インジェクションは、シングルトン アスペクトを使用するとうまく機能しますが、perthis スコープのアスペクトを使用すると失敗します。@Autowire注釈またはxml<property>タグの両方を使用してみました。

これが私のコードです。

アスペクト( @Configurable の場合、 と の両方を試しましたautowire=Autowire.BY_TYPEautowire=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");
    }
}

共通サービス:

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

および Bean の 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" />

そして、commonService が自動配線されていないことを確認する出力:

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

すべての可能性を試したため、一部の構成/注釈がオーバーロードされている可能性があります (たとえば、xml で@Scope("prototype")との両方を使用しています)。scope="prototype"

ただし、シングルトン スコープを設定し、perthis を削除 (および CommonService もシングルトン スコープにする) すると、フィールドは正しく自動配線されます。

どんな助けでも大歓迎です。

編集:

scope="prototype"TestAspect xml Bean 定義から削除すると、次の例外が発生します。

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)

TestAspect クラスは で注釈されて@Scope("protptype")いますが、Bean はシングルトンのように認識され、エラーが発生します。コンテナは @Scope アノテーションを考慮していないようですが、他のすべての Bean では機能します。も検出@Configurableされず、自動配線エラーが発生する可能性がありますか?

4

0 に答える 0