您的位置:首页 > 编程语言 > Java开发

Spring学习笔记:使用代理实现AOP

2017-10-08 18:26 846 查看

AOP基础知识

Spring AOP 即 Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统中分布于各个模块(不同方法)中的交叉关注点的问题。简单地说,就是一个拦截器( interceptor )拦截一些处理过程。

例如,当一 个method 被执行,Spring AOP 能够劫持正在运行的 method ,在 method 执行前或者后加入一些额外的功能。

在 Spring AOP 中,支持 4 种类型的通知( Advice ):

Before advice - method 执行前通知

After returning advice - method 返回一个结果后通知

After throwing advice - method 抛出异常后通知

Around advice - 环绕通知,结合了以上三种

四种类型的通知详解(Advice)

Before Advice

1.实现MethodBeforeAdvice接口,覆盖before方法

import  java.lang.reflect.Method;
import com.springframework.aop.MethodBeforeAdvice;
public class AdviceBeforeMethod implements  MethodBeforeAdvice{
public void before(Method arg0,Object[] args,Object target)
throws Throwable{
System.out.println("add some thing before the method");
}
}


2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="类名"></bean>


3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
<property name="target" ref="要拦截的类的Bean的Id"/>
<property name="interceptorNames">
<list>
<value>使用的Advice的Bean的Id</value>
</list>
</property>
</bean>


After Returning Advice

1.实现AfterReturningAdvice 接口,覆盖afterReturning方法

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterMethodAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("add some thing after returning");
}
}


2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="类名"></bean>


3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
<property name="target" ref="要拦截的类的Bean的Id"/>
<property name="interceptorNames">
<list>
<value>使用的Advice的Bean的Id</value>
</list>
</property>
</bean>


After Returning Advice

1.实现AfterReturningAdvice 接口,覆盖afterReturning方法

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterMethodAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("add some thing after returning");
}
}


2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="类名"></bean>


3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
<property name="target" ref="要拦截的类的Bean的Id"/>
<property name="interceptorNames">
<list>
<value>使用的Advice的Bean的Id</value>
</list>
</property>
</bean>


After Throwing Advice

1.实现ThrowsAdvice接口,覆盖afterThrowing方法

import org.springframework.aop.ThrowsAdvice;
public class AfterThrowingAdvice implements ThrowsAdvice {
public void afterThrowing(IllegalArgumentException e)throws Throwable{
System.out.println("add some thing after throw exception");
}
}


2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="类名"></bean>


3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
<property name="target" ref="要拦截的类的Bean的Id"/>
<property name="interceptorNames">
<list>
<value>使用的Advice的Bean的Id</value>
</list>
</property>
</bean>


Around Advice

1.实现接口MethodInterceptor,通过methodInvocation.proceed()来调用原方法

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.util.Arrays;

public class AroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
//取出方法
System.out.println("Method name : " + methodInvocation.getMethod().getName());
//取出方法的参数
System.out.println("Method arguments : " + Arrays.toString(methodInvocation.getArguments()));
//相当于MethodBeforeAdvice
System.out.println("add some thing before the method");
try{
//调用原方法
Object result = methodInvocation.proceed();
//相当于AfterReturningAdvice
System.out.println("add some thing after the method");
return result;
}catch (IllegalArgumentException e){
//相当于ThrowsAdvice
System.out.println();
throw e;
}
}
}


2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="类名"></bean>


3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
<property name="target" ref="要拦截的类的Bean的Id"/>
<property name="interceptorNames">
<list>
<value>使用的Advice的Bean的Id</value>
</list>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: