我在将方面应用到 ServiceFactoryBean-created(ServiceLoader-loaded)bean 时遇到问题。
我的 spring 上下文看起来像这样:
...
<aop:aspectj-autoproxy/>
<bean id="myBean"
class="org.springframework.beans.factory.serviceloader.ServiceFactoryBean"
p:serviceType="com.test.MyInterface"/>
<bean id="myAnotherBean"
class="com.test.MyAnotherBean"/>
我的方面定义如下:
@Aspect
public class MyAspect {
@Pointcut("bean(myBean)")
public void foo() {
}
@Pointcut("bean(myAnotherBean)")
public void bar() {
}
@Around("foo()")
public Object doFoo(final ProceedingJoinPoint jp) throws Throwable {
return timed(jp, "foo");
}
@Around("bar()")
public Object doBar(final ProceedingJoinPoint jp) throws Throwable {
return timed(jp, "bar");
}
private ThreadLocal<StopWatch> stopWatch = new ThreadLocal<StopWatch>();
private Object timed(final ProceedingJoinPoint jp, final String taskName) throws Throwable {
stopWatch.get().add(taskName);
try {
return jp.proceed();
} finally {
if (stopWatch.get() != null) {
stopWatch.get().stop(taskName);
}
}
}
}
出于某种未知原因,方面仅应用于 myAnotherBean 的调用,而不应用于 myBean 的调用。有任何想法吗 ?