Spring AOP 配置使用

一、基本原理

1、什么是aop

专业术语:在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方 式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个 热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑 的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高 了开发的效率。

白话:编程中,对象与对象之间,方法与方法之间,模块与模块之间都是一个个切面。而这些对象之前、方法之前等都要调用同一个方法的时候,为了减少代码的重复copy,所以出现切面的思想,这时候,只需要将方法注入到接口调用的某个地方(切点),这样接口只需要关心具体的业务,而不需要关注其他非该接口关注的逻辑或处理。

2、aop的相关概念

  • Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。
  • Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。
  • Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。
  • Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。
  • Target(目标对象):织入 Advice 的目标对象.。
  • Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程

3、在Java中动态代理的两种方式:

  • JDK动态代理:JDK动态代理需要实现某个接口
  • CGLib动态代理:其生成的动态代理对象是目标类的子类

场景选择:

          如果是单例的我们最好使用CGLib代理,如果是多例的我们最好使用JDK代理

 原因:

  • JDK在创建代理对象时的性能要高于CGLib代理,而生成代理对象的运行性能却比CGLib的低。
  • 如果是单例的代理,推荐使用CGLib

二、配置字段详解

1、配置文件方式

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:aop="http://www.springframework.org/schema/aop"

        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

      

        <!-- 配置bean -->

        <bean id="arithmeticCalculator" class="com.spring.aop.impl.xml.ArithmeticCalculatorImpl"></bean>

        <!-- 配置切面的bean -->

        <bean id="loggingAspect" class="com.spring.aop.impl.xml.LoggingAspect"></bean>

          

        <bean id="validationAspect" class="com.spring.aop.impl.xml.ValidationAspect"></bean>

          

        <!-- 配置AOP -->

        <aop:config>

            <!-- 配置切点表达式 -->

            <aop:pointcut expression="execution(* com.spring.aop.impl.xml.ArithmeticCalculator.*(..))" id="pointcut"/>

            <!-- 配置切面及通知,使用order指定优先级 -->

            <aop:aspect ref="loggingAspect" order="1">

                <!-- 环绕通知 -->

                <!-- 

                    <aop:around method="aroundMethod" pointcut-ref="pointcut"/>

                -->

                <!-- 前置通知 -->

                <aop:before method="beforeMethod" pointcut-ref="pointcut"/>

                <!-- 后置通知 -->

                <aop:after method="afterMethod" pointcut-ref="pointcut"/>

                <!-- 异常通知 -->

                <aop:after-throwing method="afterThrowing"  pointcut-ref="pointcut" throwing="e"/>

                <!-- 返回通知 -->

                <aop:after-returning method="afterReturnning" pointcut-ref="pointcut" returning="result"/>

                  

            </aop:aspect>

            <aop:aspect ref="validationAspect" order="2">

                <!-- 前置通知 -->

                <aop:before method="validateArgs" pointcut-ref="pointcut"/>

            </aop:aspect>

        </aop:config>

    </beans>

2、注解方式

package com.facishare.webpage.customer.aop;

 

import com.facishare.webpage.customer.api.exception.WebPageException;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

/**

 * Created by zhangyu on 2019/9/9

 */

@Aspect

public class WebPageControllerAspect {

 

    private static Logger logger = LoggerFactory.getLogger(WebPageControllerAspect.class);

 

    @Before(value = "execution(* com.facishare.webpage.customer.controller..*.*(..))")

    public void beforeCall(JoinPoint joinPoint) {

        logger.info("before WebPageController method:{} Args:{}", joinPoint.getSignature().toShortString(), joinPoint.getArgs());

    }

 

    @AfterReturning(value = "execution(* com.facishare.webpage.customer.controller..*.*(..))", returning = "ret")

    public void afterReturn(JoinPoint joinPoint, Object ret) {

        logger.info("afterReturn, WebPageController:{} arg:{} Return:{}",

                joinPoint.getSignature().toShortString(), joinPoint.getArgs(), ret);

    }

 

    @AfterThrowing(value = "execution(* com.facishare.webpage.customer.controller..*.*(..))", throwing = "e")

    public void afterThrowing(JoinPoint joinPoint, Throwable e) throws Throwable {

        if (e instanceof WebPageException) {

            logger.warn("afterThrowing, WebPageController:{} ", joinPoint.getSignature().toShortString(), e);

        else {

            logger.error("afterThrowing, WebPageController:{} ", joinPoint.getSignature().toShortString(), e);

            throw e;

        }

    }

 

}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/aop

