【Spring实战】—— 10 AOP针对参数的通知

简介:

通过前面的学习,可以了解到 Spring的AOP可以很方便的监控到方法级别的执行 ,针对于某个方法实现通知响应。

那么对于方法的参数如何呢?

  比如我们有一个方法,每次传入了一个字符串,我想要知道每次传入的这个字符串是神马?这又如何办到呢!

  举个Action上面的例子,一个思考者(thinker),每次在思考时,都会传入一个字符串作为思考的内容。

  我们想要每次获取到这个思考的内容,实现一个通知。因此读心者可以通过AOP直接监控到每次传入的内容。

源码参考

  首先看一下思考者的接口和实现类:

package com.spring.test.aopmind;

public interface Thinker {
    void thinkOfSomething(String thoughts);
}
复制代码
package com.spring.test.aopmind;

public class Volunteer implements Thinker{
    private String thoughts;
    public void thinkOfSomething(String thoughts) {
        this.thoughts = thoughts;
    }
    public String getThoughts(){
        return thoughts;
    }
}
复制代码

  下面是读心者的接口和实现类:

复制代码
package com.spring.test.aopmind;

public interface MindReader {
    void interceptThoughts(String thougths);
    String getThoughts();
}
复制代码
复制代码
package com.spring.test.aopmind;

public class Magician implements MindReader{
    private String thoughts;
    public void interceptThoughts(String thougths) {
        System.out.println("Intercepting volunteer's thoughts");
        this.thoughts = thougths;
    }
    public String getThoughts() {
        return thoughts;
    }
}
复制代码

  接着配置好bean.xml配置文件

复制代码
<?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.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
    
    <bean id="magician" class="com.spring.test.aopmind.Magician"/>
    <bean id="xingoo" class="com.spring.test.aopmind.Volunteer" />
    
    <aop:config proxy-target-class="true">    
        <aop:aspect ref="magician">
            <aop:pointcut id="thinking" 
            expression="execution(* com.spring.test.aopmind.Thinker.thinkOfSomething(String)) and args(thoughts)"/>
            <aop:before pointcut-ref="thinking" method="interceptThoughts" arg-names="thoughts"/>
        </aop:aspect>
    </aop:config>
</beans>
复制代码

  测试类如下

复制代码
package com.spring.test.aopmind;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Thinker thinker = (Thinker)ctx.getBean("xingoo");
        thinker.thinkOfSomething("吃点啥呢!");
    }
}
复制代码

  执行结果:

Intercepting volunteer's thoughts

 

讲解说明

  在配置文件中:

  在<aop:before>中指明了要传入的参数thoughts

  在<aop:pointcut>切点中通过AspectJ表达式锁定到特定的方法和参数thoughts

  这样,当执行到方法thinkOfSomething()之前,就会触发aop,得到参数thoughts,并传递给通知类的拦截方法中。

本文转自博客园xingoo的博客,原文链接:【Spring实战】—— 10 AOP针对参数的通知,如需转载请自行联系原博主。
相关文章
|
5天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
1天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
10 1
|
2天前
|
Java Spring 容器
深入理解Spring Boot启动流程及其实战应用
【5月更文挑战第9天】本文详细解析了Spring Boot启动流程的概念和关键步骤,并结合实战示例,展示了如何在实际开发中运用这些知识。
13 2
|
4天前
|
JavaScript Java 开发者
Spring Boot中的@Lazy注解:概念及实战应用
【4月更文挑战第7天】在Spring Framework中,@Lazy注解是一个非常有用的特性,它允许开发者控制Spring容器的bean初始化时机。本文将详细介绍@Lazy注解的概念,并通过一个实际的例子展示如何在Spring Boot应用中使用它。
17 2
|
5天前
|
XML Java 数据格式
Spring使用AOP 的其他方式
Spring使用AOP 的其他方式
15 2
|
5天前
|
XML Java 数据格式
Spring 项目如何使用AOP
Spring 项目如何使用AOP
19 2
|
5天前
|
前端开发 Java
SpringBoot之自定义注解参数校验
SpringBoot之自定义注解参数校验
15 2
|
9天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
21 2
|
10天前
|
Java 开发者 Spring
Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
【5月更文挑战第1天】Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
23 5
|
10天前
|
XML Java 数据格式
Spring AOP
【5月更文挑战第1天】Spring AOP
27 5