项目作者: pankajsinghal

项目描述 :
Aspect Oriented Programming(AOP) Load Time Weaving(LTW) Example
高级语言: Java
项目地址: git://github.com/pankajsinghal/aop-example.git
创建时间: 2018-08-05T07:12:55Z
项目社区:https://github.com/pankajsinghal/aop-example

开源协议:

下载


AOP Examples

Working examples of compile time weaving(CTW) & load time weaving(LTW) in AOP

AOP CTW

Aspect Oriented Programming (AOP) Compile Time Weaving (CTW) Example

For compile time weaving, you need to add aspectj-maven-plugin into your pom.
This plugin weaves the aspects to the class with the aspect annotation while compiling the code.

Steps to run:

  1. $ cd aop-example
  2. $ mvn clean install
  3. $ cd aop-ctw/target
  4. $ java -classpath aop-ctw-1.0.jar:deploy/lib/* com.aop.ctw.Main
  5. Loading
  6. 16:53:42.062 [main] DEBUG com.aop.lib.WrapDef - before wrap
  7. 16:53:42.064 [main] DEBUG com.aop.ctw.Main - inside runCTWAopDemo
  8. 16:53:42.064 [main] DEBUG com.aop.lib.WrapDef - after wrap

AOP LTW

Aspect Oriented Programming (AOP) Load Time Weaving (LTW) Example

AOP LTW example with javaagent as cmd line arg

Notice that in aop-app/pom.xml, there is only a dependency of aop-lib
& no other configuration related to AOP is required.

Steps to run with javaagent as cmd line arg.:

  1. $ cd aop-example
  2. $ mvn clean install
  3. $ cd aop-ltw/target
  4. $ java -javaagent:deploy/lib/aspectjweaver-1.9.1.jar -classpath aop-ltw-1.0.jar:deploy/lib/* com.aop.ltw.Main
  5. Loading
  6. 16:21:34.453 [main] DEBUG com.aop.lib.WrapDef - before wrap
  7. 16:21:34.459 [main] DEBUG com.aop.ltw.AOPDemo - inside runAopDemo
  8. 16:21:34.459 [main] DEBUG com.aop.lib.WrapDef - after wrap

AOP LTW example with Dynamic loading of javaagent

Refer - https://www.eclipse.org/aspectj/doc/released/README-187.html

Steps to run with dynamic loading of javaagent:

  1. $ cd aop-example
  2. $ mvn clean install
  3. $ cd aop-ltw/target
  4. $ java -DdynamicLoad=true -DAGENT_PATH=deploy/lib/aspectjweaver-1.9.1.jar -classpath aop-ltw-1.0.jar:deploy/lib/* com.aop.ltw.Main
  5. WrapDef Loaded : false
  6. java.lang.UnsupportedOperationException: AspectJ weaving agent was neither started via '-javaagent' (preMain) nor attached via 'VirtualMachine.loadAgent' (agentMain)
  7. loading javaAgent deploy/lib/aspectjweaver-1.9.1.jar
  8. loaded javaAgent deploy/lib/aspectjweaver-1.9.1.jar
  9. WrapDef Loaded : false
  10. Loading
  11. 16:20:27.904 [main] DEBUG com.aop.lib.WrapDef - before wrap
  12. 16:20:27.912 [main] DEBUG com.aop.ltw.AOPDemo - inside runAopDemo
  13. 16:20:27.912 [main] DEBUG com.aop.lib.WrapDef - after wrap
References:
Note:

If you are coming via the AspectJ AOP LTW not working with dynamic loading of javaagent question, please note that I’ve corrected the code in this repo & it’s working now.