什么是AOP?
AOP(Aspect Oriented Programming )被称为面向切面编程
AOP利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。

AOP操作术语
1、横切关注点:
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点。
2、切面(aspect):
OOP中类是对物体特征的抽象,那么切面就是AOP中对横切关注点的抽象,切面通常是一个类,里面可以定义切入点和通知(实际上就是你要增加的那段代码)。
3、连接点(joinpoint):
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器。
4、切入点(pointcut):
对连接点进行拦截的定义,通过书写切入点表达式决定究竟要把哪些方法给拦截下来。
5、通知(advice):
所谓通知指的就是指拦截到连接点之后要执行的代码,一般来说就是对目标方法的增强,通知分为前置(在目标方法前执行)、后置(在目标方法后执行)、异常(在目标方法抛出异常后执行)、返回(在目标方法返回结果后执行)、环绕(可以同时实现前置和后置功能)通知五种类型。
6、目标对象:
代理的目标对象。
7、织入(weave):
将切面应用到目标对象并导致代理对象创建的过程。
8、引入(introduction):
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段。

Spring对AOP的支持

在Spring中可以有以下三种方式来现:
1、使用ProxyFactoryBean配置和实现对应接口的配置方式(比较老旧经典的实现,但是现在已经几乎被废弃);
2、使用Spring来定义纯粹的POJO切面,即在对应的springXMl配置文件中通过 <aop:config>标签实现;
3、使用@AspectJ注解驱动切面实现(最常使用);

注意:使用的前提是要引入两个依赖jar : aopalliance-1.0.jar 和 aspectjweaver.jar

下面通过注解来演示AOP
(1)找到连接点,定义切入点

/*
*要被织入通知的方法(切入点)
*/
@Component
public class RoleServiceImpl implements IRoleService {
public void print() {
System.out.println(“ RoleServiceImpl. print() ");
}
}

(2)创建切面

@Component
@Aspect
public class XmlAspect {
//前置通知
@Before("execution(* aop.RoleServiceImpl.print(..))")
public void before(){
System.out.println("brfore.........");
}
//后置通知
@After("execution(* aop.RoleServiceImpl.print(..))")
public void after(){
System.out.println("after.........");
}
//异常通知
@AfterThrowing("execution(* aop.RoleServiceImpl.print(..))")
public void afterThrowing(){
System.out.println("afterThrowing.........");
}
//返回通知
@AfterReturning ("execution(* aop.RoleServiceImpl.print(..))")
public void afterReturing(){
System.out.println("afterReturing.........");
}
//环绕通知
@Around("execution(* aop.RoleServiceImpl.print(..))")
public void around(ProceedingJoinPoint pj){
System.out.println("环绕前");
try {
pj.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕后");
}
}

(3)在xml中进行配置

<!--下面这句是重点-->
<aop:aspectj-autoproxy/>

(4)测试,我们就能看到结果。

(略)

如果我们不用注解,就只需要在xml中配置如下:

<bean id="roleServiceImpl" class="aop.RoleServiceImpl"/>
<!-- XmlAspect就是我们的切面实例,需要在下面被引入 -->
<bean id="xmlAspect" class="aop.XmlAspect"/> <aop:config>
<aop:aspect ref="xmlAspect">
<aop:pointcut id="printId" expression="execution(* aop.RoleServiceImpl.print(..))"/>
<aop:before method="before" pointcut-ref="printId"/>
<aop:after method="after" pointcut-ref="printId"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="printId"/>
<aop:after-returning method="afterReturing" pointcut-ref="printId"/>
</aop:aspect>
</aop:config>

最后简单地说一下这里的execution正则表达式含义:
1、execution: 表示执行对应方法的时候会触发
2、* :代表任意类型返回类型的方法
3、aop.RoleServiceImpl: 代表类的全限定名
4、ptrint :代表被拦截的方法名字
5、(..) : 代表任意的参数。

最新文章

  1. Java多线程整理(li)
  2. php实现设计模式之 状态模式
  3. WereWolf项目 Postmortem
  4. 程序员用HTML5制作的爱心树表白动画
  5. BZOJ 1877: [SDOI2009]晨跑 费用流
  6. hdu 4612 Warm up 桥缩点
  7. 使用Flashbuilder/Flashbuilder-plugins搭建Flex工程每日构建(自动化构建)的方法
  8. JZ2440开发笔记(3)——配置TFTP
  9. visual studio 2013 使用IIS Express附加调试MVC5
  10. 离robots.txt启动网络爬虫之旅
  11. jQuery选择器---基本选择器总结
  12. Java Serializable接口(序列化)理解及自定义序列化
  13. hadoop第一课
  14. C# 与Sql server 获取数据和执行命令
  15. HDU - 4858 项目管理
  16. Spark以yarn-client提交任务时报错超时,Connection to 192.168.. /has been quiet forms while there are outstanding requests. Failed to send RPC.....
  17. 8.2 C++标准输出流对象
  18. 个基于TensorFlow的简单故事生成案例:带你了解LSTM
  19. 题目1461:Tempter of the bone(深度优先遍历DFS)
  20. 【error】select timeout问题

热门文章

  1. Day05_vue入门
  2. Sqlite3 实现学生信息增删改查
  3. Django学习路31_使用 locals 简化 context 写法,点击班级显示该班学生信息
  4. PHP timezone_name_from_abbr() 函数
  5. Python编程入门(第3版) PDF|百度网盘下载内附提取码
  6. Android Studio--Activity实现跳转功能
  7. Django中的ORM如何通过数据库中的表格信息自动化生成Model 模型类?
  8. C#LeetCode刷题之#21-合并两个有序链表(Merge Two Sorted Lists)
  9. wordpress-技术博客主题推荐
  10. Kruscal算法求图的最小生成树