0

I am writing my first AOP. I have pasted the code below which isnt getting intercepted on the method call. I am not sure what could be the reason.

On the console it only prints:

addCustomerAround() is running, args : dummy

and none of the AOP advice code is printed.

Can somebody help?

Interface:

package com.test.model; import org.springframework.beans.factory.annotation.Autowired; public interface AopInterface { @Autowired void addCustomerAround(String name); } 

Class:

package com.test.model; import com.test.model.AopInterface; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired; @Component public class AopClass implements AopInterface { public void addCustomerAround(String name){ System.out.println("addCustomerAround() is running, args : " + name); } } 

AOP:

package com.test.model; import java.util.Arrays; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class TestAdvice{ @Around("execution(* com.test.model.AopInterface.addCustomerAround(..))") public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("testAdvice() is running!"); System.out.println("hijacked method : " + joinPoint.getSignature().getName()); System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs())); System.out.println("Around before is running!"); joinPoint.proceed(); System.out.println("Around after is running!"); System.out.println("******"); } } 

appcontext.xml:

<!-- Aspect --> <aop:aspectj-autoproxy /> <bean id="AopClass" class="com.test.model.AopClass" /> <bean id="TestAdvice" class="com.test.model.TestAdvice" /> 

POM:

<!-- AOP --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> 

Method call:

aoptest.addCustomerAround("dummy"); 
1
  • Where do you get the reference to your AopClass instance before doing the method call? Has it been wired by Spring? Commented Sep 25, 2015 at 6:58

2 Answers 2

1

I don't see any error here. I've just tried your code and it works well. My test class:

public class App { public static void main(String[] args) throws Exception { ApplicationContext appContext = new ClassPathXmlApplicationContext("appcontext.xml"); //------------------------- AopClass aopClass = (AopClass) appContext.getBean("AopClass"); aopClass.addCustomerAround("dummy"); } } 

enter image description here Result

Can you retry to build your maven project?! And try again?!

Sign up to request clarification or add additional context in comments.

2 Comments

Did you make any changes to appcontext.xml ?
No, just had to re-build and it worked. Silly me :-/
1

According to Aspect Oriented Programming with Spring, when a class implements interface(s), a JDK dynamic proxy will be used and if no interface is implemented then a CGLIB proxy will be created. To force CGLIB proxy you have to set proxy-target-class="true".

appcontext.xml:

<!-- Aspect --> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> <bean id="AopClass" class="com.test.model.AopClass" /> <bean id="TestAdvice" class="com.test.model.TestAdvice" /> 

Output :

testAdvice() is running! hijacked method : addCustomerAround hijacked arguments : [dummy] Around before is running! addCustomerAround() is running, args : dummy Around after is running! ****** 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.