美文网首页
[java][SpringAOP]

[java][SpringAOP]

作者: 阿不不不不 | 来源:发表于2019-03-14 10:34 被阅读0次
为什么会有AOP
java是一个面向对象的语言,当我们需要为一个不具有继承关系的对象引入一个公共行为,如日志、权限验证、
事务等功能时,只能在每一个对象引用公共行为,这样就不理维护,会产生大量重复代码,AOP就很好的解决了
这个痛点
实现AOP原理
   1.target:目标类,需要被代理的类。例如:UserService

  2.Joinpoint(连接点):所谓连接点是指那些可能被拦截到的方法。例如:所有的方法

  3.PointCut 切入点:已经被增强的连接点。例如:addUser()

  4.advice 通知/增强,增强代码。例如:after、before

  5. Weaving(织入):是指把增强advice应用到目标对象target来创建新的代理对象proxy的过程.

  6.proxy 代理类:通知+切入点

  7. Aspect(切面): 是切入点pointcut和通知advice的结合
image.png
AOP通知类型
Spring按照通知Advice在目标类方法的连接点位置,可以分为5类

前置通知 org.springframework.aop.MethodBeforeAdvice
在目标方法执行前实施增强,比如上面例子的 before()方法
后置通知 org.springframework.aop.AfterReturningAdvice
在目标方法执行后实施增强,比如上面例子的 after()方法
环绕通知 org.aopalliance.intercept.MethodInterceptor
在目标方法执行前后实施增强
异常抛出通知 org.springframework.aop.ThrowsAdvice
在方法抛出异常后实施增强
引介通知 org.springframework.aop.IntroductionInterceptor
在目标类中添加一些新的方法和属性
AOP在Spring中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">
<!--1、 创建目标类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myDataSource" />
    </bean>
<!--2、创建切面类(通知)  -->
    <tx:advice id="crudAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="list*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--3、aop编程 
        3.1 导入命名空间
        3.2 使用 <aop:config>进行配置
                proxy-target-class="true" 声明时使用cglib代理
                如果不声明,Spring 会自动选择cglib代理还是JDK动态代理
            <aop:pointcut> 切入点 ,从目标对象获得具体方法
            <aop:advisor> 特殊的切面,只有一个通知 和 一个切入点
                advice-ref 通知引用
                pointcut-ref 切入点引用
        3.3 切入点表达式
            execution(* com.ys.aop.*.*(..))
            选择方法         返回值任意   包             类名任意   方法名任意   参数任意
     
    -->
    <aop:config>
        <aop:pointcut expression="execution(* com._520it.wms.service.*Service.*(..))" id="crudPc"/>
        <aop:advisor advice-ref="crudAdvice" pointcut-ref="crudPc"/>
    </aop:config>
</beans>

相关文章

  • [java][SpringAOP]

    为什么会有AOP 实现AOP原理 AOP通知类型 AOP在Spring中配置

  • AspectJ实现aop

    定义Aspect切面,配置对com.java.gk.springaop.SpringAopDemo的方法进行拦截处...

  • SpringAOP-2

    SpringAOP实现代理-4 ( AOP config形式) 定义切面类 Aspect.java实现一个通知类...

  • 后端(Spring AOP的三种代理)

    springAOP指的是在spring中的AOP,什么是AOP,相对于java中的面向对象(oop),在面向对象中...

  • spring框架 AOP

    10、 代理模式 为什么要学习代理模式?因为这就是SpringAOP的底层!【SpringAOP 和 Spring...

  • spring源码解析-基于注解的SpringAOP源码解析(二)

    在Spring源码解析之基于注解的SpringAOP源码解析(一)中,我们搭建了SpringAOP源码分析的环境,...

  • 六、AOP实现自动的系统日志功能

    一、本课目标 掌握SpringAOP的配置 二、使用SpringAOP实现日志输出 在下面的这个示例中,通过Spr...

  • SpringAOP

    SpringAOP-PPT SpringAOP视频 面向切面编程(AOP)通过提供另外一种思考程序结构的途经来弥补...

  • springAOP

    springAOP切面拦截参数进行校验。

  • Spring AOP源码分析

    前言 通过之前的俩篇文章,我们大体上已经知道如何使用SpringAOP了,同时也了解到了SpringAOP底层使用...

网友评论

      本文标题:[java][SpringAOP]

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