zoukankan      html  css  js  c++  java
  • 基于注解的AOP配置

    基于注解的AOP配置(一定要要参考上一篇的“基于xml文件的AOP配置”)

    • 主要是将上面的xml文件的内容替换掉,以下是xml文件所有内容,环绕通知的内容就是替换四种通知即可

      <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.xsd">
          
          <bean id="wordsService" class="com.mypro.service.impl.WordsServiceImpl"></bean>
          
          <bean id="logger" class="com.mypro.service.impl.WordsServiceImpl"></bean>
      <aop:config>
          <aop:aspect id="loggerAdvice" ref="logger">
              <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"></aop:point>
              <aop:before method="beforeInfo" pointcut-ref="pt1"></aop:before>
              <aop:after-returning method="afterReturningInfo" pointcut-ref="pt1"></aop:after-returning>
              <aop:after-throwing method="afterThrowingInfo" pointcut-ref="pt1"></aop:after-throwing>
              <aop:after method="afterInfo" pointcut-ref="pt1"></aop:after>
          </aop:aspect>
      </aop:config>
      </beans>
    • 首先我们需要配置我们扫描的包,将xml文件的Bean对象替换成注解,即在我们实现类上方添加注解。在此之前,还需要添加xmlns:context依赖

      <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"
             xmlns:context="http://www.springframework.org/schema/context"
             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.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
          <!-- 代替xml文件的两个Bean对象的配置 -->
          <context:component-scan base-package="com.mypro"></context:component-scan>
      </beans>
    • 开启Spring支持的AOP配置

      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    • 修改通知类的代码,注意实现五种通知标签的功能

      package com.mypro.utils;
      ​
      import org.aspectj.lang.ProceedingJoinPoint;
      import org.aspectj.lang.annotation.*;
      import org.springframework.stereotype.Component;
      ​
      /**
       * 用于记录日志的工具类,里面提供公共的代码
       */
      // 代替xml文件的<aop:config><aop:aspect id="loggerAdvice" ref="logger"></aop:aspect></aop:config>
      @Component("logger")
      @Aspect                      
      public class Logger {
          
          // 代替切入点的配置
          @Pointcut("execution(* com.mypro.service.impl.*.*(..))")
          public void pt1(){}
      ​
          @Before("pt1()")
          public void beforeInfo(){
              System.out.println("前置通知");
          }
          @AfterReturning("pt1()")
          public void totalInfo(){
              System.out.println("后置通知");
          }
          @AfterThrowing("pt1()")
          public void expectInfo(){
              System.out.println("异常通知");
          }
          @After("pt1()")
          public void afterInfo(){
              System.out.println("最终通知");
          }
          
          @Around("pt1()")
          public Object aroundInfo(){
              Object rtValue = null;
              try{
                  System.out.println("前置通知");
                  // 得到方法执行时所需的参数
                  Object[] args = pjp.getArgs();
                  // 明确调用业务层方法(切入点方法)
                  rtValue = pjp.proceed(args);
                  System.out.println("后置通知");
                  return rtValue;
              }catch(Throwable t){      // 必须是用Throwable捕捉异常
                  System.out.println("异常通知");
                  throw new RuntimeException(t);
              }finally{
                  System.out.println("最终通知");
              }
          }
      ​
      }
      ​

       

  • 相关阅读:
    C++中 destory() 和deallocate()以及delete函数的相关性和区别性
    由STL所想到的 C++显示调用析构函数
    MINIX3 内核整体架构回顾及内核定 性分析
    有一个无效 SelectedValue,因为它不在项目列表中
    SqlParameter.Value = NULL 引发的数据库异常
    前端解决跨域问题的8种方案(最新最全)
    SQL语句优化技术分析 整理他人的
    暂时未整理 已打印
    .Net_把文件数据添加到数据库中(面试题)
    ASP.NET中application对象的用法(面试题)
  • 原文地址:https://www.cnblogs.com/aitiknowledge/p/12713143.html
Copyright © 2011-2022 走看看