我有一个Spring Web应用程序,我正在尝试为服务对象添加一些方面。目标是仅通过单个请求范围维护方面的状态,并获取对方面实例的引用,以便我可以管理状态。我尝试了3种不同版本的代码,通过控制器发出请求:
@Autowired TestAspect aspect
实例与AOP框架的使用不同。factory-method="aspectOf"
,状态保留为先前的情况,@Autowired TestAspect aspect
实例与AOP框架使用的相同。虽然它有效,但我希望aspect的范围是单个请求,而在这种情况下我有一个应用程序范围的单例。@Aspect
代替@Aspect("perthis(participateAroundPointcut())")
我收到以下例外情况:Caused by: java.lang.IllegalArgumentException: Bean with name 'testAspect' is a singleton, but aspect instantiation model is not singleton
,factory-method="aspectOf"
或{}}。如何将@Autowired TestAspect aspect
引用作为AOP框架使用的相同方面实例? factory-method="aspectOf"
是唯一的一种方式吗?
我怎么能有一个请求范围的方面而不是单身?为什么我得到例外?
这是我的代码。 服务:
@Service
public class TestService {
@Autowired
private TestAspect aspect;
private static final Logger logger = LoggerFactory.getLogger(TestService.class);
public void method(){
logger.debug("Executing method");
}
public void service(){
aspect.initialize();
method();
}
}
看点:(没有("perthis(participateAroundPointcut())")
)
@Aspect
public class TestAspect {
private static final Logger logger = LoggerFactory.getLogger(ParticipatoryAspect.class);
private boolean initialized=false;
@Pointcut("execution(* org.mose.emergencyalert.TestService.method(..))")
public void participateAroundPointcut(){}
@Around("participateAroundPointcut()")
public void participateAround(ProceedingJoinPoint joinPoint) throws Throwable{
logger.debug("Pre-execution; Initialized: "+initialized);
joinPoint.proceed();
logger.debug("Post-execution");
}
public void initialize(){
this.initialized=true;
logger.debug("Initialized: "+initialized);
}
}
beans-context.xml(不含factory-method="aspectOf"
):
<aop:aspectj-autoproxy />
<bean id="testAspect" class="org.mose.emergencyalert.aop.aspects.TestAspect"/>