(14)Spring AOP Advisor
来源:互联网 发布:mac cs6破解文件下载 编辑:程序博客网 时间:2024/06/05 10:40
只能有一个通知,但是感觉很强大
被拦截类的接口
package shuai.spring.study.service;public interface IHelloService { public void sayHello(); public String returnHello(String param0, String param1);}
被拦截类
package shuai.spring.study.service.impl;import shuai.spring.study.Info;import shuai.spring.study.service.IHelloService;public class HelloServiceImpl implements IHelloService { @Override public void sayHello() { System.out.println("============Hello World!"); } @Override public String returnHello(String param0, String param1) { System.out.println("返回前输出,两个参数:" + param0 + "和" + param1); return Info.info(param0, param1); }}
Info类
package shuai.spring.study;public class Info { public static String info() { System.out.println("返回时输出:hello world 1"); return "返回值:hello world 1"; } public static String info(String param0, String param1) { System.out.println("返回时输出:" + param0 + "," + param1); return "返回值为:" + param0 + "," + param1; }}
配置文件
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/> <bean id="helloAdvisorBefore" class="shuai.spring.study.aop.HelloAdvisorBefore"/> <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> --> <aop:config> <aop:pointcut expression="execution(* shuai.spring.study.service..*.*(..))" id="pointcut"/> <aop:advisor advice-ref="helloAdvisorBefore" pointcut-ref="pointcut"/> </aop:config></beans>
注意:advice-ref引用的bean必须要实现某个通知的接口,比如这里的HelloAdvisorBefore要实现前置通知的接口MethodBeforeAdvice,就是一个通知类,目标方法被拦截了会到这里执行。
定义HelloAdvisorBefore类
package shuai.spring.study.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class HelloAdvisorBefore implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("所在方法的名字为:" + arg0.getName()); for (int i = 0; i < arg1.length; i++) { System.out.println("第" + (i + 1) + "个参数的值为:" + arg1[i]); } System.out.println("所在类的toString方法为:" + arg2); }}
测试类
package shuai.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import shuai.spring.study.service.IHelloService;public class HelloTest { @Test public void testHelloWorld() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml"); IHelloService iHelloService = context.getBean("iHelloService", IHelloService.class); iHelloService.returnHello("hello", "world"); }}
测试结果:
所在方法的名字为:returnHello
第1个参数的值为:hello
第2个参数的值为:world
所在类的toString方法为:shuai.spring.study.service.impl.HelloServiceImpl@7ff95560
返回前输出,两个参数:hello和world
返回时输出:hello,world
阅读全文
0 0
- (14)Spring AOP Advisor
- spring的AOP(四)----Spring AOP 编程(Advisor)
- Spring AOP 学习之Advisor
- spring aop 细说advice,advisor
- Spring的AOP(三):切入点Pointcut 、Advisor及引入
- Spring AOP(面向切面编程)【使用Advisor】
- Spring Aop(八)——advisor标签
- spring的aop:advisor和aop:aspect
- Spring AOP——Pointcut,Advisor
- Spring AOP Example – Pointcut , Advisor
- Spring3- Spring AOP——Pointcut,Advisor
- Spring Aop中的advisor、advice、pointcut
- Spring Aop源码学习--Advisor切面
- spring Aop中aop:advisor 与 aop:aspect的区别
- Spring之<aop:advisor>和<aop:aspect>的区别
- 编程式实现Spring AOP 引入(Introduction)和切面(Advisor)
- Spring Aop(十六)——编程式的自定义Advisor
- springframework(十)AOP之Pointcut、Advisor
- 图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
- spring 核心与源码解析(0):前言
- git push时出现Username for 'https://github.com': 解决方法
- Linux 多线程编程
- 字符个数统计
- (14)Spring AOP Advisor
- Codeforces Round #181 (Div. 2) E. Empire Strikes Back N!∣∏K,i=1ai!
- Pygame 中访问音频采样数据的模块。
- 欢迎使用CSDN-markdown编辑器
- 二分图神奇讲解
- read()函数
- java软件安装
- CCF YOCSEF 大数据+人工智能创新前沿报告会
- js闭包浅记