0
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DeadlockRetry {
}
拦截器:
public class DeadlockRetryMethodInterceptor implements MethodInterceptor
{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable
{
System.err.println("I've been Intercepted!");
return invocation.proceed();
}
}
截获的类别:
public class Intercepted {
@DeadlockRetry
public void interceptMe()
{
System.err.println("Above here should be a message I've been Intercepted!");
}
}
主起始点:
public class Main {
public static void main(String[] args)
{
//Function that loads the spring-context.xml
SpringContext.init();
Intercepted intercepted = new Intercepted();
intercepted.interceptMe();
}
}
弹簧-context.xml中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
classpath:/org/springframework/transaction/config/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
classpath:/org/springframework/aop/config/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.2.xsd">
<aop:aspectj-autoproxy />
<context:annotation-config/>
<bean id="deadlockRetryAdvice" class="com.metaregistrar.hibernate.DeadlockRetryMethodInterceptor"/>
<bean id="deadlockPointcutAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="deadlockRetryAdvice"/>
<property name="pointcut" ref="deadlockRetryPointcut"/>
</bean>
<bean name="deadlockRetryPointcut" class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut">
<constructor-arg index="0" value="com.metaregistrar.hibernate.DeadlockRetry"/>
<constructor-arg index="1" value="com.metaregistrar.hibernate.DeadlockRetry"/>
</bean>
</beans>
标准错误结果:
以上在这里应该是我已经截获了一个消息!
预计的斯德哥尔德结果:
我被拦截了! 在这里上面应该是我被拦截的消息!
我在做什么错?我一直在这个问题一整天,现在它变得非常讨厌...
我已经搬到远离的Spring AOP和AspectJ的@Aspect retreive实例。确定后,我需要将几乎每个类定义为一个bean进入春天的环境。我觉得有点不必要。 但是,嘿,谢谢你的解决方案。这是这个问题的答案。但不完全是我所追求的。 –