码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-11-06 15:32:06      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:system   frame   org   tty   tor   127.0.0.1   dem   context   profile   

参考: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的简单例子

标签:system   frame   org   tty   tor   127.0.0.1   dem   context   profile   

原文地址:http://www.cnblogs.com/alway-july/p/7793326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!