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>
运行结果: