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