Spring AOP之AspectJ表达式

SpringAspectJ介绍、切入点表达式、Aspect案例xml配置、Aspect案例注解配置、JdbcTemplate简单使用、DBCP、C3P0配置、db.properties配置-day04 目录第一节 AspectJ1.1 AspectJ简介 第一节 AspectJ 1.1 AspectJ简介 AspectJ是一个基于Java语言的AOP框架 Spring2.0以后新增了对AspectJ切点表达式支持 @AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面 新版本Spring框架,建议使用AspectJ方式来开发AOP 主要用途:自定义开发 ... 阅读详情
表达式格式:execution(方法修饰符 返回类型 类.方法(参数类型..))
  • execution(public * *(..))

          - 匹配任意public类型的方法

  • execution(* xx*(..))

          - 匹配"xx"开头的任意方法

  • execution(* xxx.xxx.service.XXXService.*(..))

          - 匹配XXXService类下的所有方法

  • execution(* xxx.xxx.service.*.*(..))

          - 匹配service包下的类和方法

  • execution(* xxx.xxx.service..*.*(..))

          - 匹配service包及子包下的类和方法

  • within(xxx.xxx.service.*)

          - 匹配service包下的类, 必须是包路径, 不能包含类名信息

  • within(xxx.xxx.service..*)

          - 匹配service包及子包下的类, 必须是包路径, 不能包含类名信息

  • this(xxx.xxx.service.XXXService)

          - 代理对象的类型匹配this指定的类型

          * JDK生成的动态代理类:实现接口为目标类的接口, 继承Proxy类
          * CGLIB生成的动态代理类:实现接口为目标类的接口, 继承目标类
          * 当采用JDK动态代理时,代理类和目标类没有直接关系, 二者只是实现相同的接口, 如果this表达式指定的是目标类时无法匹配命中

  • target(xxx.xxx.service.XXXService)

          - 目标对象类型匹配target指定的类型

          * this作用于代理对象,target作用于目标对象
          * this表示目标对象被代理之后生成的代理对象和指定的类型匹配会被拦截,匹配的是代理对象
          * target表示目标对象和指定的类型匹配会被拦截,匹配的是目标对象

  • args(*, int)

          - 匹配参数个数为2的方法,第一个任意类型,第二个int类型

  • @target(Service)

          - 匹配目标对象的类型被@Service注解

  • @within(Service)

          - 匹配被调目标方法所属的类被@Service注解

          * 被调目标方法被子类重新,那么方法就属于子类,@Service在父类上,子类上没有注解,不会被拦截
          * 被调目标方法未被子类重新,那么方法就属于父类,@Service在父类上,父类上有注解,会被拦截

          * @target(注解A):判断被调用的目标对象中是否声明了注解A,如果有,会被拦截
          * @within(注解A): 判断被调用的方法所属的类中是否声明了注解A,如果有,会被拦截
          * @target关注的是被调用的对象,@within关注的是调用的方法所在的类

  • @annotation(Transactional)

          - 匹配被@Transactional注解的方法

  • @args(Service)

          - 匹配参数个数为1的方法,且第一个参数所属的类型上有注解@Service(注意不是参数上的注解,而是参数类型上的注解)

测试代码:
package org.spring.aop.expression;

public class AspectJExpressionTest {

    public static void main(String[] args) {
        testExecution();

        System.out.println("\n");
        testWithin();

        System.out.println("\n");
        testThis();

        System.out.println("\n");
        testTarget();

        System.out.println("\n");
        testArgs();

        System.out.println("\n");
        testAnnotationTarget();

        System.out.println("\n");
        testAnnotationWithin();

        System.out.println("\n");
        testAnnotation();

        System.out.println("\n");
        testAnnotationArgs();
    }

    public static void testExecution() {
        System.out.println("------------test execution()------------");

        String expression = "execution(* org.spring.aop.expression.service.ServiceImpl.*(..))";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new ServiceImpl());
        proxyFactory.addAdvisors(advisor);

