【框架】[Spring]AOP拦截-使用切点:AspectJExpressionPointcut-切点语言

简介: 【框架】[Spring]AOP拦截-使用切点:AspectJExpressionPointcut-切点语言

用AspectJExpressionPointcut实现的切点比JdkRegexpMethodPointcut实现切点的好处就是,在设置切点的时候可以用切点语言来更加精确的表示拦截哪个方法!

可以精确到返回参数,参数类型,方法名。

当然,也可以模糊匹配。

这里用纯Java的方式和配置xml的方法都来演示一遍。

需要的包什么的就不解释了,如不动,请参考前面的。

首先,准备好原型对象Person

package cn.hncu.spring3x.aop.aspectj;
public class Person {
    public int run(){
       System.out.println("我在run...");
       return 0;
   }
   public void run(int i){
       System.out.println("我在run...<"+i+">");
   }
   public void say(){
       System.out.println("我在say...");
   }
   public void sayHi(String name){
       System.out.println("Hi,"+name+",你好");
   }
   public int say(String name, int i){
       System.out.println(name+ "----"+ i);
       return 0;
   }
}

然后,用两种方式来拦截这个对象。

纯Java方式实现

4步曲:

1、声明出代理工厂。

2、设置切点

3、设置通知

4、为工厂添加切面

请记住:切面=切点+通知

AspectjDemo

package cn.hncu.xmlImpl.aspectj;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;
public class AspectjDemo {
    @Test
    public void demo(){
        ProxyFactoryBean factory = new ProxyFactoryBean();
        factory.setTarget(new Person());
        //声明一个aspectj切点
        AspectJExpressionPointcut cut = new AspectJExpressionPointcut();
        //设置需要拦截的方法-用切点语言来写
        cut.setExpression("execution( int cn.hncu.xmlImpl.aspectj.Person.run() )");//拦截:空参返回值为int的run方法
        Advice advice = new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("放行前拦截...");
                Object obj = invocation.proceed();//放行
                System.out.println("放行后拦截...");
                return obj;
            }
        };
        //切面=切点+通知
        Advisor advisor = new DefaultPointcutAdvisor(cut,advice);
        factory.addAdvisor(advisor);
        Person p = (Person) factory.getObject();
        p.run();
        p.run(10);
        p.say();
        p.sayHi("Jack");
        p.say("Tom", 666);
    }
}

运行结果:

image.png

切点语言:

AspectJExpressionPointcut对象在调用:

setExpression时,这个方法的参数就是使用切点语言的。

切点语言格式:

execution ( 返回类型 方法路径.方法名(参数) )

例子:

//声明一个aspectj切点
        AspectJExpressionPointcut cut = new AspectJExpressionPointcut();
cut.setExpression("execution( int cn.hncu.xmlImpl.aspectj.Person.run() )");//拦截:空参返回值为int的run方法
cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*() )");//拦截:空参空返回值的任意方法
cut.setExpression("execution (void cn.hncu.xmlImpl.aspectj.Person.*(String))");//拦截:只有1个String类型参数,空返回值的任意方法
cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*(*) )");//拦截:有1个参数(类型不限),空返回值的任意方法
cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*(*,*) )");//拦截:有2个参数(类型不限),空返回值的任意方法
cut.setExpression("execution( void cn.hncu.xmlImpl.aspectj.Person.*(..) )");//拦截:任意(个数和类型)参数,空返回值的任意方法
cut.setExpression("execution( int cn.hncu.xmlImpl.aspectj.Person.*(*,..) )");//拦截:至少有1个参数(类型不限),返回值类型是int的任意方法
cut.setExpression("execution( * cn.hncu.xmlImpl.aspectj.Person.*(*,..) )");//拦截:至少有1个参数(类型不限),返回值类型任意的方法
cut.setExpression("execution( * cn.hncu..*son.*(*,..) )");//拦截:cn.hncu包下,类名以"son"结束,至少有1个参数(类型不限),返回值类型任意的方法

里面的参数是可以匹配正则表达式的。

“.”代表除\r\n外的任意字符。

“*”代表0个或多个。

由于切点语言无法定义指定的多个返回值,所以,例如:

如果需要拦截void和int返回值方法,则可以通过定义2个切点解决。

xml配置AOP拦截

AroundAdvice

package cn.hncu.xmlImpl.aspectj;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor{
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("前面拦截....");
        Object resObj = invocation.proceed();//放行
        System.out.println("后面拦截.....");
        return resObj;
    }
}

配置文件

