2013-08-02 118 views
0

註釋:的Spring AOP:攔截器不工作

@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> 

標準錯誤結果:

以上在這裏應該是我已經截獲了一個消息!

預計的斯德哥爾德結果:

我被攔截了! 在這裏上面應該是我被攔截的消息!

我在做什麼錯?我一直在這個問題一整天,現在它變得非常討厭...

回答

1

春季AOP只適用於春豆。你應該在spring配置文件中將截獲的對象定義爲一個bean,從上下文中獲取並在其上調用方法。

這是不正確的部分。

Intercepted intercepted = new Intercepted(); 
intercepted.interceptMe(); 

一下添加到XML文件

然後從春天CTX

ApplicationContext ctx = // create context using the config file 

intercepted = ctx.getBean("intercepted",Intercepted.class); 

intercepted.interceptMe(); 
+0

我已經搬到遠離的Spring AOP和AspectJ的@Aspect retreive實例。確定後,我需要將幾乎每個類定義爲一個bean進入春天的環境。我覺得有點不必要。 但是,嘿,謝謝你的解決方案。這是這個問題的答案。但不完全是我所追求的。 –