16、spring AOP通知——Advice
来源:互联网 发布:二叉树遍历原理java 编辑:程序博客网 时间:2024/05/09 04:12
16、spring AOP通知——Advice
**说明:**spring AOP是一个面向切面的编程框架,也就是拦截器的一个集合,可以劫持一些方法,并在这些方法执行前,执行后或者围绕其他动作进行一些处理或添加以下新功能
主要有以下四种通知方式:
- Advice之前——在方法执行前运行
- Advice之后——在方法执行后运行
- Advice抛出异常之后——在方法抛出异常之后运行
- 环绕通知——综合运用上述三种方式
在使用Advice通知之前,我们先来建立一个简单的spring例子
目录结构如下:
HelloWorld.java
package com.main.AOP;public class HelloWorld { private String name; private String type; public String getName() { return name; } public String getType(){ return type; } public void setName(String name) { this.name = name; } public void setType(String type){ this.type = type; } public void printName(){ System.out.println("Name : "+name); } public void printType(){ System.out.println("Type : "+type); } public void printThrowException(){ throw new IllegalArgumentException(); }}
UnitTest.java
package com.main.AOP;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UnitTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/main/AOP/bean.xml"); HelloWorld helloworld = (HelloWorld)context.getBean("helloWorld"); System.out.println("-------------------------------"); helloworld.printName(); System.out.println("-------------------------------"); helloworld.printType(); System.out.println("-------------------------------"); try{ helloworld.printThrowException(); }catch(Exception e){ } }}
bean.xml
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="helloWorld" class="com.main.AOP.HelloWorld"> <property name="name" value="jack"/> <property name="type" value="user"/> </bean></beans>
运行UnitTest的test方法,得到正常结果:
spring的简单例子就完成了,下面介绍spring AOP通知——Advice的四种通知方式:
1、之前通知
第一步:新增一个Service 类
AdviceService.java
package com.main.AOP;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class AdviceService implements MethodBeforeAdvice{ public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("AdviceService : before method advice"); }}
第二步:修改你的bean.xml文件,添加以下代码
<bean id="adviceService" class="com.main.AOP.AdviceService"></bean> <bean id="adviceServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 被拦截的bean --> <property name="target" ref="helloWorld"/> <!-- 代理bean --> <property name="interceptorNames"> <list> <value>adviceService</value> </list> </property> </bean>
第三步:修改你的UnitTest类的test方法如下:
HelloWorld helloworld = (HelloWorld)context.getBean("adviceServiceProxy");
运行后结果:
】
解释:helloWorld bean充当被拦截bean,adviceService bean充当代理bean,在被拦截bean的任何方法执行前,代理bean都会调用执行before方法。
2、之后通知
修改你的AdviceService如下
添加一个新接口AfterReturningAdvice,并实现相应方法
public class AdviceService implements MethodBeforeAdvice,AfterReturningAdvice{ public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("AdviceService : before method advice"); } public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("AdviceService : after method advice"); }}
运行结果:
3、抛出后通知
修改你的AdviceServicelei,新增实现一个接口ThrowsAdvice:
public class AdviceService implements MethodBeforeAdvice,AfterReturningAdvice,ThrowsAdvice { //之前执行的方法 public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("AdviceService : before method advice"); } //返回后执行的方法 public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("AdviceService : after method advice"); } //抛出异常后执行的方法 public void afterThrowing(IllegalArgumentException e) throws Throwable { System.out.println("AdviceService : Throw exception!"); }}
运行结果:
环绕通知(综合上述三种)
为了方便演示和更好理解,这里新建一个类
AdviceAroundService.java
public class AdviceAroundService implements MethodInterceptor{ public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("AdviceAroundService : 之前执行"); try{ // proceed to original method call Object result = methodInvocation.proceed(); System.out.println("AdviceAroundService : 返回后执行"); return result; }catch(IllegalArgumentException e){ System.out.println("AdviceAroundService : 抛出异常后执行"); throw e; } }}
并且修改你的bean.xml文件如下:
<bean id="adviceAroundService" class="com.main.AOP.AdviceAroundService"></bean> <bean id="adviceServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 被拦截的bean --> <property name="target" ref="helloWorld"/> <!-- 代理bean --> <property name="interceptorNames"> <list> <value>adviceAroundService</value> </list> </property> </bean>
运行结果:
0 0
- 16、spring AOP通知——Advice
- Spring AOP——在通知(Advice)方法中获取目标方法的参数
- Spring AOP通知实例 – Advice
- Spring Aop源码学习--Advice通知
- Spring AOP : Advice 声明 (通知注解)
- spring aop学习3:通知(Advice注解)
- Spring3- Spring AOP——Advice
- Spring3系列:Spring AOP——Advice
- Spring3系列9- Spring AOP——Advice
- Spring Aop(五)——给Advice传递参数
- Spring Aop(五)——给Advice传递参数
- spring的通知(advice)
- Spring AOP tx:advice
- Spring AOP之Advice
- Spring框架——AOP前置通知
- Spring AOP Example – Advice
- Spring AOP Example – Advice
- Spring AOP Example – Advice
- foritclient vpn client
- Codeforces Round #399 (A,B,C)
- 顺序表
- MySQL学习(二)DQL 数据查询语言
- 讨论checked异常和runtime异常体系的优劣
- 16、spring AOP通知——Advice
- GB2312 和 UTF-8的区别
- Android 最火的快速开发框架XUtils
- SQL Server Analysis Services 数据挖掘(1)
- 子元素浮动让父元素塌陷解决办法
- 利用NLTK处理HTML
- logback同时按照日期和大小分割日志
- bzoj1030: [JSOI2007]文本生成器
- JS:对等差分