【框架】[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就干脆定义成标识接口了吧。

目录
相关文章
|
1月前
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
115 25
|
1月前
|
XML 安全 Java
Spring AOP—深入动态代理 万字详解(通俗易懂)
Spring 第四节 AOP——动态代理 万字详解!
87 24
|
2月前
|
XML Java 开发者
Spring Boot中的AOP实现
Spring AOP(面向切面编程)允许开发者在不修改原有业务逻辑的情况下增强功能,基于代理模式拦截和增强方法调用。Spring Boot通过集成Spring AOP和AspectJ简化了AOP的使用,只需添加依赖并定义切面类。关键概念包括切面、通知和切点。切面类使用`@Aspect`和`@Component`注解标注,通知定义切面行为,切点定义应用位置。Spring Boot自动检测并创建代理对象,支持JDK动态代理和CGLIB代理。通过源码分析可深入了解其实现细节,优化应用功能。
127 6
|
8月前
|
分布式计算 Java MaxCompute
详解 Java 限流接口实现问题之在Spring框架中使用AOP来实现基于注解的限流问题如何解决
详解 Java 限流接口实现问题之在Spring框架中使用AOP来实现基于注解的限流问题如何解决
123 0
|
9月前
|
设计模式 SQL Java
Spring框架第四章(AOP概念及相关术语)
Spring框架第四章(AOP概念及相关术语)
|
10月前
|
安全 Java 开发者
在Spring框架中,IoC和AOP是如何实现的?
【4月更文挑战第30天】在Spring框架中,IoC和AOP是如何实现的?
122 0
|
10月前
|
Java Spring
spring框架 aop:aspectj-autoproxy proxy-target-class=“true“用法理解
spring框架 aop:aspectj-autoproxy proxy-target-class=“true“用法理解
678 0
|
10月前
|
安全 Java 数据库连接
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
118 1
|
Java Spring
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
161 0
|
监控 安全 Java
作为spring框架的另外的重点AOP的介绍(详细篇)
作为spring框架的另外的重点AOP的介绍(详细篇)
133 0