SpringAOP 通知(adivce)- methodIntercepor

简介: 环绕通知(即methodIntercepor) 是SpringAOP 通知模块中的一种通知方式。可用在指定方法执行之前,执行之后。对于同时要实现两种通知的方法是一种便利。若使用BeforeAdivce和afterreturningadvice则显得太多与繁琐。 可通过实现MethodInterceptor接口来实现环绕通知。1.MethodInterceptor接口源代

环绕通知(即methodIntercepor) 是SpringAOP 通知模块中的一种通知方式。可用在指定方法执行之前,执行之后。对于同时要实现两种通知的方法是一种便利。若使用BeforeAdivce和afterreturningadvice则显得太多与繁琐。
可通过实现MethodInterceptor接口来实现环绕通知。

1.MethodInterceptor接口源代码

public interface MethodInterceptor extends Interceptor {
    Object invoke(MethodInvocation var1) throws Throwable;
}

2.具体小案例实现环绕通知。

2.1 具体的业务类
   /**
     * 目标业务类

     * Created by engle on 16-5-14.
     */
    public class Target{

        public void log() {
            System.out.println("日志信息");
        }
    }
2.2 环绕通知实现类
/**
 * Created by engle on 16-5-14.
 */
public class InterceptorMessage implements MethodInterceptor {

    /**
     *
     * @param methodInvocation 调用方法对象
     * @return 放回的对象
     * @throws Throwable 抛出的异常
     */
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        System.out.println("在目标方法执行的之前的环绕通知");
        Object proceed = methodInvocation.proceed(); //通过反射机制获取代理对象执行的方法
        System.out.println("在目标方法执行之后的环绕通知");
        return proceed;
    }
}
2.3测试类
/**
 * 测试类
 * Created by engle on 16-5-14.
 */
public class Test {
    public static void main(String[] args) {

        Target target = new Target();
        BeforeAdvice before = new BeforeMessage();
        AfterMessage after = new AfterMessage();
        MethodInterceptor interceptor = new InterceptorMessage();

        ProxyFactory factory = new ProxyFactory();  //设置代理工厂
        factory.addAdvice(interceptor);
        factory.setTarget(log);     //添加执行目标信息
        Target proxy = (Target) factory.getProxy(); //获取代理对象
        proxy.log();

    }
}
2.4测试结果
在目标方法执行的之前的环绕通知
日志信息
在目标方法执行之后的环绕通知
目录
相关文章
|
存储 运维 监控
深入Linux核心:文件系统与日志解析
【8月更文挑战第20天】
222 2
|
Java API 开发工具
Spring Boot与Spring Cloud Config的集成
Spring Boot与Spring Cloud Config的集成
|
设计模式 Java C++
揭秘!JDK动态代理VS CGLIB:一场关于Java代理界的‘宫心计’,你站哪队?
【8月更文挑战第24天】Java 动态代理是一种设计模式,允许在不改动原类的基础上通过代理类扩展功能。主要实现方式包括 JDK 动态代理和 CGLIB。前者基于接口,利用反射机制在运行时创建代理类;后者采用继承方式并通过字节码技术生成子类实现类的代理。两者在实现机制、性能及适用场景上有明显差异。JDK 动态代理适用于有接口的场景,而 CGLIB 更适合代理未实现接口的类,尽管性能更优但存在一些限制。开发者可根据需求选择合适的代理方式。
415 0
|
Java 数据库连接 Spring
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could
这个错误通常出现在使用Spring Boot进行数据库连接时。错误信息表明Spring Boot未能配置一个DataSource,因为没有指定'url'属性,并且没有发现默认的数据库连接。
2952 0
|
11月前
|
JavaScript
vue3,使用watch监听props中的数据
【10月更文挑战第3天】
3545 2
|
缓存 前端开发 应用服务中间件
Nginx:location配置模块的用法(二)
Nginx:location配置模块的用法(二)
867 2
简单易操作 VsCoe离线安装插件【步骤+图片+插件】
这篇文章介绍了在Visual Studio Code (VSCode) 中进行离线安装插件的详细步骤,包括如何下载插件、以SVN插件为例的离线安装过程、通过命令行安装以及一个更加简单的离线安装方式,还提供了操作界面的截图帮助理解。
简单易操作 VsCoe离线安装插件【步骤+图片+插件】
|
JavaScript
在Vue中,如何使用`$once`侦听一个事件?
在Vue中,如何使用`$once`侦听一个事件?
229 1
|
存储 消息中间件 JSON
DDD基础教程:一文带你读懂DDD分层架构
DDD基础教程:一文带你读懂DDD分层架构
|
前端开发 开发者
stylus、sass、less区别, Sass 、LESS是什么
【4月更文挑战第1天】stylus、sass、less区别, Sass 、LESS是什么
195 0