AOP

来源:互联网 发布:nginx限制返回大小 编辑:程序博客网 时间:2024/05/21 22:21

SpringMVC AOP


什么是AOP?实现方式是什么?

Aspect Oriented Programming的缩写,意思为:面向切面编程,通过预编译方式运行期动态代理实现程序功能的统一维护的一种技术
主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理

实现方式

预编译
AspectJ
运行期动态代理(JDK动态代理、CGLib动态代理)
SpringAOP、JbossAOP

Advice
前置通知(Before advice)
返回后通知(After returning advice)
抛出异常后通知(After trowing advice)
后通知(After(finally)advice)
环绕通知(Around Advice)
方法的第一个参数必须时ProceedingJoinPoint类型
顺序为 before -> around1 -> around2 -> afterreturning (aftertrowing) -> after

AOP基本概念

Spring中的AOP

  • 提供了声明式的企业服务,特别是EJB的替代服务的声明
  • 允许用户定制自己的方面,以完成OOP与AOP的互补使用
    立足于Spring IoC容器自身的基础上,提供一种有用的AOP实现,解决问题中Aop的实现。

Schema-based aop

Spring所有的切面和通知器都必须放在一个<aop:config>内(可以配置包括多个<aop:config>元素),每一个<aop:config>可以包含pointcut,advisor和aspect元素。
<aop:config>
  <aop:aspect id="myAspect" ref="aBean">
  <aop:pointcut id="Pointcut" expression="execution(* 包名.类名.*(..))"/>
  <aop:before method="before" pointcut-ref="Pointcut"/>
  <aop:after-returning method="afterReturning" pointcut-ref="Pointcut"/>
  <aop:after-throwing method="afterThrowing" pointcut-ref="Pointcut"/>
  <aop:after method="after" pointcut-ref="Pointcut"/>
  <aop:around method="around" pointcut-ref="Pointcut"/>
  <aop:around method="aroundInit" pointcut="execution(* 包名.类名.*(String,int) and args(name,times))"/>
  //为指定对象bean指定一个新的父类
  <aop:declare-parents types-matching="对象bean"
  implement-interface="接口"
  default-impl="接口的实现类"
  </aop:aspect>
</aop:config>

0 0
原创粉丝点击