【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut

简介: 【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut

这篇博客讲了AOP代理-通知的3种方式:

1、MethodBeforeAdvice-前置通知

2、AfterReturningAdvice-正常返回后通知

3、MethodInterceptor-环绕通知

【框架】[Spring]纯Java的方式实现AOP切面(拦截)技术

现在本篇博客再详细讲解一下ThrowsAdvice-异常通知。

顾明思议,就是被代理的原型对象出异常了,就会运行到实现此接口中的一个方法。

这个和AfterReturningAdvice互补哦。

被代理的类:

package cn.hncu.javaImpl;
public class Person {
    public void run(){
        System.out.println("我在run...");
    }
    public void run(int i){
        System.out.println("我在run"+i+"...");
        throw  new  IllegalArgumentException();  
    }
    public void say(){
        System.out.println("我在say...");
    }
}

实现ThrowsAdvice的方法:

package cn.hncu.javaImpl;
import org.springframework.aop.ThrowsAdvice;
public class ThrowException implements ThrowsAdvice{
    public  void  afterThrowing(Exception e)  throws  Throwable{  
        System.out.println("出异常了..."+e);
    }
}

运行的方法:

@Test
    public void demo3(){
        ProxyFactoryBean factory = new ProxyFactoryBean();
        factory.setTarget(new Person());//给代理工厂一个原型对象
        //切面 = 切点 + 通知
        //切点
        JdkRegexpMethodPointcut cut = new JdkRegexpMethodPointcut();
        cut.setPatterns(new String[]{".*run.*",".*say.*"});//可以配置多个正则表达式
        Advice throwsAdvice = new ThrowException();
        //切面 = 切点 + 通知
        Advisor throwsAdviceAdvisor = new DefaultPointcutAdvisor(cut, throwsAdvice);
        factory.addAdvisors(throwsAdviceAdvisor);
        Person p2 = (Person) factory.getObject();//从代理工厂中获取一个代理后的对象
        p2.run();
        p2.run(2222);
    }

运行结果:

image.png

有几个需要注意的地方:

1、就是原型对象的异常不能抓!一旦抓取就无法运行afterThrowing。

也就是只有出异常了,且没被抓,才会运行这个方法。

2、不能在运行的方法中直接new ThrowsAdvice然后实现afterThrowing方法,这样因为出异常,线程挂了,也会无法运行这个afterThrowing方法。

也就是不能在测试的方法中直接:

Advice throwsAdvice = new ThrowsAdvice() {
            public  void  afterThrowing(Exception e)  throws  Throwable{  
                System.out.println("出异常了..."+e);
            }
        };

这样也无法实现原型对象处异常拦截。

ThrowsAdvice源代码分析:

直接看ThrowsAdvice接口的源代码:

/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.aop;
/**
 * Tag interface for throws advice.
 *
 * <p>There are not any methods on this interface, as methods are invoked by
 * reflection. Implementing classes must implement methods of the form:
 *
 * <pre class="code">void afterThrowing([Method, args, target], ThrowableSubclass);</pre>
 *
 * <p>Some examples of valid methods would be:
 *
 * <pre class="code">public void afterThrowing(Exception ex)</pre>
 * <pre class="code">public void afterThrowing(RemoteException)</pre>
 * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, Exception ex)</pre>
 * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)</pre>
 *
 * The first three arguments are optional, and only useful if we want further
 * information about the joinpoint, as in AspectJ <b>after-throwing</b> advice.
 *
 * <p><b>Note:</b> If a throws-advice method throws an exception itself, it will
 * override the original exception (i.e. change the exception thrown to the user).
 * The overriding exception will typically be a RuntimeException; this is compatible
 * with any method signature. However, if a throws-advice method throws a checked
 * exception, it will have to match the declared exceptions of the target method
 * and is hence to some degree coupled to specific target method signatures.
 * <b>Do not throw an undeclared checked exception that is incompatible with
 * the target method's signature!</b>
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see AfterReturningAdvice
 * @see MethodBeforeAdvice
 */
public interface ThrowsAdvice extends AfterAdvice {
}

你会发现里面并没有一个抽象方法!也行会有小伙伴迷茫,那为什么我们要实现那个方法啊。

没办法,因为我们是用Spring的框架,Spring内部用类反射来匹配了的,实现这个接口必须要实现这4个方法中的一个:

public void afterThrowing(Exception ex)
public void afterThrowing(RemoteException)
public void afterThrowing(Method method, Object[] args, Object target, Exception ex)
public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)

它的源代码有解释的,英文好一点就能看懂啦。

至于为什么不直接在这个接口中写这4个抽象类,可能怕代码太冗余吧。

毕竟,我们实现这个接口,我们用到的方法只会有一个,而如果都被声明成抽象方法了,那么,用户实现接口也必须实现这4个方法,显得冗余了。

所以估计Spring就干脆定义成标识接口了吧。

目录
相关文章
|
2天前
|
XML Java 数据格式
Spring使用AOP 的其他方式
Spring使用AOP 的其他方式
12 2
|
2天前
|
XML Java 数据格式
Spring 项目如何使用AOP
Spring 项目如何使用AOP
16 2
|
2天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
6天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
20 2
|
7天前
|
Java 开发者 Spring
Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
【5月更文挑战第1天】Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
20 5
|
7天前
|
XML Java 数据格式
Spring AOP
【5月更文挑战第1天】Spring AOP
24 5
|
3月前
|
Java 数据库连接 应用服务中间件
Spring5源码(39)-Aop事物管理简介及编程式事物实现
Spring5源码(39)-Aop事物管理简介及编程式事物实现
26 0
|
4月前
AOP&面向切面编程
AOP&面向切面编程
56 0
|
4月前
|
Java 程序员 Maven
Spring AOP入门指南:轻松掌握面向切面编程的基础知识
Spring AOP入门指南:轻松掌握面向切面编程的基础知识
|
4月前
|
数据库
AOP(面向切面编程)的基本概念和原理
AOP(面向切面编程)的基本概念和原理
85 0