Pointcut does not work with Spring AOP

To complete the registration using Spring AOP, I followed these simple steps. But it does not seem to work. Any help would be helpful

1) Created a class MyLoggingAspect

import org.aspectj.lang.ProceedingJoinPoint; public class MyLoggingAspect { public MyLoggingAspect() { super(); System.out.println("Instantiated MyLoggingAspect"); } public Object log(ProceedingJoinPoint call) throws Throwable { System.out.println("from logging aspect: entering method [" + call.toShortString() +"] with param:"+call.getArgs()[0] ); Object point = call.proceed(); System.out.println("from logging aspect: exiting method [" + call.toShortString() + "with return as:" +point); return point; } 

}

2) Created a class ( TixServiceImpl ) where I want to keep a log

 public class TixServiceImpl implements TixService{ @Override public void calculateSomething() { String s = "did some calculation.."; System.out.println(s); } @Override public String getTixName() { return null; } } 

3) Created the spring -aspectj.xml file

 <beans... <bean id="LoggingAspect" class = "MyLoggingAspect"/> <aop:config> <aop:aspect ref="LoggingAspect"> <aop:pointcut id="myCutLogging" expression="execution(* TixService*.*(..))"/> <aop:around pointcut-ref="myCutLogging" method="log"/> </aop:aspect> </aop:config> </beans> 

4) Created a simple test client ( TixClient )

 import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class TixClient { public static void main(String[] a){ ApplicationContext context = new FileSystemXmlApplicationContext("conf/spring-aspectj.xml"); TixService tix = new TixServiceImpl(); tix.calculateSomething(); String s = tix.getTixName(); System.out.println("End of the the client invocation!!"); } } 

5) He gives me the next Exit

 ... Instantiated MyLoggingAspect did some calculation.. End of the the client invocation!! 
+4
source share
2 answers

I am just checking your code, but I have a suspicion that the problem is not that you are not getting your TixServiceImpl instance from Spring, but rather manually creating it in your TixClient . I think your TixService should be a Spring bean derived from Application> Spring so that Spring has the ability to customize aspects of the returned instance.

+14
source

Scott Bale is right: let spring install TixServiceImpl for you. Also in such cases, including a Springs log entry may help, as it tells you how many targets for aspect / avice were found.

+2
source

All Articles