一个使用Spring的AspectJ LTW的简单例子
参考: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的效果。
一个使用Spring的AspectJ LTW的简单例子的更多相关文章
- 一个基于MINA框架应用的最简单例子
直接上代码.关于原理和主要的API以后在说.先能跑通了在说. 主要的包:mina-core-2.0.0.jar[到官网上下载完整项目包里面还有文档和依赖包],jcl-over-slf4j-1.5.11 ...
- 10 Spring框架 AOP (三) Spring对AspectJ的整合
上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...
- Spring结合AspectJ的研究
本文阐述以下内容:1.AspectJ是什么及使用方式2.Spring AOP和AspectJ的区别3.Spring结合AspectJ的使用方法和原理4.Spring注解方式使用AspectJ遇到的问题 ...
- Spring 自定义注解,配置简单日志注解
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...
- 关于 Spring AOP (AspectJ) 该知晓的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...
- (转)Spring使用AspectJ进行AOP的开发:注解方式
http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...
- 关于 Spring AOP (AspectJ) 你该知晓的一切
版权声明:本文为CSDN博主「zejian_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/javazej ...
- Spring框架系列(2) - Spring简单例子引入Spring要点
上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...
- 菜鸟学习Spring——60s使用annotation实现简单AOP
一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...
随机推荐
- 分享一篇Linux系统使用Tomcat服务时交互式修改server.xml中端口号的shell脚本
#!/bin/bash echo -e '\n' echo "***********************************" port1=`grep -r "s ...
- tomcat 端口8080占用问题
启动tomcat时,有时会出现8080端口占用的问题. 解决方法: 终端:ps -e | grep tomcat 会看到下边的结果 途中标记的是进程号,kill掉即可. kill -9 9734(97 ...
- 【转载】研发应该懂的binlog知识(下)
引言 这篇是<研发应该懂的binlog知识(上)>的下半部分.在本文,我会阐述一下binlog的结构,以及如何使用java来解析binlog.不过,话说回来,其实严格意义上来说,研发应该还 ...
- npm基本介绍及使用
1.什么是npm npm全称node package manager,是node包管理和分发工具.可以理解为前端的maven.通过npm可以很方便的下载js库,管理前端工程.node已经集成npm工具 ...
- [Codeforces 1205B]Shortest Cycle(最小环)
[Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...
- Form表单组件验证
第一版:最基本版本 views源码 #——————————————————————form验证—————————————— from django import forms from django.f ...
- JCTF 2014 小菜一碟
测试文件:https://static2.ichunqiu.com/icq/resources/fileupload//CTF/JCTF2014/re100 1.准备 获得信息 ZIP文件 Java文 ...
- VUE组件嵌套
vue中组件嵌套烦分为两种,分别是全局注册组件和局部注册组件 基本步骤: 1.在components 下创建一个新的.vue结尾的文件,文件首字母最好是大写,基于规范复制代码 2.分别写出结构层< ...
- JavaScript基础8——弹窗案例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- idea 创建java web项目ssm-gradle
环境准备:jdk1.8+tomcat8+idea+gradle 1.创建项目SSM 使用gradle创建项目,按照提示如下 image.png 输入项目名称,组名 image.png im ...