Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

AOP Advice,Pointcut,Joinpoint和Advisor 定义:Advice是一个接口,是AOP中的一个核心概念,表示切面在连接点(程序执行的特定点,如方法的调用或异常的抛出)上执行的操作。用于定义在连接点上执行的增强逻辑。它本身不是连接点,而是在连接点触发时执行的一段代码。作用:Advice用于在方法执行前、执行后、抛出异常时等不同的执行点插入额外的逻辑,类似于AOP中的切面。它可以用于在不修改原始方法的情况下,实现一些横切关注点的功能,比如日志记录、性能监控、事务管理等。是程序执行过程中某个特定的位置,例如方法调用前、调用后或抛出异常后等。 阅读详情

Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

Spring3系列10- Spring AOP——Pointcut,Advisor

 

  上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都被自动的拦截了。但是大多情况下,你只需要一个方法去拦截一两个method。这样就引入了Pointcut(切入点)的概念,它允许你根据method的名字去拦截指定的method。另外,一个Pointcut必须结合一个Advisor来使用。

 

在Spring AOP中,有3个常用的概念,Advices、Pointcut、Advisor,解释如下,

Advices:表示一个method执行前或执行后的动作。

Pointcut:表示根据method的名字或者正则表达式去拦截一个method。

Advisor:Advice和Pointcut组成的独立的单元,并且能够传给proxy factory 对象。

 

下边来回顾一下上一篇例子中的代码

CustomerService.java

复制代码
package com.lei.demo.aop.advice;

public class CustomerService {

    private String name;
    private String url;
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public void printName() {
        System.out.println("Customer name : " + this.name);
    }
 
    public void printURL() {
        System.out.println("Customer website : " + this.url);
    }
 
    public void printThrowException() {
        throw new IllegalArgumentException();
    }

}
复制代码

 

 配置文件Spring-AOP-Advice.xml:

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
        <property name="name" value="LeiOOLei" />
        <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
    </bean>
    
    <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" />
 
    <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="customerService" />
        <property name="interceptorNames">
            <list>
                <value>hijackAroundMethodBean</value>
            </list>
        </property>
    </bean>
 
</beans>
复制代码

 

HijackAroundMethod.java

复制代码
package com.lei.demo.aop.advice;

import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class HijackAroundMethod implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Method name : "
                + methodInvocation.getMethod().getName());
        System.out.println("Method arguments : "
                + Arrays.toString(methodInvocation.getArguments()));
 
        // 相当于  MethodBeforeAdvice
        System.out.println("HijackAroundMethod : Before method hijacked!");
 
        try {
            // 调用原方法,即调用CustomerService中的方法
            Object result = methodInvocation.proceed();
 
            // 相当于 AfterReturningAdvice
            System.out.println("HijackAroundMethod : After method hijacked!");
 
            return result;
 
        } catch (IllegalArgumentException e) {
            // 相当于 ThrowsAdvice
            System.out.println("HijackAroundMethod : Throw exception hijacked!");
            throw e;
        }
    }

}
复制代码

 

运行如下App.java

复制代码
package com.lei.demo.aop.advice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext(
                new String[] { "Spring-AOP-Advice.xml" });

        
        System.out.println("使用Spring AOP 如下");
        CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
        System.out.println("*************************");
        cust.printName();
        System.out.println("*************************");
        cust.printURL();
        System.out.println("*************************");
        
        try {
            cust.printThrowException();
        } catch (Exception e) {
 
        }
 
    }

}
复制代码

 

 运行结果:

使用Spring AOP 如下

*************************

Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer name : LeiOOLei

HijackAroundMethod : After method hijacked!

*************************

Method name : printURL

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer website : http://www.cnblogs.com/leiOOlei/

HijackAroundMethod : After method hijacked!

*************************

Method name : printThrowException

Method arguments : []

HijackAroundMethod : Before method hijacked!

HijackAroundMethod : Throw exception hijacked!

 

上边的结果中,CustomerService.java中,全部的method方法全部被拦截了,下边我们将展示怎样利用Pointcuts只拦截printName()。

 

你可以用名字匹配法和正则表达式匹配法去匹配要拦截的method。

1.      Pointcut——Name match example

通过pointcut和advisor拦截printName()方法。

创建一个NameMatchMethodPointcut的bean,将你想拦截的方法的名字printName注入到属性mappedName,如下

<bean id="customerPointcut"
        class="org.springframework.aop.support.NameMatchMethodPointcut">
        <property name="mappedName" value="printName" />
</bean>

 

 创建一个DefaultPointcutAdvisor的advisor bean,将pointcut和advice关联起来。

<bean id="customerAdvisor"
        class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="pointcut" ref="customerPointcut" />
        <property name="advice" ref=" hijackAroundMethodBean " />
</bean>

 

更改代理的interceptorNames值,将上边的advisor( customerAdvisor)替代原来的hijackAroundMethodBean。

复制代码
<bean id="customerServiceProxy"
        class="org.springframework.aop.framework.ProxyFactoryBean">
 
        <property name="target" ref="customerService" />
 
        <property name="interceptorNames">
            <list>
                <value>customerAdvisor</value>
            </list>
        </property>
</bean>
复制代码

 

所有的配置文件如下:

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
        <property name="name" value="LeiOOLei" />
        <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
    </bean>
 
    <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" />
 
    <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
 
        <property name="target" ref="customerService" />
 
        <property name="interceptorNames">
            <list>
                <value>customerAdvisor</value>
            </list>
        </property>
    </bean>
 
    <bean id="customerPointcut"class="org.springframework.aop.support.NameMatchMethodPointcut">
        <property name="mappedName" value="printName" />
    </bean>
 
    <bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="pointcut" ref="customerPointcut" />
        <property name="advice" ref=" hijackAroundMethodBean " />
    </bean>
 
</beans>
复制代码

 

 再运行一下App.java,输出结果如下:

使用Spring AOP 如下

*************************

Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer name : LeiOOLei

HijackAroundMethod : After method hijacked!

*************************

Customer website : http://www.cnblogs.com/leiOOlei/

*************************

 

以上运行结果显示,只拦截了printName()方法。

 

注意:

以上配置中pointcut和advisor可以合并在一起配置,即不用单独配置customerPointcut和customerAdvisor,只要配置customerAdvisor时class选择NameMatchMethodPointcutAdvisor如下:

<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="mappedName" value="printName" />
        <property name="advice" ref="hijackAroundMethodBean" />
</bean>

 

这样,整个配置文件如下:

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
        <property name="name" value="LeiOOLei" />
        <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
    </bean>
    
    <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" />
 
    <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="customerService" />
        <property name="interceptorNames">
            <list>
                <value>customerAdvisor</value>
            </list>
        </property>
    </bean>
    
    
    <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="mappedName" value="printName" />
        <property name="advice" ref="hijackAroundMethodBean" />
    </bean>
 
</beans>
复制代码

 

  实际上这种做法将method名字与具体的advice捆绑在一起,有悖于Spring松耦合理念,如果将method名字单独配置成pointcut(切入点),advice和pointcut的结合会更灵活,使一个pointcut可以和多个advice结合。

2.      Pointcut——Regular exxpression match example

你可以配置用正则表达式匹配需要拦截的method,如下配置

复制代码
<bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="patterns">
            <list>
                <value>.*URL.*</value>
            </list>
        </property>
        <property name="advice" ref="hijackAroundMethodBeanAdvice" />
    </bean>
复制代码

 

现在,你可以拦截名字中包含URL字符的method了,在实际工作中,你可以用它来管理DAO层,例如,你可以用“.*DAO.*”来拦截所有DAO层中的相关业务。

 

 

 

 

 

分类:  Spring,  AOP
标签:  AOP,  Advisor,  Spring AOP

Python 爬虫进阶技巧:定时爬虫任务实现无人值守采集 常规爬虫多依赖手动触发脚本运行,单次采集完成后需人工二次启动,无法满足日常周期性数据监控、行情抓取、资讯同步、业务台账定时归档等常态化采集需求。搭建可自主调度、自动启停、周期循环的定时爬虫任务,脱离人工干预实现无人值守全自动采集,是爬虫从临时脚本走向工程化落地的关键一步。schedule 轻量定时库官方文档APScheduler 高级定时框架官方文档Python time 内置模块官方文档Linux crontab 定时任务官方手册。 阅读详情

相关推荐

从零搭建:基于Matlab/Simulink与FlightGear的飞行器实时可视化仿真平台

本文详细介绍了如何从零搭建基于Matlab/Simulink与FlightGear的飞行器实时可视化仿真平台。通过环境准备、模型部署、接口配置及联合仿真等步骤,帮助用户快速掌握飞行器仿真的核心技术,实现高效的三维可视化与实时数据交互。

weixin_29100399的博客 517

Springboot的切点可配置化-DefaultPointcutAdvisor

问题现象:之前为解决异常的统一处理方式,写了异常处理切面,但是随着微服务节点越来越多,会出现每个微服务都需要单独写一个异常处理的切面,比较麻烦,因此考虑将切点可配置,切面提取到公共包内,但是几经尝试,@Aspect的方式始终无法读取配置的切点,最终使用DefaultPointcutAdvisor解决。 原理: DefaultPointcutAdvisor是Spring默认的一个很强大的增强类,使用...

sinat_31396769的博客 7892

