Spring框架(6)---AspectJ实现AOP
AspectJ实现AOP
上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP
通过AspectJ实现AOP要比普通的实现Aop要方便的多,所以第五篇文章有关SpringAOP我暂且不写,后面整理好了再补充上;
那我们首先还是讲一些有的没的的东西:
什么是Spring的AspectJ的AOP
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
Spring2.0以后新增了对AspectJ切点表达式支持,@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
新版本Spring框架,建议使用AspectJ方式来开发AOP
那我们先来一个小案例,还是以前面狗的案例举例:
1.导入相关架包

在原来的基础上导入下面四个架包:
- spring-aop-4.2.0.RELEASE.jar
- com.springsource.org.aopalliance-1.0.0.jar
- spring-aspects-4.2.0.RELEASE.jar
- com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
2.编写配置文件applicationContext.xml 导入aop的命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 配置注解bean所在的包 -->
<context:annotation-config/>
<context:component-scan base-package="com.study.spring.d_advisor"></context:component-scan>
<!-- 打开自动代理 -->
<aop:aspectj-autoproxy/>
</beans>
3.编写代理对象
public class Dog {
public void run() {
System.out.println("狗会跑");
}
public void jump() {
System.out.println("狗会跳");
}
public void eat() {
System.out.println("狗能吃");
}
}
4)编写切面类
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//@Aspect声明当前类是一个切面
@Aspect
public class MyAspectJ {
//@Before代表在执行方法之前执行增强代码
@Before("execution(* com.study.dog.Dog.*(..))")
public void before1() {
System.out.println("饲养员叫你你猜能动");
} }
5编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 配置注解bean所在的包 -->
<context:annotation-config/>
<context:component-scan base-package="com.study.spring.d_advisor"></context:component-scan>
<!-- 打开自动代理 -->
<aop:aspectj-autoproxy/> <!-- 被代理对象 -->
<bean id="Dog" class="com.study.dog.Dog"></bean>
<!-- 切面 -->
<bean id="MyAspectJ" class="com.study.dog.MyAspectJ"></bean> </beans>
6.编写测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //完成配置文件的加载
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AspectJTest {
// 得到dog对象
@Autowired
@Qualifier("Dog")
private Dog dog; @Test
public void Demo1() {
dog.eat();
dog.run();
dog.jump();
}
/*
* 输出结果:
* 饲养员叫你你猜能动
* 狗能吃
* 饲养员叫你你猜能动
* 狗会跑
* 饲养员叫你你猜能动
* 狗会跳
*/
}
总结:上面是一个最简单的AspectJ的AOP,我们这样就可以通过输入一个”饲养员叫你你猜能动",就可以在多个方法中存在,在以后开发非常需要
那么下来来说一些非常重要的干货!
AspectJ表达式:
语法:execution(表达式) 通过execution函数,可以定义切点的方法切入
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
例如
execution(public * *(..)) 匹配所有类public方法
execution(* cn.study.dao..*(..)) 匹配指定包下所有类方法,不包含子包
execution(* cn.study.dao..*(..)) ..*表示包、子孙包下所有类
execution(* cn.study.service.UserService.*(..)) 匹配指定类所有方法
execution(* cn.study.dao.GenericDAO+.*(..)) 匹配实现特定接口所有类方法
execution(* save*(..)) 匹配所有save开头的方法
AspectJ增强
@Before 前置通知,相当于BeforeAdvice
@AfterReturning 后置通知,相当于AfterReturningAdvice
@Around 环绕通知,相当于MethodInterceptor
@AfterThrowing抛出通知,相当于ThrowAdvice
@After 最终final通知,不管是否异常,该通知都会执行
@DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)
那现在我们通过上面的列子,来全面贯彻上面的干货!
对上面MyAspectJ的进行全面增强:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
//@Aspect声明当前类是一个切面
@Aspect
public class MyAspectJ { //@Before代表在执行方法之前执行增强代码
@Before("execution(* com.study.dog.Dog.run(..))")
public void before(JoinPoint joinPoint){
System.out.println("前置增强...."+joinPoint);
}
//后置通知
@AfterReturning (value="execution(* com.study.dog.Dog.jump(..))",returning="returnVal")
public void afterReturin(Object returnVal){
System.out.println("后置增强....方法的返回值:"+returnVal);
}
//环绕通知
@Around(value="MyAspectJ.myPointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强....");
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕后增强....");
return obj;
}
//抛出通知
@AfterThrowing(value="MyAspectJ.myPointcut()",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("不好了 出异常了!!!"+e.getMessage());
} //最终final通知,不管是否异常,该通知都会执行
@After("MyAspectJ.myPointcut()")
public void after(){
System.out.println("最终通知...");
}
//切点的定义
@Pointcut("execution(* com.study.dog.Dog.eat(..))")
private void myPointcut(){}
}
编写测试类AspectJTest 和上面一点不变
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //完成配置文件的加载
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AspectJTest {
// 得到dog对象
@Autowired
@Qualifier("Dog")
private Dog dog; @Test
public void Demo1() {
dog.eat();
dog.run();
dog.jump();
} }
运行结果:

