a-sak a-sak - 4 months ago 30
Java Question

Pointcut not working with Spring AOP

In order to implement Logging using Spring AOP I followed these simple steps. But it seems like its not working. Any help would be useful

1) Created MyLoggingAspect class

import org.aspectj.lang.ProceedingJoinPoint;

public class MyLoggingAspect

public MyLoggingAspect() {
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 logging

public class TixServiceImpl implements TixService{

public void calculateSomething() {
String s = "did some calculation..";

public String getTixName() {
return null;

3) Created a spring-aspectj.xml file

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

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();
String s = tix.getTixName();

System.out.println("End of the the client invocation!!");

5) It gives me the following Output


Instantiated MyLoggingAspect

did some calculation..

End of the the client invocation!!


I'm just inspecting your code, but I have a hunch the problem is that you're not getting your TixServiceImpl instance from Spring, but rather you're manually instantiating it yourself in your TixClient. I'm thinking your TixService needs to be a Spring bean, gotten from the Spring ApplicationContext, so that Spring has a chance to set up the aspects on the returned instance.