美文网首页
Spring AOP

Spring AOP

作者: lc_666 | 来源:发表于2020-03-31 21:13 被阅读0次
    • 面向切面编程;

    启动AspectJ

    • 添加AspectJ类库;
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aopalliance/com.springsource.org.aopalliance -->
    <dependency>
        <groupId>org.aopalliance</groupId>
        <artifactId>com.springsource.org.aopalliance</artifactId>
        <version>1.0.0</version>
    </dependency>
    
    • 在配置文件中定义一个空的xml元素;
    <aop:aspectj-autoproxy/>
    
    • 编写类,需要加入扫描,标注@Aspect
    @Aspect
    @Component
    public class AopLogging {
    //    //声明该方法是一个前置通知,在方法执行之前执行
    //    @Before("execution(public int ArithmeticCalculator.add(int,int))")
    //    public void before(JoinPoint joinPoint) {
    //        String methodName = joinPoint.getSignature().getName();
    //        List args = Arrays.asList(joinPoint.getArgs());
    //        System.out.println("The method" + methodName + " begins with " + args);
    //    }
    //    @After("execution(public int ArithmeticCalculator.*(int,int))")
    //    public void after(JoinPoint joinPoint) {
    //        String methodName = joinPoint.getSignature().getName();
    //        List args = Arrays.asList(joinPoint.getArgs());
    //        System.out.println("The method" + methodName + " end with " + args);
    //    }
    //
    //    @AfterReturning(value = "execution(public int ArithmeticCalculator.*(int,int))",returning = "result")
    //    public void afterReturn(JoinPoint joinPoint,Object result) {
    //        String methodName = joinPoint.getSignature().getName();
    //        System.out.println("The method" + methodName + " returns with " + result);
    //    }
    //
    //    @AfterThrowing(value = "execution(public int ArithmeticCalculator.*(int,int))",throwing = "ex")
    //    public void afterThrow(JoinPoint joinPoint, Exception ex) {
    //        String methodName = joinPoint.getSignature().getName();
    //        System.out.println("The method" + methodName + " occurs: " + ex);
    //    }
    
        @Around("execution(public int ArithmeticCalculator.*(..))")
        public Object around(ProceedingJoinPoint point) {
            String methodName = point.getSignature().getName();
            Object result = null;
    
            try {
                System.out.println("This method starts with name: " + methodName);
                result = point.proceed();
                System.out.println("This method return result: " + result);
            } catch (Throwable throwable) {
                System.out.println("This method occurs with" + throwable);
                throwable.printStackTrace();
            }
    
            System.out.println("This method ends with name: " + methodName);
            return result;
        }
    }
    
    • 在类中声明各种通知:
      • @Before:前置通知;
      • @After:目标方法执行后,无论是否发生异常;
      • @AfterReturning:携带方法的返回值;
      • @AfterThrowing:抛出异常时执行,可获取异常对象;
      • @Around:环绕,功能最强大,包含以上几种;

    切面的优先级

    @Order(1)
    @Aspect
    @Component
    public class AopLogging {
    
    • 使用@Order指定优先级,值越小,优先级越高

    切面表达式重用

    • 定义方法来指定相同的切面表达式;
    • 使用时,直接引用方法名;
    • 外部使用需要全类名.方法名;
    @Pointcut("execution(public int ArithmeticCalculator.*(..))")
    public void declarePoint(){}
    
    @Before("declarePoint()")
    public void before(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method" + methodName + " begins with " + args);
    }
    

    使用基于配置文件的使用

    <bean id="aopLogging" class="com.llds.aop2.AopLogging"/>
    <bean id="calculator" class="com.llds.aop2.ArithmeticCalculatorImpl"/>
    
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.llds.aop2.ArithmeticCalculator.*(..))"/>
        <aop:aspect ref="aopLogging" >
            <aop:before method="before" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
    

    相关文章

      网友评论

          本文标题:Spring AOP

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