通过这个例子,相信大家对于AspectJ的AOP有了一定的了解了。
最后补充一个面试题:
Advisor和Aspect的区别?
Advisor:Spring传统意义上的切面:支持一个切点和一个通知的组合.
Aspect:可以支持多个切点和多个通知的组合.
这篇文章就讲到这里,有不足之处,欢迎大家指出,谢谢!
Spring框架(6)---AspectJ实现AOP的更多相关文章
- Spring框架IOC容器和AOP解析 非常 有用
Spring框架IOC容器和AOP解析 主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...
- spring框架之AspectJ的XML方式完成AOP的开发
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- Spring框架的基本使用(AOP部分)
AOP,Aspect Oriented Programming,意为面向切面编程,是通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP采取横向抽取机制,取代了传统纵向继承体系重复 ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第3章.AOP技术
第3章--AOP技术 Spring框架 - AOP概述 笔记https://my.oschina.net/hava/blog/758873Spring框架 - AOP使用 笔记https://my.o ...
- 深入学习Spring框架(三)- AOP面向切面
1.什么是AOP? AOP为 Aspect Oriented Programming 的缩写,即面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术..AOP是OOP的延续, ...
- Spring框架之IoC和AOP
Spring框架简介: 2003年2月,Spring框架正式成为一个开源项目,并发布于SourceForge中.致力于Java EE应用的各种解决方案,而并不是仅仅专注于某一层的方案,是企业应用开发的 ...
- Spring 框架基础(04):AOP切面编程概念,几种实现方式演示
本文源码:GitHub·点这里 || GitEE·点这里 一.AOP基础简介 1.切面编程简介 AOP全称:Aspect Oriented Programming,面向切面编程.通过预编译方式和运行期 ...
- Spring框架IOC容器和AOP解析
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- Spring框架(四)AOP面向切面编程
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
随机推荐
- 使用(Drawable)资源———ShapeDrawable资源
ShapeDrawable用于定义一个基本的几何图形(如矩形.圆形.线条等),定义ShapeDrawable的XML文件的根元素是<shape.../>元素,该元素可指定如下属性. and ...
- Libcurl细说
libcurl教程 原文地址:http://curl.haxx.se/libcurl/c/libcurl-tutorial.html 译者:JGood(http://blog.csdn.net/J ...
- loadrunner controller:设置多个load generator
下面讲一下如何使用多台电脑进行负载测试. 1) 打开load generator,如图所示默认已添加了我们本地的Generator: 2) 点击"Add. ...
- 【Android】 分享一个完整的项目,适合新手!
写这个app之前是因为看了头条的一篇文章:http://www.managershare.com/post/155110,然后心想要不做一个这样的app,让手机计算就行了.也就没多想就去开始整了. ...
- android:在ViewPager中使用Button
最近在项目用用到ViewPager ,其中页面包含有Button,因为之前也有使用个ViewPager ,所以这个也照搬之前的方式,测试后发现点击button无法执行,这个button是在第一页面的默 ...
- [html]关于html标签的一些总结
以下内容纯属个人对项目细节的总结,因为只是为了自己回顾方便,所以比较杂乱. 1.img 如果不指定img的高度和宽度,则img显示的是原图片的大小:如果只指定了高度和宽度中的一者,则为指定的一者等比例 ...
- CTF入门指南
转自http://www.cnblogs.com/christychang/p/6032532.html ctf入门指南 如何入门?如何组队? capture the flag 夺旗比赛 类型: We ...
- java_流
转自:http://blog.csdn.net/hguisu/article/details/7418161 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有 ...
- vs基础:无法断点调试dll项目 无法命中
调试vs时,经常会出现,你设置了dll项目的一些断点,可f5之后,这些断点无效.时代定制的程序组的童鞋告诉你解决方法:在解决方案上右键“属性”,点击左侧树“配置属性”-->“配置”,右侧项目列表 ...
- 性能优化之数据存储&DOM编程
多读书多看报 数据存储 ·在javascript中,数据存储的位置会对代码整体性能产生重大的影响. ·数据存储共有4种方式:字面量.变量.数组.对象成员. ·要理解变量的访问速度,就要理解作用域. ...