        IService service = (IService) proxyFactory.getProxy();
        service.say("execution()");
    }

    public static void testWithin() {
        System.out.println("------------test within()------------");

        String expression = "within(org.spring.aop.expression.service.*)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new ServiceImpl());
        proxyFactory.addAdvisors(advisor);

        IService service = (IService) proxyFactory.getProxy();
        service.say("within()");
    }

    public static void testThis() {
        System.out.println("------------test this()------------");

        String expression = "this(org.spring.aop.expression.service.ServiceImpl)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new ServiceImpl());
        //采用JDK动态代理
        proxyFactory.setProxyTargetClass(false);
        proxyFactory.addAdvisors(ExposeInvocationInterceptor.ADVISOR, advisor);

        IService service = (IService) proxyFactory.getProxy();
        service.say("this()");


        proxyFactory = new ProxyFactory(new ServiceImpl());
        //采用CGLIB动态代理
        proxyFactory.setProxyTargetClass(true);
        proxyFactory.addAdvisors(ExposeInvocationInterceptor.ADVISOR, advisor);

        service = (IService) proxyFactory.getProxy();
        service.say("this()");
    }

    public static void testTarget() {
        System.out.println("------------test target()------------");

        String expression = "target(org.spring.aop.expression.service.ServiceImpl)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new ServiceImpl());
        proxyFactory.addAdvisors(ExposeInvocationInterceptor.ADVISOR, advisor);

        IService service = (IService) proxyFactory.getProxy();
        service.say("target()");
    }

    public static void testArgs() {
        System.out.println("------------test args()------------");

        String expression = "args(*, int)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new ServiceImpl());
        proxyFactory.addAdvisors(advisor);

        IService service = (IService) proxyFactory.getProxy();
        service.say("args()");
        service.say("args()", 5);
    }

    public static void testAnnotationTarget() {
        System.out.println("------------test @target()------------");

        String expression = "@target(org.springframework.stereotype.Service)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new AnnotationService());
        proxyFactory.addAdvisors(ExposeInvocationInterceptor.ADVISOR, advisor);

        AnnotationService service = (AnnotationService) proxyFactory.getProxy();
        service.say();

        System.out.println();
        proxyFactory = new ProxyFactory(new AnnotationServiceImpl());
        proxyFactory.addAdvisors(ExposeInvocationInterceptor.ADVISOR, advisor);
        service = (AnnotationService) proxyFactory.getProxy();
        service.say();
    }

    public static void testAnnotationWithin() {
        System.out.println("------------test @within()------------");

        String expression = "@within(org.springframework.stereotype.Service)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new AnnotationServiceImpl());
        proxyFactory.addAdvisors(advisor);

        AnnotationServiceImpl service = (AnnotationServiceImpl) proxyFactory.getProxy();
        //AnnotationServiceImpl重写了AnnotationService的say方法,方法属于AnnotationServiceImpl但是AnnotationServiceImpl上没有@Service注解
        //不拦截
        service.say();

        //AnnotationServiceImpl未重写AnnotationService的hello方法,方法属于AnnotationService,AnnotationService上有@Service注解
        //拦截
        service.hello();
    }

    public static void testAnnotation() {
        System.out.println("------------test @annotation()------------");

        String expression = "@annotation(org.springframework.transaction.annotation.Transactional)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new AnnotationServiceImpl());
        proxyFactory.addAdvisors(advisor);

        AnnotationServiceImpl service = (AnnotationServiceImpl) proxyFactory.getProxy();
        service.say();
        System.out.println();
        service.doSomthing();
    }

    public static void testAnnotationArgs() {
        System.out.println("------------test @args()------------");
        String expression = "@args(org.springframework.stereotype.Service)";
        Advisor advisor = buildAdvisor(expression);
        ProxyFactory proxyFactory = new ProxyFactory(new AnnotationServiceImpl());
        proxyFactory.addAdvisors(advisor);

        AnnotationServiceImpl service = (AnnotationServiceImpl) proxyFactory.getProxy();
        service.say();

        AnnotationService annotationService = new AnnotationService();
        service.doSomthing(annotationService);
    }

    public static Advisor buildAdvisor(String expression) {
        //创建pointcut
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(expression);
        System.out.println("------------MethodMatcher.isRuntime()=" + pointcut.isRuntime() + "------------");

        //创建advice
        LogAdvice advice = new LogAdvice();

        //创建advisor
        Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);

        return advisor;
    }
}
package org.spring.aop.expression.service;

public interface IService {
    default void say(String name) {}

    default void say(String name, int age) {}
}
package org.spring.aop.expression.service;

public class ServiceImpl implements IService {

