Spring AOP切面的时候参数的传递

Xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><bean id="volunteer" class="com.stono.sprtest2.Volunteer"></bean><bean id="magician" class="com.stono.sprtest2.Magician"></bean><bean id="singer" class="com.stono.sprtest2.Singer"></bean><aop:config><aop:aspect ref="magician"><aop:pointcut expression="execution(* com.stono.sprtest2.Thinker.thinkOfSomething(String)) and args(thoughts)" id="thinking" /><aop:before method="interceptThoughts" pointcut-ref="thinking" arg-names="thoughts" /></aop:aspect></aop:config>
</beans>

AppBean:

package com.stono.sprtest2;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppBeans10 {@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("appbeans10.xml");// 在AOP情况下,如果有接口就必须用接口来接,否则会报ClassCastException;Thinker bean = (Thinker) context.getBean("volunteer");bean.thinkOfSomething("volunteer think of something");MindReader bean2 = (MindReader) context.getBean("magician");String thoughts = bean2.getThoughts();System.out.println(thoughts);// 没有接口的Bean还是可以用类的;Singer bean3 = (Singer) context.getBean("singer");bean3.perform();}
}

POJO:

package com.stono.sprtest2;public interface Thinker {void thinkOfSomething(String thoughts);
}

package com.stono.sprtest2;public class Volunteer implements Thinker {private String thoughts;@Overridepublic void thinkOfSomething(String thoughts) {this.thoughts = thoughts;}public String getThoughts() {return thoughts;}
}

AOP:

package com.stono.sprtest2;public interface MindReader {void interceptThoughts(String thoughts);String getThoughts();
}

package com.stono.sprtest2;public class Magician implements MindReader {private String thoughts;@Overridepublic void interceptThoughts(String thoughts) {System.out.println("Intercepting volunteer's thoughts");this.thoughts = thoughts;}@Overridepublic String getThoughts() {return thoughts;}
}

Spring AOP切面的时候参数的传递相关推荐

  1. Spring AOP 切面@Around注解的具体使用

    @Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务. 比如我们想在执行controller中方法前打印出请求参数,并在方法执行结束后来打印出响应值,这个时候,我们就可以借助于@ ...

  2. 【经典】Spring aop切面实现异步添加日志—完整版

    系统开发中我们常遇到要处理系统日志等信息的,在此我分享一篇 利用spring aop切面来异步添加日志的操作,其中用到了 队列和多线程,前面的博客有写. 第一步:创建log实体,根据自己业务而定, p ...

  3. spring AOP切面日志

    spring AOP切面日志 导入依赖 <properties><fastjson.version>1.2.49</fastjson.version> </p ...

  4. spring aop 切面添加日志

    这是一个非常简单的spring aop切面添加日志的程序,下面来看一下这个程序 1.程序使用jar包 2.切面类LoggingAspect.java package com.cailei.aop.as ...

  5. Spring AOP切面使用详细解析

    相关文章: SpringBoot AOP切面的使用 一步一步手绘Spring AOP运行时序图(Spring AOP 源码分析) 架构师系列内容:架构师学习笔记(持续更新)) Spring AOP 应 ...

  6. 日志管理(spring AOP切面拦截)

    **最近,在写日志管理的东西呢,用了几种方法试,还是拦截比较好用,直接截下来传入到数据库中存储. 写的时候,真是头疼啊. 哦~对了,忘了说了,不需要用到Dao层哦. 首先,来一个POJO实体类吧,我看 ...

  7. Spring AOP 切面(Aspect)应用详解

    1. AOP 切面应用 下面是一个AOP切面的一个简单的应用实例 引入AOP依赖 <dependency><groupId>org.springframework.boot&l ...

  8. Spring aop切面插入事物回滚

    <!-- tx标签配置 事物--> <tx:advice id="txadvice" transaction-manager="transactionM ...

  9. Spring Aop(五)——给Advice传递参数

    5 给Advice传递参数 Advice除了可以接收JoinPoint(非Around Advice)或ProceedingJoinPoint(Around Advice)参数外,还可以直接接收与切入 ...

最新文章

  1. 如何修改可运行Jar包,如何反编译Jar包
  2. 新鲜的jquery插件收集
  3. keil C 51 strlen库函数使用
  4. Django出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xda in position 134: invalid
  5. 大学计算机基础python第二次作业_python第二次作业-titanic数据集练习
  6. Ubuntu16.04 Oracle JDK8和Open JDK8的安装与配置
  7. 收藏!数据建模最全知识体系解读
  8. stringbuffer java API_StringBuffer类
  9. L3-016 二叉搜索树的结构 (30 分)
  10. Nginx配置与安装及发布项目
  11. java web免费案例_java web 典型项目开发案例
  12. CIS Control v8的变化
  13. storm任务提交流程
  14. win10下装黑苹果双系统_高手拆换GPD P2 Max无线模块,实现完美黑苹果
  15. 《A Survey on Evolutionary Computation for Complex Continuous Optimization》笔记
  16. android三星s8底部菜单,【Android】三星Galaxy S8及S8+的屏幕适配
  17. 组合数据类型练习,英文词频统计实例
  18. 【原创】从头开始,使用安卓系统WebView做一个功能强大的Epub阅读器(一)
  19. 单片机8位、16位、32位和64位系统在内存上的区别
  20. YOLOV4垃圾检测召回率提升

热门文章

  1. POJ 1236 学校网络间的强连通
  2. Java中的Annotation(2)----Annotation工作原理
  3. SqlServer中bit类型的性别的一些问题
  4. CSS滤镜(Filters)
  5. 【OpenCV】5种图像滤波辨析:方框、均值、高斯、中值、双边
  6. 『宝藏 状态压缩DP NOIP2017』
  7. Linux组管理和权限管理
  8. home目录迁移至新分区
  9. Atitit .linux 取回root 密码q99
  10. 新增的querySelector、querySelectorAll测试