zoukankan      html  css  js  c++  java
  • AOP实现方式

    Annotation方式注解方式

    1.采用Aspect定义切面

    2.在Aspect中定义PointCut和Advice

    3.启用AspectJ对Annotation的支持并且将Aspect类和目标对象配置到IOC容器中。

    定义一个类,表示为@Aspect  定义一个方法标示为Pointcut(“execution(* add*(..)) ”) 定义方法执行的顺序 before(“pointcutname”)。

    最终生成的是代理。

    AOP:

    横切性关注点:Cross cutting concern

    切面:Aspect

    具体要植入的方法  :Advice

    切入点:Pointcut

    Joinpoint

    植入:Weave

    目标对象:target Object

    代理:Proxy

    动态方法:Introduction

    配置文件的方式:

    <aop:config>

    <aop:aspect id=”security” ref=”securityHandler”>

    <aop:pointcut id=”alladdmethod” expression=”execution(* cn.synjones.dreams.usermanagerimpl.add*(..))” />

    <aop:before method=”checkSecurity”/>

    </aop:aspect>

    </aop:config>

    Aspect默认情况下不用实现接口,但对于目标对象,在默认情况下必须实现接口,如果没有实现接口必须引入CGLIB库。

    如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP,也可以强制使用CGLIB库实现AOP.

    如果目标对象没有实现接口,必须采用CGLIB库,Spring会自动在JDK动态代理和CGLIB之间转换。

    JDK动态代理和CGLIB字节码生成的区别:

    1.JDK动态代理只能对实现了接口的类生成代理,而不能正对类。

    2.CGLIB是正对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法,因为是集成,所以该类的方法最好不要声明成final.

    如何强制使用CGLIB实现AOP?

    1.引入CGLIB库,/cglib/*.jar

    <aop:aspectj-autoproxy proxy-target-class=”true”/>

    Joinpoint获取方法的参数

    在Advice中使用joinpoint参数可以获取到执行的方法的参数: joinpint.getArgs();

  • 相关阅读:
    免费的视频、音频转文本
    Errors are values
    Codebase Refactoring (with help from Go)
    Golang中的坑二
    Cleaner, more elegant, and wrong(msdn blog)
    Cleaner, more elegant, and wrong(翻译)
    Cleaner, more elegant, and harder to recognize(翻译)
    vue控制父子组件渲染顺序
    computed 和 watch 组合使用,监听数据全局数据状态
    webstorm破解方法
  • 原文地址:https://www.cnblogs.com/guaniu/p/2322114.html
Copyright © 2011-2022 走看看