使用aspectJ的java拦截静态方法
我正在使用Spring并试图用AspectJ编写示例应用程序。我需要学习如何拦截静态方法调用。在我的示例中,我尝试截取main方法,如下所示:
Spring配置文件:
<aop:aspectj-autoproxy />
<!-- Aspect -->
<bean id="logAspect" class="com.package.Aspect" />
主要方法是:
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Customer.xml");
System.out.println("str");
}
}
协会本身:
@Aspect
public class Aspect {
@Around("execution(*App.main(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Intercepted!");
}
}
但是当我运行应用程序时,只打印了str
字符串
# 1 楼答案
您使用的是在运行时创建代理对象的动态代理方法。此代理对象将继承用于代理目标方法。由于不能继承静态方法,因此这种方法不适用于静态方法
要为
static
方法创建代理,需要使用AspectJ的编译时编织。有关详细信息,请参阅this链接This也可能有帮助