参考:Spring Framework Reference Documentation

   Spring AOP 实现原理与 CGLIB 应用  

   比较分析 Spring AOP 和 AspectJ 之间的差别

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

AspectJ中有两种实现的方式:

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的效果。

最新文章

  1. C语言程序设计第12次作业
  2. ajax载入数据是小细节
  3. BZOJ 1042 硬币购物(完全背包+DP)
  4. Xcode中如何更改Bundle identifier
  5. 10.12_win8风格,把专业书籍当小说看,SQLite
  6. C++Primer第5版学习笔记(四)
  7. 工具类CTools实现字符编码转换和获取当前路径
  8. Anaconda入门安装教程
  9. Spring通过注释配置Bean2 关联关系
  10. 维修数列 Splay(这可能是我写过最麻烦的题之一了。。。用平衡树维护dp。。。丧心病狂啊。。。。)
  11. HDU5909Tree Cutting
  12. jvm常见的面试题
  13. Java -- POI -- 入门使用以及简单介绍
  14. WPF自定义路由事件(二)
  15. G - 看病要排队
  16. tikv性能参数调优
  17. 第一个shell程序
  18. windows7内核分析之x86&amp;x64第二章系统调用
  19. 64位win10系统无法安装.Net framework3.5的两种解决方法
  20. 前端框架 vue 和 react 的区别

热门文章

  1. exsi中使用vSphere客户端复制克隆虚拟机
  2. Sql Server 之游标
  3. document.domain location.hostname location.host
  4. HDU 1024 Max Sum Plus Plus (递推)
  5. php使用Socket实现聊天室功能(书中的代码)
  6. windwos下nginx 配置https并http强制跳转https
  7. load 和 initialize 的区别
  8. 07http基础
  9. DDD领域驱动设计初探(一):聚合
  10. ORM和JDBC