    @Override
    public void say(String name) {
        System.out.println("I am " + name + "!!");
    }

    @Override
    public void say(String name, int age) {
        System.out.println("I am " + name + "!! " + age + " years old");
    }
}
package org.spring.aop.expression.service;

import org.springframework.stereotype.Service;

@Service
public class AnnotationService {

    public void say() {
        System.out.println("I am AnnotationService.say()!!");
    }

    public void hello() {
        System.out.println("I am AnnotationService.hello()!!");
    }

    public void doSomthing() {
        System.out.println("I am AnnotationService.doSomthing()!!");
    }
}
package org.spring.aop.expression.service;

import org.springframework.transaction.annotation.Transactional;

public class AnnotationServiceImpl extends AnnotationService {
    @Transactional
    public void say() {
        System.out.println("I am AnnotationServiceImpl.say()!!");
    }

    public void doSomthing(AnnotationService annotationService) {
        System.out.println("I am AnnotationServiceImpl.(@Param(\"name\") String name)!!");
    }
}
package org.spring.aop.expression.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LogAdvice implements MethodInterceptor {
	@Override
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		System.out.println("===============log record start==============");
		Object object = methodInvocation.proceed();
		System.out.println("===============log record end================");
		return object;
	}
}

Spring AOP 只是借鉴了 AspectJ 的注解和切点表达式吗? 答案:不完全是我们可以从两个层面来分析 :下面我们来详细解析这两个层面。这是我们日常开发中最常接触到的模式。借鉴了什么?底层实现是什么?这才是体现两者关系深度的地方。Spring 框架提供了一种机制,可以让你在 Spring 应用中直接使用 AspectJ 的织入 (Weaving) 能力,而不仅仅是它的注解。这通常通过加载时织入 (Load-Time Weaving, LTW) 来实现。加载时织入是指,在 JVM 的类加载器(ClassLoader)加载 文件时,AspectJ 的一个特殊代理(Agen 阅读详情

相关推荐

面向切面的Spring(一) AOP术语、AspectJ表达式说明,execution表达式含有注解写法

AOP术语 如果要重用通用功能的话,常见的面向对象技术是继承或委托,但是如果在整个应用中都是用相同的基类,继承往往会导致一个脆弱的体系,而使用委托则有比较复杂的调用 切面,就是将关注的功能模块化为特殊的类,这类就是切面。 通知Advice: 切面中具体要做的事情,分为前置、后置、返回、异常、环绕通知。 连接点(Joint point): 执行过程中能够插入切面的一个点。 切点(Poi...

z1340954953 1742

Aspectj表达式

Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execution (* com.sample.service.impl….(…)) execution()是最常用的切点函数,其语法如下所示: 整个表达式可以分为五个部分: 1、execution(): 表达...

qq_36498236的博客 1827

工控AI从2026到2030:OPC UA、MCP与数字孪生的落地实践

工业自动化与AI的融合正从概念走向工程落地。其核心原理在于通过标准化协议打通设备层、数据层与AI层,实现感知、决策、执行链路的闭环。OPC UA统一了设备信息模型,MCP则标准化了AI与工具间的调用接口,二者结合将大幅降低系统集成复杂度。技术价值在于提升数据质量与连接可靠性,使AI推理能安全地辅助甚至优化控制。典型应用包括产线数据采集、数字孪生同步、PLC代码生成与边缘智能。本文深入探讨了这些关键技术在工控场景中的实操要点与常见陷阱。

weixin_32025789的博客 192

AspectJ的切入表达式

AspectJ定义了专门的表达式用于切入点,表达式的原型是 execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?) modifiers-pattern 访问权限类型(public,protected,private,默认) ret-type-pattern 返回值类型 (void,简单类型,引用类型等) declaring-t

weixin_56204177的博客 1844

保姆级教程:用Python和PyTorch复现PTB-XL心电信号分类论文(附完整代码)

本文提供了一份详细的PTB-XL心电信号分类教程,涵盖从数据预处理到模型优化的完整流程。通过PyTorch实现三种深度学习架构(Conv1D、SincNet和熵特征融合网络),并针对医疗AI特有的类别不平衡问题提供解决方案。教程包含可运行的代码示例,帮助开发者快速复现论文结果并应用于实际心电信号分析场景。

weixin_30555125的博客 412

《物联网技术》课程笔记——第二章 物联网技术架构

文章目录第二章:物联网技术架构1 物联网技术体系(C3SD)2 系统体系结构的基本概念3 物联网体系结构——信息传递视角3.1物联网感知层3.1.1 感知层要解决的问题3.1.2 感知层的形态3.1.3 节点的感知能力与控制能力3.1.4 感知层实例3.2 物联网网络层3.2.1 网络相关概念3.2.2 IPV6与IPV4相比互联网协议的升级3.2.3 计算机网络的逻辑层次3.2.4 传输网的拓扑结构3.2.4.1 举例:智能小区的三表采集系统3.2.4.2 接入层3.2.4.2 汇聚层3.2.4.2 核心

南京大学软件工程硕士在读 2517

AspectJ 中切点表达式的匹配过程

从源码角度介绍了 AspectJ 中切入点表达式同连接点方法的匹配过程

iCheetor 1147

2025年北京市职工职业技能大赛第六届信息通信行业网络安全技能大赛复赛CTF部分WP-哥斯拉流量分析

题目没有任何提示,附件gzl.pcap解题哥斯拉流量300多KB包很多,没啥经验只能挨个看回来之后又狠狠得撸了一把哥斯拉流量分析我这里用的是哥斯拉4.0.1测试链接这里发了两个包,我们进行分析。

深耕网络安全领域,聚焦护网行动(HW)、渗透测试、等级保护(等保)、CTF 竞赛四大核心方向,提供技术干货、实战经验与行业洞察。 1828

常用 AspectJ 切点表达式

常用 AspectJ 切点表达式

qq_38074398的博客 507

【100%通过率】2023华为od机试C卷【围棋的气】Python 实现

围棋棋盘由纵横各19条线垂直相交组成,棋盘上一共19 x 19 = 361 个交点,对奔双方一方执白棋,一方执黑棋,落子时只能将棋子置于交点上。 “气”是围棋中很重要的一个概念,某个棋子有几口气,是指其上下左右方向四个相邻的交叉点中,有几个交叉点没有棋子,由此可知:

MISAYAONE的博客 1865

python基础:工作会用到的95%的基础python代码都在这了(万字详解!)

本文是一份Python编程语言的快速参考备忘单,涵盖了Python基础语法和常用数据类型。主要内容包括:Python入门指南(打印、变量声明)、内置数据类型(字符串、数值、列表、元组、集合、字典等)、控制结构(判断、循环)、函数定义、文件处理操作以及字符串操作方法(切片、循环遍历等)。这个备忘单特别适合初学者快速查阅Python基础语法,同时也提供了数据类型转换、f-string格式化字符串等实用技巧。通过简洁的代码示例展示了Python的基本语法规则和典型用法。

数据知道的博客 8176

Spring-aop 基于Aspectj 表达式配置AOP

基于Aspectj 表达式配置AOP(推荐使用) 基于配置文件 1.添加切面对象(aspect对象) 2.把切面对象交给spring 3.配置文件<?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

lx1315998513的博客 533

关于IBV_WR_RDMA_WRITE_WITH_IMM的理解

对于IBV_WC_RECV_RDMA_WITH_IMM的描述是这样的,也就是说并不需要sge,所以下发的sge,可以是NULL,个数可以是0。(1)需要下发post_recv,但是下发是post_recv当中的sge的地址可以是空,sge个数可以是0。伪代码,实际的rkey和addr已经通过socket交换。并且poll_cq时,有返回,在这篇文章当中对于poll_cq时,主要区别点有两个,这个是对于接收端而言。(2)可以带即时数据,

qq_38158479的博客 2429

Spring AOP AspectJ 切面表达式高级用法

一、Spring AOP 配置 首先,明白一点,Spring的AOP代理,分为JDK动态代理和Cglib动态代理,这两种代理的实现方式不一样,他们针对同一Aspect的配置效果也可能不一样。 JDK的动态代理,只能代理接口,无法代理非接口的方法。 Cglib动态代理,采用的是继承代理对象的方法,所以可以代理非private的所有方法。 Spring和AspectJ的关系: AspectJ是Eclipse基金会的一个动态代理框架(官方网站为:http://www....

zollty的专栏 1255

Allegro如何输出ODB文件操作指导

在PCB设计完成之后,需要输出生产文件用于生产加工,除了gerber文件可以用生产制造,ODB文件同样也可以用于生产,如下图。用Allegro如何输出ODB文件,具体操作如下。如何输出ODB文件操作指导。

weixin_54787054的博客 7253

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Spring AOP AspectJ Pointcut 表达式例子

主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/ 1. 方法标签匹配方式 假设定义了接口EmployeeManager接口。 1) execution(* com.howtodoinjava.Employe...

diehuangtong8203的博客 211

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Spring核心部分之AOP,aspectJ框架实现AOP,切入点表达式

1. 面向切面编程(Aspect Oriented Programming,AOP) 以切面为核心,确定切面的执行时间以及执行位置。 1.1 qie

走过路过不要错过 456

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Spring AOP AspectJ切点表达式详解

Spring AOP支持的AspectJ表达式概览: execution: 匹配方法执行的切入点。Spring AOP主要使用的切点标识符。 within: 限制匹配在特定类型内的连接点。(给定class的所有方法) this: 限制匹配是给定类型的实例的bean引用(Spring AOP proxy)的连接点。(代理类是给定类型的类的所有方法) target: 限制匹配是给定类型的实例的目标对象(被代理对象)的连接点。(目标对象是给定类型的类的所有方法) args: 匹配参数是给定类型的连接点。(方法入

不知明月的博客 3349

spring中AOP的AspectJ的Execution表达式

AspectJ的Execution表达式: 使用springAOP定义pointcut里面的execution表达式: execution()函数是常用的切点函数 "execution(* com.briup.aop.service..*.*(..))"   第一个*表示返回值,*表示返回值为任意类型。   后面这个就是典型的包名路径,其中可以包含 * 来进行通配,几个 * 没区别...

weixin_30273501的博客 121

Spring AOP详解& AspectJ表达式

近期项目中用到了Spring AOP,开一篇文章记录我爬过的坑。不知道其他初次接触Spring AOP框架的同学有没有感同身受,Spring AOP中最大的障碍莫过于写AspectJ表达式,稍有不慎便可能出现如下错误。 1.Pointcut is not well-formed: expecting ‘)’ at character position 71 execution(* com.won

wonking666的博客 740

Spring系列五(AOP概念、相关术语、AOP实战(基于AspectJ实现)、Adice类型及执行顺序、切入点表达式写法)

AOP(Aspect Oriented Programming)面向切面编程),是OOP(Object Oriented Programming )面向对象的一种补充和完善。它的作用是用来在程序运行期间,在不修改程序代码的情况下对已有方法进行增强。AOP提供一种“横向切面”的逻辑,将多个对象的公共模块封装成一个可重用模块,并将这个模块整合成为Aspect,即切面。切面就是与具体的业务逻辑无关的,...

xingze的博客 559

AOP___基于aspectJ方式实现

AspectJ实现了AOP功能,使用方便,支持注解。Spring中也有AspectJ的方式实现AOP。在Spring中使用AOP开发时,一般使用AspectJ的实现方式 AspectJ的切点表达式: execution(A, B, C, D, E); [权限修饰符 可选] A:方法返回类型 B:包名 C:类名 D:方法名 E:参数名 [异常类型 可选] 包名,类名,方法名要用.隔开,靠近()的...

qq_37432174的博客 356

AspectJ 对于 AOP 的实现

介绍了 AspectJ 的相关内容及 spring 引入 AspectJ 后,注解方式和 xml 配置方式的 AOP 实现

iCheetor 876

Spring AOP aspectJ切入点表达式示例

最典型的切入点表达式,用于通过签名匹配多个方法 1.1匹配另一个包中一个类中的所有方法 例如:前面通配符匹配任何修饰符(public、protested和private)和任务返回类型的方法。参数列表中的两个点匹配任意数量的参数。*表示匹配任意字符。 1.2匹配同一包内的类中的所有方法 如果目标类或接口与此方面位于同一包中,组可以省略包名。 1.3匹配类中的所有公共方法 1.4匹配所有公共方法与返回类型匹配 使用public关键字和返回类型EmployeeDTO 1.5将所有公共方法与返回类型 2.如何匹配

m0_49382941的博客 575
上一篇: 修改final属性值-内联优化
下一篇: Spring Bean名称内部生成工具
java_key_code
博客等级 码龄16年 781粉丝 53原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址