1

我正在尝试使用 Spring AOP 和 AspectJ 创建 Aspect。

@Aspect(value = "perthis(execution(* x.y.z.command.Command.execute()))")
public class CommandAdvice {

     // Advices....
}

'价值属性的 Javadoc 说:-

Per clause expression, defaults to singleton aspect.
Valid values are "" (singleton), "perthis(...)", etc

一切工作文件没有在@Aspect 注释中指定“值”属性。

使用上述 perthis 设置导致以下异常:-

Caused by: java.lang.IllegalArgumentException: Bean with name 'commandAspect' is a singleton, but aspect instantiation model is not singleton.

所以我尝试将建议 bean 的范围更改为原型:-

<bean id="commandAspect" class="x.y.z.command.CommandAdvice" **scope="prototype"**
    factory-method="aspectOf" />

这导致: -

Caused by: java.lang.IllegalArgumentException: Can not set x.y.z.command.Command field x.y.z.test.CommandTest.command to sun.proxy.$Proxy30
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:502)

我的要求是创建“原型”范围的 Aspect bean。

  • 原型Aspect bean是否可取?多线程环境的优缺点是什么?

  • 如何使用该“价值”属性来做到这一点?

4

0 回答 0