         http://www.springframework.org/schema/aop/spring-aop.xsd

         http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd">

 

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

        <aop:include name="webPageServiceAspect"/>

        <aop:include name="webPageControllerAspect"/>

        <aop:include name="serviceProfiler"/>

        <aop:include name="webPageConsoleAspect"/>

    </aop:aspectj-autoproxy>

 

    <bean id="webPageServiceAspect" class="com.facishare.webpage.customer.aop.WebPageServiceAspect"/>

    <bean id="webPageControllerAspect" class="com.facishare.webpage.customer.aop.WebPageControllerAspect"/>

    <!--蜂眼配置-->

    <bean id="serviceProfiler" class="com.facishare.qixin.common.aop.QixinTraceServiceProfiler"/>

    <bean id="webPageConsoleAspect" class="com.facishare.webpage.customer.aop.WebPageConsoleAspect"/>

    <aop:config proxy-target-class="true">

        <aop:aspect ref="serviceProfiler">

            <aop:around method="profile" pointcut="execution(* com.facishare.webpage.customer.controller.impl.*.*(..))"/>

        </aop:aspect>

    </aop:config>

</beans>

 

三、应用

1、正确的使用姿势

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/aop

         http://www.springframework.org/schema/aop/spring-aop.xsd

         http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 配置aop 注解生效 备注:可以使用两种方式:一种是下面这种,另一种是<aop:aspectj-autoproxy/> 如果使用下面这种方式,必须将对应的aop加进来-->

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

        <aop:include name="webPageServiceAspect"/>

        <aop:include name="webPageControllerAspect"/>

        <aop:include name="serviceProfiler"/>

    </aop:aspectj-autoproxy>

 

    <bean id="webPageServiceAspect" class="com.facishare.webpage.customer.aop.WebPageServiceAspect"/>

    <bean id="webPageControllerAspect" class="com.facishare.webpage.customer.aop.WebPageControllerAspect"/>

    <!--蜂眼配置-->

    <bean id="serviceProfiler" class="com.facishare.qixin.common.aop.QixinTraceServiceProfiler"/>

    <!--aop配置 切面类名 用于申明切面类-->

    <aop:config proxy-target-class="true">

        <aop:aspect ref="serviceProfiler">

            <aop:around method="profile" pointcut="execution(* com.facishare.webpage.customer.controller.impl.*.*(..))"/>

        </aop:aspect>

    </aop:config>

</beans>

 

2、曾经踩过的坑

         备注:这个就是没有把idempotentAspect   include到aspectj-autoproxy,导致切不到对应的aop

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/aop

         http://www.springframework.org/schema/aop/spring-aop.xsd

         http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd">

 

 

    <aop:aspectj-autoproxy>

        <aop:include name="serviceAop"/>

    </aop:aspectj-autoproxy>

 

    <bean id="serviceAop" class=" com.facishare.extension.provider.aop.ExtensionProviderAop"/>

    <bean id="idempotentAspect" class="com.facishare.idempotent.IdempotentAspect" />

 

    <bean id="qixinTraceProfiler" class="com.facishare.extension.provider.profile.QixinTraceProfiler"/>

 

    <aop:config>

 

        <aop:aspect ref="qixinTraceProfiler">

            <aop:around method="profile" pointcut="execution(* com.facishare.extension.provider.service.dubboService.SpeechToTextServiceImpl.*(..))"/>

        </aop:aspect>

 

        <aop:aspect ref="qixinTraceProfiler">

            <aop:around method="profile" pointcut="execution(* com.facishare.extension.provider.service.dubboService.ApplyServiceImpl.*(..))"/>

        </aop:aspect>

 

        <aop:aspect ref="qixinTraceProfiler">

            <aop:around method="profile" pointcut="execution(* com.facishare.extension.provider.service.dubboService.ChatBoardServiceImpl.*(..))"/>

        </aop:aspect>

        <aop:aspect ref="qixinTraceProfiler">

            <aop:around method="profile" pointcut="execution(* com.facishare.extension.provider.service.dubboService.InteractionServiceImpl.*(..))"/>

        </aop:aspect>

 

 

    </aop:config>

 

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值