第十讲-AOP实现之agent(类加载阶段增强)

第十讲-AOP实现之agent(类加载增强)

本讲来讲解使用agent类加载阶段实现功能增强。如下面的代码:

在类加载时使用JVM参数,注意后面的aspectjweaver-1.9.7.jar改成自己的路径:

-javaagent:D:/development_tools/mvn_repository/org/aspectj/aspectjweaver/1.9.7/aspectjweaver-1.9.7.jar

image-20240726190819090

紧接着,在resources目录下建立一个META_INF目录,并在目录下添加aop.xml文件,文件内容为下:

<aspectj>
    <aspects>
        <aspect name="com.cherry.chapter2.a09.aop.MyAspect"/>	// AOP的全限定路径名(改成自己的)
        <weaver options="-verbose -showWeaveInfo">				// 固定的
            <include within="com.cherry.chapter2.a09.MyService"/> // 目标类的全限定路径名(改成自己的)
            <include within="com.cherry.chapter2.a09.aop.MyAspect"/> // AOP的全限定路径名(改成自己的)
        </weaver>
    </aspects>
</aspectj>

运行测试:

[AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.9.7 built on Thursday Jun 24, 2021 at 16:14:45 PDT
[AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2
[AppClassLoader@18b4aac2] info using configuration /D:/javaProject/spring-analyse/chapter2/target/classes/META-INF/aop.xml
[AppClassLoader@18b4aac2] info register aspect com.cherry.chapter2.a09.aop.MyAspect
[AppClassLoader@18b4aac2] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.5)

2024-07-26 19:14:41.494  INFO 812 --- [           main] com.cherry.chapter2.a09.A10Application   : Starting A10Application using Java 1.8.0_202 on DESKTOP-9UHTRF7 with PID 812 (D:\javaProject\spring-analyse\chapter2\target\classes started by yihaoshen in D:\javaProject\spring-analyse)
2024-07-26 19:14:41.499 DEBUG 812 --- [           main] com.cherry.chapter2.a09.A10Application   : Running with Spring Boot v2.5.5, Spring v5.3.10
2024-07-26 19:14:41.499  INFO 812 --- [           main] com.cherry.chapter2.a09.A10Application   : No active profile set, falling back to default profiles: default
2024-07-26 19:14:42.906  INFO 812 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2024-07-26 19:14:42.920  INFO 812 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-07-26 19:14:42.920  INFO 812 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.53]
2024-07-26 19:14:43.027  INFO 812 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-07-26 19:14:43.027  INFO 812 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1391 ms
[MethodUtil@5ff8e049] info AspectJ Weaver Version 1.9.7 built on Thursday Jun 24, 2021 at 16:14:45 PDT
[MethodUtil@5ff8e049] info register classloader sun.reflect.misc.MethodUtil@5ff8e049
[MethodUtil@5ff8e049] info using configuration /D:/javaProject/spring-analyse/chapter2/target/classes/META-INF/aop.xml
[MethodUtil@5ff8e049] info register aspect com.cherry.chapter2.a09.aop.MyAspect
2024-07-26 19:14:43.646  WARN 812 --- [           main] o.s.b.a.f.FreeMarkerAutoConfiguration    : Cannot find template location(s): [classpath:/templates/] (please add some templates, check your FreeMarker configuration, or set spring.freemarker.checkTemplateLocation=false)
2024-07-26 19:14:43.707  INFO 812 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2024-07-26 19:14:43.721  INFO 812 --- [           main] com.cherry.chapter2.a09.A10Application   : Started A10Application in 3.082 seconds (JVM running for 5.907)
[AppClassLoader@18b4aac2] weaveinfo Join point 'method-execution(void com.cherry.chapter2.a09.MyService.foo())' in Type 'com.cherry.chapter2.a09.MyService' (MyService.java:11) advised by before advice from 'com.cherry.chapter2.a09.aop.MyAspect' (MyAspect.java)
[AppClassLoader@18b4aac2] weaveinfo Join point 'method-execution(void com.cherry.chapter2.a09.MyService.bar())' in Type 'com.cherry.chapter2.a09.MyService' (MyService.java:16) advised by before advice from 'com.cherry.chapter2.a09.aop.MyAspect' (MyAspect.java)
2024-07-26 19:14:43.961 DEBUG 812 --- [           main] com.cherry.chapter2.a09.A10Application   : service class:class com.cherry.chapter2.a09.MyService
2024-07-26 19:14:43.971 DEBUG 812 --- [           main] com.cherry.chapter2.a09.MyService        : before()
2024-07-26 19:14:43.971 DEBUG 812 --- [           main] com.cherry.chapter2.a09.MyService        : foo()
2024-07-26 19:14:43.971 DEBUG 812 --- [           main] com.cherry.chapter2.a09.MyService        : before()
2024-07-26 19:14:43.971 DEBUG 812 --- [           main] com.cherry.chapter2.a09.MyService        : bar()
[TomcatEmbeddedWebappClassLoader@5bdaf2ce] info AspectJ Weaver Version 1.9.7 built on Thursday Jun 24, 2021 at 16:14:45 PDT
[TomcatEmbeddedWebappClassLoader@5bdaf2ce] info register classloader org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@5bdaf2ce
[TomcatEmbeddedWebappClassLoader@5bdaf2ce] info using configuration /D:/javaProject/spring-analyse/chapter2/target/classes/META-INF/aop.xml
[TomcatEmbeddedWebappClassLoader@5bdaf2ce] info register aspect com.cherry.chapter2.a09.aop.MyAspect

Process finished with exit code 0

我们发现,使用agent在类加载阶段实现了目标来的功能增强,同样,使用agent增强也突破了代理类的限制。同样的,我们可以使用阿里巴巴提供的阿尔萨斯工具来看一下生成的目标类(这里我们暂时注释掉context.close()):

image-20240726193040574

我们发现,使用agent在类加载阶段对目标方法进行增强其实和AspectJ原理类似,是在类加载阶段在方法处插入了通知方法。

posted @ 2024-07-26 19:36  Cherry_Shen  阅读(50)  评论(0)    收藏  举报