一个使用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的效果。

时间: 2024-08-03 17:01:44

一个使用Spring的AspectJ LTW的简单例子的相关文章

spring aop 和 ioc的简单例子

aop http://blog.csdn.net/wangpeng047/article/details/8560694 ioc http://blog.csdn.net/baple/article/details/39895239

Spring结合AspectJ的研究

本文阐述以下内容:1.AspectJ是什么及使用方式2.Spring AOP和AspectJ的区别3.Spring结合AspectJ的使用方法和原理4.Spring注解方式使用AspectJ遇到的问题5.总结 一.AspectJ是什么 提到面向切面编程(AOP,Aspect Oriented Programming),大家首先想到的是Spring AOP,或许有人也会想到AspectJ,也有人搞不清楚这两者的区别.AOP是一种编程思想,AOP的作用是在不修改源程序的情况下修改源程序的动态执行流和

关于 Spring AOP (AspectJ) 你该知晓的一切

[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54629058 出自[zejian的博客] 关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容

Spring基础系列12 -- Spring AOP AspectJ

Spring基础系列12 -- Spring AOP AspectJ 转载:http://www.cnblogs.com/leiOOlei/p/3613352.html 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某方法. Advisor:Advice和Pointcut的结合单元,以便将Advice和Pointcut分开实现灵活配置. Aspe

Spring @AspectJ 实现AOP 入门例子(转)

AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): Java代码   package com.samter.common; /** * 猴子 * @author Administrator * */ public class Monkey { public void stealPeaches(String name){ System.out.println("[猴子]"+name+&quo

(转)Spring使用AspectJ进行AOP的开发:注解方式

http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Spring使用AspectJ通过配置文件的方式来进行AOP的开发,现在就来讲怎样使用注解方式进行AOP的开发. 创建一个Web项目, 引入相关的jar包.所要导入的jar包如下:  引入Spring的配置文件.主要引入AOP的约束: <?xml version="1.0" encoding=

Spring AOP AspectJ 代码实例

本文参考来源 http://examples.javacodegeeks.com/enterprise-java/spring/aop/spring-aop-aspectj-example/http://oss.org.cn/ossdocs/framework/spring/zh-cn/aop.html 概念 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层次的对象,而AOP将程序分解成各个方面 或者说 关注点 . 这使得可

关于 Spring AOP (AspectJ) 该知晓的一切

关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容,尽力呈现出简单易懂的文字含义,如文中有错误请留言,谢谢. OOP的新生机 OOP新生机前夕 神一样的AspectJ-AOP的领跑者 AspectJ的织入方式及其原理概要 基于Aspect Spring AOP

Spring注入aspectJ切面

1.用关键字aspect即可声明一个切面如:                    切面定义完毕,可以发现,pointcut被当做一个类型,指定切点还是用execution表达式:after()和returning()也被当做一个类型来声明一个通知 2.在JudgeAspect中有一个CriticismEngine类型的成员变量,为了实现对象间的松耦合,我们在aspectJ中使用Spring的依赖注入来为JudgeAspect注入CriticismEngine对象.首先将CriticismEng