ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java中详述Spring AOP(中)

2020-11-22 18:59:01  阅读:209  来源: 互联网

标签:Java name Spring jp System AOP println public out


@Around注解

上篇简述了AspectJ支持@Before、@After、@AfterReturning和@AfterThrowing及其执行过程,本模块来阐述其支持的另一种注解:@Around注解。
该注释修饰的方法可以灵活调整目标方法的执行时机;通过@Around注解还可以实现@Before,@After,@AfterReturning和@AfterThrowing增强效果。
下面将按照图中方式进行实现:
在这里插入图片描述
代码如下:

@Around("pointcut()")
    public Object around(ProceedingJoinPoint jp){
        try{
            String name = jp.getSignature().getName();
            Object result = null;
            try {
                Object [] args = jp.getArgs();
                System.out.println("The "+name+" method begins");
                System.out.println("The parameters of the  "+name+"  method are "+args[0]+","+args[1]);
                result = jp.proceed();
            }finally {
                System.out.println("The "+name+" method ends");
            }
            System.out.println("The "+name+" method result:"+result);
            return result;
        }catch (Throwable e){
            System.out.println("#############"+e.getMessage());
        }
        return -1;
    }

代码修改后,Test运行结果如下:
在这里插入图片描述
由图可以看出,通过@Around注解可以完全实现@Before,@After,@AfterReturning和@AfterThrowing增强效果。

@Pointcut注解:

通过观察CalculatorAspect类中前面的代码可以发现,在@Before,@After,@AfterReturning、@AfterThrowing和@Around注解中切入点表达式均由pointcut()替代。

是因为@Before,@After,@AfterReturning、@AfterThrowing和@Around注解中切入点表达式相同,为了简化代码,我们选择通过单独自定义一个@Pointcut注解修饰的空方法的方式,对@Before,@After,@AfterReturning、@AfterThrowing和@Around注解中的切入点表达式进行简化,具体代码如下:

@Pointcut("execution(public int com.jd.calculator.service.CalculatorService.*(..))")
    public void pointcut(){
    }

XML配置AOP

通过XML配置AOP是Spring框架专有的,但由于AspectJ得到越来越多的AOP框架支持,所以通过注解实现AOP将会有更多重用的机会。通过XML配置实现AOP。

CalculatorAspect类中代码展示:

package com.jd.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

public class CalculatorAspect {

    @Pointcut("execution(public int com.jd.calculator.service.CalculatorService.*(..))")
    public void pointcut(){
    }
    
    public void before(JoinPoint jp){
        Object [] args = jp.getArgs();
        String name = jp.getSignature().getName();
        System.out.println("The "+name+" method begins");
        System.out.println("The parameters of the  "+name+"  method are "+args[0]+","+args[1]);
    }
    
    public void after(JoinPoint jp){
        String name = jp.getSignature().getName();
        System.out.println("The "+name+" method ends");
    }

    public void afterReturning(JoinPoint jp,Object obj){
        String name = jp.getSignature().getName();
        System.out.println("The "+name+" method result:"+obj);
    }

    public void afterThrowing(JoinPoint jp,Throwable e){
        System.out.println("#############"+e.getMessage());
        e.printStackTrace();
    }
}

application.xml文件代码展示:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


<bean id="calculatorAspect" class="com.jd.aspect.CalculatorAspect"></bean>

    <aop:config>
        <aop:pointcut id="p" expression="execution(public int com.jd.calculator.service.CalculatorService.*(..))"/>
        <aop:aspect ref="calculatorAspect">
            <aop:before pointcut-ref="p" method="before"></aop:before>
            <aop:after pointcut-ref="p" method="after"></aop:after>
            <aop:after-returning pointcut-ref="p" method="afterReturning" returning="obj"></aop:after-returning>
            <aop:after-throwing pointcut-ref="p" method="afterThrowing" throwing="e"></aop:after-throwing>
        </aop:aspect>
    </aop:config>

以此种方法实现在XML文件中实现AOP。

标签:Java,name,Spring,jp,System,AOP,println,public,out
来源: https://blog.csdn.net/zzu957/article/details/109959751

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有