AOP

作者: 头发旺盛的程序猿 | 来源:发表于2020-08-29 13:06 被阅读0次

AOP(Aspect Oriented Programing 面向切面编程)

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

      <!--被代理对象(target)-->
      <bean id="orderServiceBean" class="com.apesource.service.impl.OrderServiceImpl"/>
      <bean id="userServiceBean" class="com.apesource.service.impl.UserServiceImpl"/>
      
      <!--通知(Advice)-->
      <bean id="logAdviceBean" class="com.apesource.advice.LogAdvice"/>
      <bean id="performanceAdviceBean" class="com.apesource.advice.PerformanceAdvice"/>
      
      <!--切入点(Pointcut):通过正则表达式描述指定切入点(某些指定方法)-->
      <bean id="createMethodPointcutBean" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
          <!--注入正则表达式:描述哪些方法为切入点-->
          <property name="pattern" value=".*create.*"/>
      </bean>
      
      <!--Advisor(高级通知) = Advice(通知) + Pointcut(切入点)-->
      <bean id="performanceAdvisorBean" class="org.springframework.aop.support.DefaultPointcutAdvisor">
          <!--注入切入点-->
          <property name="pointcut" ref="createMethodPointcutBean"/>
      
          <!--注入通知-->
          <property name="advice" ref="performanceAdviceBean"/>
      </bean>
      
      <!--自动代理创建-->
      <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
          <!--Bean名称规则(数组):指定哪些bean创建自动代理-->
          <property name="beanNames">
              <list>
                  <value>*ServiceBean</value>
                  <value>*TaskBean</value><!--自己设置需要自动代理的后缀名-->
              </list>
          </property>
      
          <!--通知列表:需要执行哪些通知-->
          <property name="interceptorNames">
              <list>
                  <value>performanceAdvisorBean</value>
                  <value>logAdviceBean</value>
              </list>
          </property>
      </bean>
      
    2. 使用AspectJ框架,可任意选择类来定义通知,在xml中进行配置

      <!--业务组件bean-->
      <bean id="orderServiceBean" class="com.apesource.service.impl.OrderServiceImpl"/>
      
      <!--日志Aspect切面-->
      <bean id="logAspectBean" class="com.apesource.advice.LogAspect"/>
      
      <!--使用Aspect实现切面,使用spring AOP进行配置-->
      <aop:config>
          <!--配置切面-->
          <!--注入切面bean-->
          <aop:aspect ref="logAspectBean">
              <!--定义Pointcut:通过expression表达式,来查找特定的方法(pointcut)-->
              <aop:pointcut id="serviceMethodPointcut" expression="execution(* com.apesource.service.impl.*.create*(..) )"/>
      
              <!--配置"前置通知"-->
              <!--在pointcut切入点(serviceMethodPointcut)查找到的方法执行“前”,执行当前logAspectBean的doBefore-->
              <aop:before method="doBefore" pointcut-ref="serviceMethodPointcut"/>
      
              <!--配置"后置通知"-->
              <!--returning属性:配置当前方法中用来接收返回值的参数名-->
              <aop:after-returning returning="returnVal" method="afterReturningAdvice" pointcut-ref="serviceMethodPointcut"/>
              <aop:after method="afterAdvice" pointcut-ref="serviceMethodPointcut"/>
      
              <!--配置"异常通知"-->
              <!--throwing属性:配置大年方法中用来接收当前异常的参数名-->
              <aop:after-throwing throwing="ex" method="throwingAdvice" pointcut-ref="serviceMethodPointcut"/>
      
              <!--配置环绕通知-->
              <aop:around method="aroundAdvice" pointcut-ref="serviceMethodPointcut"/>
          </aop:aspect>
      </aop:config>
      
    3. 使用注解,在xml中配置自动扫描器和AspectJ的自动代理,在自定义需要通知的类或方法前添加注解。

      <!--自动扫描器-->
      <context:component-scan base-package="com.apesource"/>
      
      <!--配置AspectJ的自动代理-->
      <aop:aspectj-autoproxy/>
      
      @Aspect
      @Component
      public class LogAnotationAspect {
          private static final String POINTCUT_EXPRESSION = "execution(* com.apesource.service.impl.*.create*(..) )";
      
          /**
           * 前置通知
           */
          @Before(POINTCUT_EXPRESSION)
          public void doBefore(JoinPoint joinPoint){
              System.out.println("========【使用AspectJ实现前置通知】开始===========");
              System.out.println("目标对象:" + joinPoint.getTarget());
              System.out.println("目标方法:" + joinPoint.getSignature().getName());
              System.out.println("参数列表:" + joinPoint.getArgs());
              System.out.println("========【使用AspectJ实现前置通知】结束===========");
          }
      
          /**
           * 后置通知:方法正常执行后,有返回值,执行该后置通知;如果该方法执行出现异常,则不执行后置通知
           */
          @AfterReturning(value = POINTCUT_EXPRESSION,returning = "returnVal")
          public void afterReturningAdvice(JoinPoint joinPoint,Object returnVal){
              System.out.println("========【使用AspectJ实现后置通知】开始===========");
              System.out.println("目标对象:" + joinPoint.getTarget());
              System.out.println("目标方法:" + joinPoint.getSignature().getName());
              System.out.println("参数列表:" + joinPoint.getArgs());
              System.out.println("返回值:" + returnVal);
              System.out.println("========【使用AspectJ实现后置通知】结束===========");
          }
      
          /**
           * 后置通知
           */
          @After(POINTCUT_EXPRESSION)
          public void afterAdvice(JoinPoint joinPoint){
              System.out.println("========【使用AspectJ实现后置通知】开始===========");
              System.out.println("目标对象:" + joinPoint.getTarget());
              System.out.println("目标方法:" + joinPoint.getSignature().getName());
              System.out.println("参数列表:" + joinPoint.getArgs());
              System.out.println("========【使用AspectJ实现后置通知】结束===========");
          }
      
          /**
           * 异常通知:方法出现异常时,执行该通知
           * @param joinpoint
           * @param ex
           */
          @AfterThrowing(value = POINTCUT_EXPRESSION,throwing = "ex")
          public void throwingAdvice(JoinPoint joinpoint,Exception ex){
              System.out.println("==========【使用AspectJ实现异常通知】开始==================");
              System.out.println("目标对象:" + joinpoint.getTarget());
              System.out.println("目标方法:" + joinpoint.getSignature().getName());
              System.out.println("出现异常:" + ex.getMessage());
              System.out.println("==========【使用AspectJ实现异常通知】结束==================");
          }
      
          /**
           * 环绕通知
           */
          @Around(POINTCUT_EXPRESSION)
          public Object aroundAdvice(ProceedingJoinPoint joinpoint) throws Throwable{
      
              System.out.println("#########$环绕通知的前置部分$###############");
              Object returnVal = joinpoint.proceed();
              System.out.println("#########$环绕通知的后置部分$###############");
      
              return returnVal;
          }
      }
      

相关文章

网友评论

      本文标题:AOP

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