一个使用Spring的AspectJ LTW的简单例子

Posted Always_July

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个使用Spring的AspectJ LTW的简单例子相关的知识,希望对你有一定的参考价值。

参考:https://docs.spring.io/spring-framework/docs/4.3.9.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw-first-example

           https://www.ibm.com/developerworks/cn/java/j-lo-springaopcglib/(Spring AOP 实现原理与 CGLIB 应用)

   http://blog.csdn.net/a128953ad/article/details/50509437(比较分析 Spring AOP 和 AspectJ 之间的差别)

Aspect是实现AOP编程的一种具体实现

Aspect中有两种实现的方式:

1.使用Ajc编译器在编译器生成代理类

2.使用AspectJ LTW 在类加载时生成代理

主要的Bean

public class DemoBean {
        public void run() {
            System.out.println("Run");
        }
        public void run1() {
            System.out.println("run1...");
        }
        public void run2() throws Exception {
            TimeUnit.SECONDS.sleep(2);
            System.out.println("run2...");
        }
    }

Aspect

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
@Aspect
public class ProfilingAspect {

    @Around("profileMethod()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch(getClass().getSimpleName());
        try {
            sw.start(pjp.getSignature().getName());
            return pjp.proceed();
        } finally {
            sw.stop();
            System.out.println(sw.prettyPrint());
        }
    }
    @Pointcut("execution(* com.jxufe.study.spring.aspect..*.*(..))")
    public void profileMethod() {

    }

}

META-INF/aop.xml   (这个路径和名字是确定的)

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
    <aspectj>

        <weaver>
            <!-- only weave classes in our application-specific packages -->
            <include within="com.jxufe.study.spring.aspect.*"/>
        </weaver>
        <aspects>
            <!-- weave in just this aspect -->
            <aspect name="com.jxufe.study.spring.aspect.ProfilingAspect"/>
        </aspects>

    </aspectj>

最后的aspect.xml

<?xml version="1.0" encoding="GBK"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <context:load-time-weaver/>
   <bean id="demoBean" class="com.jxufe.study.spring.aspect.DemoBean"></bean>
</beans>

Test类

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


        ApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/aspect.xml", Main.class);
/*
        DemoBean demoBean = (DemoBean) ctx.getBean("de");
*/
        DemoBean demoBean  = new DemoBean();
        demoBean.run();
        demoBean.run1();
        demoBean.run2();

运行时添加 jvm 参数  -javaagent:C:\Users\1\.m2\repository\org\springframework\spring-instrument\4.3.9.RELEASE\spring-instrument-4.3.9.RELEASE.jar

输出结果:

Run
StopWatch ‘ProfilingAspect‘: running time (millis) = 0
-----------------------------------------
ms     %     Task name
-----------------------------------------
00000  ?  run

run1...
StopWatch ‘ProfilingAspect‘: running time (millis) = 1
-----------------------------------------
ms     %     Task name
-----------------------------------------
00001  100%  run1

Disconnected from the target VM, address: ‘127.0.0.1:52489‘, transport: ‘socket‘
run2...
StopWatch ‘ProfilingAspect‘: running time (millis) = 2003
-----------------------------------------
ms     %     Task name
-----------------------------------------
02003  100%  run2

这里的Test类中,这个Bean中使用的是DemoBean demoBean = new DemoBean();但结果却是实现了AOP的效果。

 

以上是关于一个使用Spring的AspectJ LTW的简单例子的主要内容,如果未能解决你的问题,请参考以下文章

AspectJ 与 weblogic

Spring LTW+DDD实现业务数据变化的自动跟踪与回退

无法在非 Spring 项目中使用 aspectJ 拦截器

SpringBoot中使用LoadTimeWeaving技术实现AOP功能

Spring AOP—注解配置方法的使用

Spring 框架的核心技术(五)