Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 a 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) It gives me the following Output

...
Instantiated MyLoggingAspect
did some calculation..
End of the the client invocation!!
like image 414
a-sak Avatar asked Jan 18 '09 18:01

a-sak


People also ask

What is pointcut in Spring AOP?

Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.

What is the difference between JoinPoint and pointcut?

A join point is an individual place where you can execute code with AOP. E.g. "when a method throws an exception". A pointcut is a collection of join points. E.g. "when a method in class Foo throws an exception".

Does Spring AOP use AspectJ?

In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style). Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)


2 Answers

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.

like image 106
Scott Bale Avatar answered Oct 15 '22 07:10

Scott Bale


Scott Bale is right: Let spring instatiate the TixServiceImpl for you. Also in cases like this, enabling Springs logging might help because it tells you how many targets for a aspect/avice were found.

like image 21
tobsen Avatar answered Oct 15 '22 05:10

tobsen