• AOP 切面


      /**
      * 配置aop首先得配置需要aop业务的类 例如 <bean id="helloWorldService" class="com.foreignstudent.util.HelloWorldService" />
      * 其次需要配置aop处理类         例如 <bean id="helloWorldAspectBean" class="com.foreignstudent.util.HelloWorldAspect" />

      * 然后配置相应的通知方式
      * 需要注意的是,需修改返回值的话只能在环绕通知中修改
      * 在一个是参数的传递,只要看懂配置方式aop,就可以依葫芦画瓢摸索出注解方式
      */

      <!-- 需要aop业务的类 --> <bean id="helloWorldService" class="com.foreignstudent.util.HelloWorldService" />
      <!-- aop处理通知类 --> <bean id="helloWorldAspectBean" class="com.foreignstudent.util.HelloWorldAspect" /> <!-- 配置一个切面 --> <aop:config> <aop:aspect id="helloWorldAspect" ref="helloWorldAspectBean"> <aop:pointcut id="helloWorldServicePointcut" expression="execution(* com.foreignstudent.util.*.*(..))" /> <!-- 配置前置通知,这个是自定义的通知类可有可无,需要配置类和方法,一下雷同 --> <aop:before pointcut-ref="helloWorldServicePointcut" method="beforeAdvice" /> <!-- 配置前置通知 --> <aop:after pointcut-ref="helloWorldServicePointcut" method="afterAdvice" /> <!-- 配置后置返回通知 --> <aop:after-returning pointcut-ref="helloWorldServicePointcut" method="afterReturnAdvice" returning="result" /> <!-- 配置环绕通知 --> <aop:around pointcut-ref="helloWorldServicePointcut" method="aroundAdvice" /> <!-- 异常通知 --> <aop:after-throwing pointcut-ref="helloWorldServicePointcut" method="throwingAdvice" throwing="e" /> </aop:aspect> </aop:config>

    <!-- 需要通知的类 -->
    package com.foreignstudent.util;

    import org.springframework.stereotype.Service;

    @Service
    public class HelloWorldService {

        public String SayHelloWorld(String a) {
            System.out.println("Hello World~ " + a);
            int a1 = 0/0;
            return "执行业务方法的返回值为:Hello World";
        }
    }

    <!-- 该类已被我修改为注解配置上述内容可去掉注解只保留相应方法即可,需要注意的是参数的传递,会在类中说明 -->
    package com.foreignstudent.util;

    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;

    @Aspect
    @Component
    public class HelloWorldAspect {

       /**
       * 该注解为带参数,参数名称需和方法参数变量名称相同
       */
        @Pointcut(value = "execution(* com.foreignstudent.util.*.*(..)) && args(obj)", argNames = "obj")
        public void foreignstudent(Object obj) {
        }
       /**
       * 该注解为不带参数,(返回值 包名。类。方法(参数))
       */
        @Pointcut(value = "execution(* com.foreignstudent.util.*.*(..))")
        public void foreign() {
        }

      /**
       *
       */
        @Before(value = "foreignstudent(obj)", argNames = "obj")
        public void beforeAdvice(Object obj) {
            System.out.println("前置通知执行了" + obj);
        }

        @After(value = "foreignstudent(obj)", argNames = "obj")
        public void afterAdvice(Object obj) {
            System.out.println("后置通知执行了");
        }

        @AfterReturning(pointcut = "foreign()", returning = "obj")
        public void afterReturnAdvice(Object obj) {
            System.out.println("返回通知执行了" + "运行业务方法返回的结果为 == " + obj);
        }

        @Around(value = "execution(* com.foreignstudent.util.*.*(..))")
        public String aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            String result = "1";
            System.out.println("环绕通知开始执行了");
            long start = System.currentTimeMillis();
            proceedingJoinPoint.proceed();
            long end = System.currentTimeMillis();
            System.out.println("环绕通知执行结束了");
            System.out.println("执行业务方法共计:" + (end - start) + "毫秒。");
            return result;
        }

        public void throwingAdvice(JoinPoint joinPoint, Exception e) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("异常通知执行了.");
            stringBuffer.append("方法:").append(joinPoint.getSignature().getName()).append("出现了异常.");
            stringBuffer.append("异常信息为:").append(e.getMessage());
            System.out.println(stringBuffer.toString());
        }
    }


  • 相关阅读:
    csp-s模拟110
    csp-s模拟109
    留念
    csp-s 2019 游记
    HEOI2020
    CSP-S2019记
    堆积的$TIPS$
    低错复习
    倍增并查集
    4.26
  • 原文地址:https://www.cnblogs.com/foreign-student/p/7867631.html
Copyright © 2020-2023  润新知