Spring-AOP的五种通知方式

AOP的五种通知方式:

前置通知:在我们执行目标方法之前运行(@Before

后置通知:在我们目标方法运行结束之后,不管有没有异常(@After

返回通知:在我们的目标方法正常返回值后运行(@AfterReturning

异常通知:在我们的目标方法出现异常后运行(@AfterThrowing

环绕通知:目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,joinPoint.procced()就是执行目标方法的代码 。环绕通知可以控制返回对象(@Around)

一、导jar包

com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

二、在类路径下建applicationContext.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: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-3.0.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--配置自动扫描的包-->
    <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>

    <!--配置自动为匹配aspectJ 注解的Java类生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

三、接口

//接口
public interface ArithmeticCalculator {

    int add(int i, int j);

    int sub(int i, int j);

    int mul(int i, int j);

    int div(int i, int j);
}

四、实现类

package com.atguigu.spring.aop;

import org.springframework.stereotype.Component;

/**
 * @Author 谢军帅
 * @Date2019/12/6 21:23
 * @Description
 */

//实现类
@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    @Override
    public int add(int i, int j) {
        int relust = i+j;
        return relust;
    }

    @Override
    public int sub(int i, int j) {
        int relust = i-j;
        return relust;
    }

    @Override
    public int mul(int i, int j) {
        int relust = i*j;
        return relust;
    }

    @Override
    public int div(int i, int j) {
        int relust = i/j;
        return relust;
    }
}

五、定义切面类

package com.atguigu.spring.aop;

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

import java.util.Arrays;

/**
 * @Author 谢军帅
 * @Date2019/12/11 11:17
 * @Description
 */
@Component
@Aspect
public class LoggingAspect {
    /**
     * 在每一个接口的实现类的每一个方法开始之前执行一段代码
     */

    @Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();

        System.out.println("The method "+methodName+" begins with "+ Arrays.asList(args));
    }

    @After("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();

        System.out.println("The method "+methodName +" end......");
    }

    /**
     * 返回通知
     * 在方法正常结束后执行的代码
     * 返回通知是可以访问方法的返回值的!
     * @param joinPoint*/

    @AfterReturning(value = "execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))",
                    returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method "+methodName +" end......result:"+result);
    }

    /**
     * 在目标方法出现异常时会执行的代码
     * 可以访问到异常对象,且可以指定在出现特定异常时在执行通知代码
     * @param joinPoint
     * @param ex*/

    @AfterThrowing(value = "execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method "+methodName +"occurs exception :" +ex);
    }

    /**
     * 环绕通知需要携带 ProceedingJoinPoint 类型的参数
     * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint 类型的参数可以决定是否执行目标方法。
     * 且环绕通知必须有返回值,返回值即为目标方法的返回值
     * @param proceedingJoinPoint
     */
    /*@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){

        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();

        try {
            //前置通知
            System.out.println("The method "+methodName+" begins with "+Arrays.asList(proceedingJoinPoint.getArgs()));
            //执行目标方法
            result = proceedingJoinPoint.proceed();

            //返回通知
            System.out.println("The method ends with "+result);
        } catch (Throwable e) {
            //异常通知
            System.out.println("The method occurs exception:"+e);

            throw new RuntimeException(e);
        }

        //后置通知
        System.out.println("The method "+methodName+" ends........");

        return result;
    }*/
}

六、测试

public class Test_aop {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) context.getBean("arithmeticCalculator");

        System.out.println(arithmeticCalculator.getClass().getName());

        int result = arithmeticCalculator.add(1,2);
        System.out.println("result:"+result);

        result = arithmeticCalculator.div(200,0);
        System.out.println("result:"+result);

    }
}

原文地址:https://www.cnblogs.com/xjs1874704478/p/12024944.html

时间: 2024-08-27 13:45:27

Spring-AOP的五种通知方式的相关文章

spring aop的五种通知类型

昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. 回答问题的关键是,你得知道aop有哪几种通知类型吧! spring aop通知(advice)分成五类: 前置通知[Before advice]: 在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常. 正常返回通知[After returning advice]: 在连接点正常执行完

Java Spring AOP的两种配置方式

第一种:注解配置AOP java中注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).2. 开发需要被拦截的类.3. 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式.这样的话,那就交由Spring AoP容器管理. 另外需要引用 aspectJ 的 jar

Spring事务的五种实现方式

前段时间对spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分只是会根据数据访问方式有所变化,

spring AOP的两种配置方式

连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他如AspectJ还可以让你在构造器或属性注入时都行,不过那不是咱们关注的,只要记住,和方法有关的前前后后都是连接点. 方式一:xml方式配置 1.配置xml文件 <bean id="dataSourceExchange" class="com.ooper.www.datas

spring AOP 的几种实现方式(能测试)

我们经常会用到的有如下几种 1.基于代理的AOP 2.纯简单Java对象切面 3.@Aspect注解形式的 4.注入形式的Aspcet切面 一.需要的java文件 public class ChenLliNa implements Sleepable { @Override public void sleep() { // TODO Auto-generated method stub System.out.println("乖,该睡觉了!s"); } public void slee

5、Spring AOP的几种通知(xml)

1.前置.后置.返回.异常和环绕 1 public interface Calculator { 2 3 int add(int a, int b); 4 5 int sub(int a, int b); 6 } 7 8 public class CalculatorImpl implements Calculator { 9 10 @Override 11 public int add(int a, int b) { 12 return a + b; 13 } 14 15 @Override

spring ----&gt; aop的两种实现方式

实现1:基于xml 1 package com.rr.spring3.interf; //接口 2 3 public interface SayHello { 4 5 public void sayHello(); 6 } 1 package com.rr.spring3.interf.impl; //接口实现类 2 3 import com.rr.spring3.interf.SayHello; 4 5 public class Hello implements SayHello { 6 pu

spring框架学习6:spring-aop的五种通知类型

使用springaop时需要注意,如果bean对象,即service层的对象没有实现接口的话,使用spring-aop的话会报错,因此需要在service层创建接口. spring-aop的基层是基于动态代理来实现的,动态代理的实现有两种方式: 1.jdk动态代理 spring模式默认使用jdk动态代理,jdk动态代理要求目标类的对象必须实现一个接口,而且获取目标类对象的时候要做向上转型为接口. 2.cglib动态代理 cglib代理方式spring aop也支持,cglib实现动态代理的时候,

Spring事务Transaction配置的五种注入方式详解

Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ??? 总结如下: ??? Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. ???DataSou