Pointcut not working with Spring AOP

a-sak :

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!!
Scott Bale :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring AOP: pointcut @annotation(MyAnnotation) && call(..) is not triggered as expected

Spring AOP pointcut for all methods in a controller

Spring AOP - pointcut for every method with an annotation

Spring AOP pointcut that matches annotation on interface

Spring aop pointcut expression to access method return type

Spring AOP: Annotation pointcut not causing advice to execute

Spring AOP pointcut with one certain argument

why is this Spring AOP pointcut not triggered?

Avoiding in-place pointcut expression in Spring AOP

Getting error when tried to use target pointcut designator in Spring AOP

Spring AOP pointcut for custom annotation not working inner static class

How to intercept the return type List using the spring AOP pointcut expression

Spring AOP: @annotation() pointcut does not match type annotation

Spring aop: Pointcut defined for subclasses but only one subclass is invoked

Spring AOP Pointcut with method name starting with get

Spring AOP - @Pointcut: @Before advice for @Test methods does not work

Spring AOP, pointcut expressions : annotation with specific param

Spring AOP: Interceptor not working

Can't intercept a pointcut with Spring aop

Java - Spring AOP Pointcut not working

spring AoP, pointcut expression for overloaded methods with same parameter types

Spring AOP Pointcut does not trigger

Spring AOP Annotation not working

Spring AOP pointcut and advice in separate module

spring AOP error at 0 can't find referenced pointcut

Spring Aop Error of formal unbound pointcut

Spring-AOP pointcut not working?

Spring AOP @AfterThrowing not working properly, error at ::0 formal unbound in pointcut

How to test pointcut method in Spring AOP?