<?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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- 自动代理 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
    <bean id="p" class="cn.hncu.xmlImpl.aspectj.Person"></bean>
    <!-- 切面=切点+通知 (把切点和通知写成内部bean)-->
    <bean id="cut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
        <!-- 拦截:cn.hncu包下,类名以"son"结束,至少有1个参数(类型不限),返回值类型任意的方法 -->
      <property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>    
    </bean>
    <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
       <property name="pointcut" ref="cut"></property>
       <property name="advice">
            <bean id="advice" class="cn.hncu.xmlImpl.aspectj.AroundAdvice"></bean>
       </property>
    </bean>
</beans>

中间也可以这样配置:

<!-- 自动代理 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
    <bean id="p" class="cn.hncu.xmlImpl.aspectj.Person"></bean>
    <!-- 切面=切点+通知 (※※采用面向切点语言进行配置切面)-->
    <bean id="advisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
       <property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>
       <property name="advice">
            <bean id="advice" class="cn.hncu.xmlImpl.aspectj.AroundAdvice"></bean>
       </property>
    </bean>

测试类:

package cn.hncu.xmlImpl.aspectj;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectjXmlDemo {
    @Test
    public void demo1(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/aspectj/aspectj.xml");
        Person p = ctx.getBean(Person.class);
        p.run();
        p.run(10);
        p.say();
        p.sayHi("Jack");
        p.say("Tom", 666);
    }
}

测试结果

image.png

在本例:xml配置与纯Java方式相比,即把通过Java代码new出来的对象,通过xml配置来造对象。

如果是开发项目,用Spring的框架,我们的一些通过xml注入的对象就只需要依赖xml文件了。

而依赖xml的依赖不叫依赖,也就是独立了。

目录
打赏
0
0
0
0
989
分享
相关文章
Spring AOP实现原理
本内容主要介绍了Spring AOP的核心概念、实现机制及代理生成流程。涵盖切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)等关键概念,解析了JDK动态代理与CGLIB代理的原理及对比,并深入探讨了通知执行链路和责任链模式的应用。同时,详细分析了AspectJ注解驱动的AOP解析过程,包括切面识别、切点表达式匹配及通知适配为Advice的机制,帮助理解Spring AOP的工作原理与实现细节。
|
4月前
|
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——Spring Boot 中的 AOP 处理
本文详细讲解了Spring Boot中的AOP(面向切面编程)处理方法。首先介绍如何引入AOP依赖,通过添加`spring-boot-starter-aop`实现。接着阐述了如何定义和实现AOP切面,包括常用注解如`@Aspect`、`@Pointcut`、`@Before`、`@After`、`@AfterReturning`和`@AfterThrowing`的使用场景与示例代码。通过这些注解,可以分别在方法执行前、后、返回时或抛出异常时插入自定义逻辑,从而实现功能增强或日志记录等操作。最后总结了AOP在实际项目中的重要作用,并提供了课程源码下载链接供进一步学习。
208 0
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——什么是AOP
本文介绍了Spring Boot中的切面AOP处理。AOP(Aspect Oriented Programming)即面向切面编程,其核心思想是分离关注点。通过AOP,程序可以将与业务逻辑无关的代码(如日志记录、事务管理等)从主要逻辑中抽离,交由专门的“仆人”处理,从而让开发者专注于核心任务。这种机制实现了模块间的灵活组合,使程序结构更加可配置、可扩展。文中以生活化比喻生动阐释了AOP的工作原理及其优势。
98 0
微服务——SpringBoot使用归纳——Spring Boot中的全局异常处理——拦截自定义异常
本文介绍了在实际项目中如何拦截自定义异常。首先,通过定义异常信息枚举类 `BusinessMsgEnum`,统一管理业务异常的代码和消息。接着,创建自定义业务异常类 `BusinessErrorException`,并在其构造方法中传入枚举类以实现异常信息的封装。最后,利用 `GlobalExceptionHandler` 拦截并处理自定义异常,返回标准的 JSON 响应格式。文章还提供了示例代码和测试方法,展示了全局异常处理在 Spring Boot 项目中的应用价值。
93 0
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
206 0
Spring框架之AOP(面向切面编程)
Spring框架之AOP(面向切面编程)
125 0
详解 Java 限流接口实现问题之在Spring框架中使用AOP来实现基于注解的限流问题如何解决
详解 Java 限流接口实现问题之在Spring框架中使用AOP来实现基于注解的限流问题如何解决
173 0
【Spring框架四】——Spring AOP 注解实现和xml方式实现1
【Spring框架四】——Spring AOP 注解实现和xml方式实现
220 0
在Spring框架中,IoC和AOP是如何实现的?
【4月更文挑战第30天】在Spring框架中,IoC和AOP是如何实现的?
172 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问