0

The annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DeadlockRetry {


}

The Interceptor:

public class DeadlockRetryMethodInterceptor implements MethodInterceptor
{

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable
    {
        System.err.println("I've been Intercepted!");
        return invocation.proceed();
    }
}

The Intercepted Class:

public class Intercepted {


    @DeadlockRetry
    public void interceptMe()
    {
        System.err.println("Above here should be a message I've been Intercepted!");
    }
}

The Main startpoint:

public class Main {
    public static void main(String[] args)
    {
        //Function that loads the spring-context.xml
        SpringContext.init();

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

The spring-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>

The Stderr result:

Above here should be a message I've been Intercepted!

The expected Stderr result:

I've been Intercepted! Above here should be a message I've been Intercepted!

What am I doing wrong? I've been at this problem for the whole day now and It's getting pretty annoying...

4

1 回答 1

1

Spring AOP 仅适用于 spring bean。您应该在 spring 配置文件中将拦截的对象定义为 bean,从上下文中获取它并在其上调用方法。

这是不正确的部分。

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

将此添加到 xml 文件中

然后从 spring ctx 中检索实例

ApplicationContext ctx = // create context using the config file

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

intercepted.interceptMe();
于 2013-08-02T15:59:54.213 回答