美文网首页
三、面向切面编程(AOP)

三、面向切面编程(AOP)

作者: 呆呆_ | 来源:发表于2017-01-21 02:35 被阅读0次

1. 概念

  1. Aspect-Oriented Programming (AOP)可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。

  2. OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。

  3. AOP技术恰恰相反,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。

2. AOP核心概念

  • Aspect(切面)
  • Join point
  • Advice
  • Pointcut
  • Introduction
  • Target object
  • AOP proxy
  • Weaving

3. Spring对AOP的支持

3.1示例
    <!--AOP begin-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <bean id="testAspect" class="com.xxxx.fabu.aspect.TestAspect"/>
    <!--要求进入到AOP的配置,重点配置切入点,以及确定切入的处理操作方法-->
    <aop:config>
        <aop:pointcut id="pointCut" expression="execution(* com.xxxx.fabu.service..*.*(..)) and args(id)"/>
        <aop:aspect ref="testAspect"><!--进行切面的配置,如果是切面控制应该有一个控制的类-->
            <aop:before method="beforeMethod" pointcut-ref="pointCut" arg-names="id"/>
            <aop:after method="afterMethod" pointcut="execution(* com.xxxx.fabu.service..*.*(..))"/>
        </aop:aspect>
    </aop:config>
    <!--AOP end-->

3.2 其他示例

  • the execution of any public method:
execution(public * *(..))
  • the execution of any method with a name beginning with "set":
execution(* set*(..))
  • the execution of any method defined by the AccountService interface:
execution(* com.xyz.service.AccountService.*(..))
  • the execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..))
  • the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service..*.*(..))
  • any join point (method execution only in Spring AOP) within the service package:
within(com.xyz.service.*)
  • any join point (method execution only in Spring AOP) within the service package or a sub-package:
within(com.xyz.service..*)
  • any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:
this(com.xyz.service.AccountService)

相关文章

网友评论

      本文标题:三、面向切面编程(AOP)

      本文链接:https://www.haomeiwen.com/subject/xxmgbttx.html