我正在尝试使用Spring AOP和AspectJ创建Aspect。
@Aspect(value = "perthis(execution(* x.y.z.command.Command.execute()))")
public class CommandAdvice {
// Advices....
}
'atatbute'的Javadoc说: -
Per clause expression, defaults to singleton aspect.
Valid values are "" (singleton), "perthis(...)", etc
Everything工作文件而不指定@Aspect注释中的'value'属性。
使用上述相关设置导致以下异常: -
Caused by: java.lang.IllegalArgumentException: Bean with name 'commandAspect' is a singleton, but aspect instantiation model is not singleton.
所以我尝试将advice 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)
我的要求是创建'prototype'范围的Aspect bean。
原型Aspect bean是否可取?多线程环境中的优点和缺点是什么?
如何使用'value'属性进行操作?