Question

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

Was it helpful?

Solution

Spring AOP only works for spring beans. You should define the intercepted object as a bean in the spring config file, get it from the context and invoke method on it.

This is the part that is not correct.

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

Add this to the xml file

And then retreive the instance from the spring ctx

ApplicationContext ctx = // create context using the config file

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

intercepted.interceptMe();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top