1

I'm using Spring and trying to write my sample application with AspectJ. I need to learn how to intercept static method calls. In my example I'm trying to intercept the main method as follows:

Spring configuration file:

<aop:aspectj-autoproxy />

<!-- Aspect -->
<bean id="logAspect" class="com.package.Aspect" />

The main method:

public class App {
    public static void main(String[] args) throws Exception {

        ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Customer.xml");

        System.out.println("str");
    }
}

The asspect itself:

@Aspect
public class Aspect {
    @Around("execution(*App.main(..))")
    public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Intercepted!");
    }

}

But when I run the application, the only str string is being printed.

St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

4

You are using dynamic proxy approach where proxy object is created at runtime. This proxy object uses inheritance for proxying target methods. Since you can not inherit a static method, this approach wont work for static methods.

For creating proxy for static methods you need to use AspectJ's compile time weaving. You can refer this link for more info. This might also help.

justAbit
  • 4,226
  • 2
  • 19
  • 34