3
想象一下,你有一個方面:簡單使用的AspectJ
@Aspect
public class MyAspect {
@Pointcut("whatever")
public void anyAnnotation() {}
@Before("anyAnnotation()")
public void anyComponentAdvice(JoinPoint joinPoint) {
System.out.println("DONE");
}
}
而且你有一個簡單的應用程序:
public class MyClass {
public static void main(String[] args) {
// ASPECTJ INITIALIZATION BLOCK
// rest of the code
}
}
你需要什麼代碼放入「AspectJ的初始化阻止「應用程序自動識別和應用方面?使用Spring AOP等一些框架,你可以使用<aop:aspectj-autoproxy/>
,但是我不知道如何在沒有Spring的情況下做同樣的事情。
編輯:其他更簡單的方法來攔截執行時方法調用,沒有方面,是值得歡迎的。
我不認爲這就是AspectJ的工作原理。根據我的記憶,它通過修改字節碼作爲構建步驟或加載類時將各個方面「編織」到現有代碼中。這兩者都不能以直接的方式從'main()'方法完成。 – millimoose
(非直截了當的方式涉及使用文檔中提到的自定義類加載器加載應用程序代碼:http://www.eclipse.org/aspectj/doc/released/devguide/ltw-configuration.html#d0e2867) – millimoose
@Inerdia感謝您的評論,您是否知道任何其他方式/庫來攔截方法調用? (任何類型的代理...)在執行時? –