[java][SpringAOP]

为什么会有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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。