编程式实现Spring AOP

来源:互联网 发布:北风网大数据课程 编辑:程序博客网 时间:2024/05/17 08:43

在我们使用Spring的过程中,经常会接触到AOP这个概念,但是在实际的项目使用过程中,大部分都是通过配置的方式来实现方法的增强,今天我们通过这篇文章来告诉大家,在Spring中如何通过编程的方式来实现方法的前置增强,后置增强和环绕增强。

1、前置增强(Before Advice)

所谓前置增强,就是在执行对应方法逻辑前,需要预先处理额外增加的逻辑,在某些特殊的情况下,如果我们仅仅只需要在执行方法逻辑前增加一些额外的逻辑,同时这些逻辑在同一个Bean的方法执行前都需要执行,例如在调用方法前,需要记录一下日志,如果在Bean的所有方法体中都加入一段记录日志的代码,这不但使得代码量增加,同时会造成代码难以维护。那假如我们使用前置增强,就可以单独写一个前置增强类,然后通过代理工厂将增强类添加进去,在调用方法时就可以自动执行前置增强逻辑,然后才执行方法体逻辑。

我们写一个接口,定义一个sayHello的方法

public interface Greeting {void sayHello(String name);}

再写一个实现类,让他实现以上接口

public class GreetingImpl implements Greeting {@Overridepublic void sayHello(String name) {System.out.println("Hello:"+name);}}
现在我们有一个需求,需要在执行sayHello方法的逻辑前,打印出一段文字,我们可以直接在方法体中打印,也可以是按照上一篇文章我们所说的静态代理、JDK动态代理、CGLib动态代理的方式实现,今天我们介绍的第三种通过Spring的面向切面的方式来实现。

首先我们定义一个前置增强类GreetingBeforeAdvice,让他实现Spring AOP的MethodBeforeAdvice接口。

public class GreetingBeforeAdvice implements MethodBeforeAdvice {@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println("Before");}}
在重写的before方法中,我们实现我们对应的前置逻辑。

public static void main(String[] args) {//创建代理工厂ProxyFactory proxyFactory = new ProxyFactory();//设置目标类对象proxyFactory.setTarget(new GreetingImpl());//添加增强proxyFactory.addAdvice(new GreetingBeforeAdvice());//获取代理Greeting greeting = (Greeting) proxyFactory.getProxy();greeting.sayHello("Ron.Zheng");}
在使用增强信息时,首先我们需要创建一个代理工厂,然后在代理工厂中设置需要增强的目标对象,添加对应的增强。执行以上代码,我们会得到如下结果。
BeforeHello:Ron.Zheng

2、后置增强(After Advice)

所谓后置增强,就是在执行对应方法逻辑后,需要处理额外增加的逻辑,在某些特殊的情况下,如果我们仅仅只需要在执行方法逻辑后增加一些额外的逻辑,同时这些逻辑在同一个Bean的方法执行前都需要执行,例如我们同样需要在方法执行完毕之后记录一下日志,我们就可以采用后置增强。

我们写一个后置增强类GreetingAfterAdvice,让他实现AfterReturningAdvice。

public class GreetingAfterAdvice implements AfterReturningAdvice {@Overridepublic void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("After");}}

然后我们在我们的调用方法中,我们往代理工厂加入对应的通知就可以了。

public static void main(String[] args) {//创建代理工厂ProxyFactory proxyFactory = new ProxyFactory();//设置目标类对象proxyFactory.setTarget(new GreetingImpl());//添加增强proxyFactory.addAdvice(new GreetingAfterAdvice());//获取代理Greeting greeting = (Greeting) proxyFactory.getProxy();greeting.sayHello("Ron.Zheng");}
执行结果

Hello:Ron.ZhengAfter

3、环绕增强(Around Advice)

所谓环绕增强,就是在执行方法逻辑前后,都需要增加额外逻辑的增强,一般情况下,环绕增强会使用得多一些。

环绕增强需要实现org.aopalliance.intercept.MethodInterceptor接口,望文生义,环绕增强,可以当成一个方法拦截器来理解。

我们定义一个GreetingAroundAdvice类,然后实现MethodInterceptor接口。

public class GreetingAroundAdvice implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {before();Object result = invocation.proceed();after();return result;}private void after() {System.out.println("调用方法之后");}private void before() {System.out.println("调用方法之前");}}

在我们的应用中,将增强类的对象添加到代理工厂中

public static void main(String[] args) {//创建代理工厂ProxyFactory proxyFactory = new ProxyFactory();//设置目标类对象proxyFactory.setTarget(new GreetingImpl());//添加增强proxyFactory.addAdvice(new GreetingAroundAdvice());//获取代理Greeting greeting = (Greeting) proxyFactory.getProxy();greeting.sayHello("Ron.Zheng");}
执行程序就能看到如下结果。

调用方法之前Hello:Ron.Zheng调用方法之后


0 0
原创粉丝点击