Cursor AI编程工具保姆级安装指南:从下载到中文界面设置(含免费额度说明)

本文提供Cursor AI编程工具的保姆级安装与设置教程。详细指导从官网下载、安装到中文界面切换的每一步,并重点说明其每月500次高级模型调用的免费额度策略。通过个性化设置与核心插件推荐,帮助开发者快速上手,提升AI辅助编程效率。

weixin_29191761的博客 672

Spring源码分析----AOP概念(Advice,Pointcut,Advisor)和AOP的设计与实现

一、基本知识 1.1.AOP基本概念: Aspect-Oriented Programming,面向方面编程的简称,Aspect是一种新的模块化机制,用来描述分散在对象、类或方法中的横切关注点(crosscutting concern),从关注点中分离出横切关注点是面向方面程序设计的核心所在。分离关注点使得解决特定领域问题的代码从业务逻辑中独立出来,业务逻辑的代码中不再含有针对特定领域

a657281084的博客 5139

华为HCIA-Datacom实战:200人企业园区网从规划到部署全流程解析

本文详细解析了华为HCIA-Datacom认证实战中200人企业园区网的规划与部署全流程。从需求分析、组网架构设计到设备选型、VLAN划分及安全策略实施,全面覆盖园区网建设的关键环节,特别针对部门隔离、无线覆盖和基础安全防护等典型场景提供可落地的解决方案。

f0g1h2的博客 711

A robust and easy to implement method for IMU calibration without external equipments(IMU标定)

A robust and easy to implement method for IMU calibration without external equipments 1 标定原理1 亲试可用,还有动态演示效果不错。相比商用产品的简易标定流程,该标定方法有以下几点不同: 给出了加速度计和陀螺仪更复杂的模型。 利用Allan方差的定义,标定陀螺仪的偏置。 大量冗余数据,保证最优化收敛。利用数十...

擦擦擦大侠的博客 3948

spring之AOP源码分析上

此方式就是我们编写一个切面类,里边包含切面方法,像@Befoer、@After等,通过扫描Spring容器中的Advisor,为匹配的Bean创建代理对象,并将Advisor中定义的通知应用到代理对象上。@Bean@Bean上述方式为非注解方式,注解方式就是@Before、@After,如果是注解方式会通过@EnableAspectJAutoProxy注解去解析,后续会分析。

尽力而为! 1269

SAP FICO 资产会计AA后台配置 (上)

输入折旧表“CZ11”如有外币计价的资产,则需指定外币计价的资产折旧范围,以及规则。因为在设置资产类型时,需指定一个对应的 资产编号范围、资产账户确定类型(便于分配自动会计科目)、屏幕格式规则(便于设置资产字段必填与否)。因为在设置资产类型时,需指定一个对应的 资产编号范围、资产账户确定类型(便于分配自动会计科目)、屏幕格式规则(便于设置资产字段必填与否)。新增资产类型:Z101 固定资产-房屋及建筑物新增资产类型:Z301 固定资产-房屋及建筑物。

ghgxm520的博客 1762

spring aop

连接点joinpoint:可以织入代码的位置,方法前后,出异常时等位置 切点pointcut:用PointCut接口进行描述,切点功能上类似一个正则表达式,描述了哪些类的哪些方法符合条件,具体不到方法前还是方法后等连接点 增强advice:织入到连接点上的方法,定位了大致连接点(方法前,方法后,异常时等)和要织入的代码 目标对象target:要织入的代码的目标类 引介introductio

linchunhua的专栏 1625

【实用运维脚本】Windows 批量 TCP 端口检测(tcping 一键巡检)

摘要:本文介绍一款高效的Windows批量TCP端口检测脚本,适配tcpingv0.39工具,实现多服务器一键巡检。脚本具有进度可视化、双日志输出、自动容错等优势,支持注释行过滤和UTF-8编码。 使用仅需三步: 1)下载tcping工具 2)编辑目标列表 3)运行脚本即可自动检测并生成带编号的检测报告。 该方案解决了手动检测效率低、易遗漏的问题,特别适合日常运维中的端口连通性检查,实测稳定可靠,开箱即用。

蕲士|D365 & 服务器运维,心有东坡,行稳致远。 799

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的零售柜商品检测软件(Python+PySide6界面+训练代码)

随着新零售概念的兴起,无人零售柜、智能零售店等新型零售模式快速发展。传统零售柜依赖于人工盘点和补货,效率低下且成本高昂。本文提出了一种基于YOLO系列目标检测算法的零售柜商品检测系统,实现了对零售柜内商品的自动识别、计数和库存管理。系统采用PySide6开发用户界面,集成了YOLOv5、YOLOv6、YOLOv7和YOLOv8等多种先进的深度学习模型,提供了完整的训练和推理流程。本文将详细介绍系统设计、算法实现、数据集构建和实际应用效果,并提供完整的代码实现。

YOLO 333

Spring3系列10-Spring AOP——Pointcut,Advisor拦截指定方法

上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都被自动的拦截了。但是大多情况下,你只需要一个方法去拦截一两个method。这样就引入了Pointcut(切入点)的概念,它允许你根据method的名字去拦截指定的method。另外,一个Pointcut必须结合一个Advisor来使用。 在Spring AOP中,有3个常用的...

weixin_30547797的博客 204

Android AVB 分析(十六)5 个例子彻底理解 FEC(Reed-Solomon) 的工作原理

关于 FEC 是如何工作的,一直是我 Android OTA 讨论群中的未解话题,大家基本上都不清楚 Android 上面的 FEC 是如何工作的,为什么在 OTA 升级的最后阶段,FEC 的编码计算会消耗大量的 IO 资源,并持续很长时间。接下来的几篇,我会从一个程序员的角度,演示基于里德所罗门编码的 FEC 到底是如何工作的。

洛奇看世界 669

博客摘录「 Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法」2023年4月11日

上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都被自动的拦截了。但是大多情况下,你只需要一个方法去拦截一两个method。这样就引入了Pointcut(切入点)的概念,它允许你根据method的名字去拦截指定的method。另外,一个Pointcut必须结合一个Advisor来使用。

m0_50272029的博客 121

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

聊聊spring aop中的advisor组件

在spring aop中,

cyh的博客 7358

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Spring AOP 拦截指定方法

在Spring AOP中,有3个常用的概念,Advices、Pointcut、Advisor,解释如下,Advices:表示一个method执行前或执行后的动作。Pointcut:表示根据method的名字或者正则表达式去拦截一个method。Advisor:Advice和Pointcut组成的独立的单元,并且能够传给proxy factory 对象。package testaop; publi...

wanwei1987的博客 1万+

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Spring中的Advisor,Advice,Pointcut,Advised

        在Spring中,相关的api提供了对某类方法的拦截,也提供了对某些类的代理,以下是本人对spring参考手册的理解,纯属一家之言。对方法的拦截通过定义配置Advisior,Advice,Pointcut而实现;通过Advised接口实现代理类的生成,以下分两方面拦截者和代理类来阐述I、拦截者一、Spring中的Advisor,Advice,Point概述1、Advis

knight117的专栏 4809

聊聊Spring AOP 中的Advisor

平时我们项目中涉及到 AOP,基本上就是声明式配置一下就行了,无论是基于 XML 的配置还是基于 Java 代码的配置,都是简单配置即可使用。声明式配置有一个好处就是对源代码的侵入小甚至是零侵入。不过今天松哥要和小伙伴们聊一聊编程式的 AOP,为什么要聊这个话题呢?因为在 Spring 源码中,底层就是通过这种方式创建代理对象的,所以如果自己会通过编程式的方式进行 AOP 开发,那么在看 Spring 中相关源码的时候,就会很好理解了。

LoveSummer 1万+

Spring AOP源码总结

1、Spring创建bean的过程中,会在bean后置处理器的bean初始后方法中判断当前类是否需要进行AOP(产生代理对象)2、Spring从容器中找到所有的Advisor(包括切面类中的@Before、@After等注解修饰的方法也会构建成Advisor)3、获取所有匹配的Advisor调用Advisor.Pointcut.MethodMatcher的match方法,只要方法匹配器(MethodMatcher)与当前类(正在创建的bean)的任意一个方法匹配即可。

qq_34101321的博客 1850

Spring AOP Example – Pointcut , Advisor

In last Spring AOP advice examples, the entire methods of a class are intercepted automatically. But for most cases, you may just need a way to intercept only one or two methods, this is what ‘Pointcut

596

Spring Advisor增强规则实现原理介绍

一、什么是 Advisor?1. Advisor 的定义与本质2. Advisor 的核心作用统一封装切点与通知构建拦截器链的基础实现增强逻辑的灵活组合二. Sprin当中的实现逻辑1 `Advisor` 接口定义2 `PointcutAdvisor` 接口定义3 `AspectJExpressionPointcutAdvisor` 实现类AOP相关核心概念与作用说明(补充)AdvisorAdvice:增强逻辑,即实际执行的横切代码。Pointcut:切点逻辑,定义增强逻辑在哪些连接点(方法)生效。

几许的博客 1238

AOP - Pointcut & Advisor

知识点 Pointcut Advisor

Distance_NBA的博客 524
上一篇: Spring3系列9- Spring AOP——Advice
下一篇: The type org.springframework.beans.BeansException cannot be resolved. It is indirectly referenced fr
javaPie
博客等级 码龄16年 1171粉丝 43原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值