spring4中文技术手册
在《静态代理和动态代理》中提到了面向方面编程,主要就是基于动态代理。单独抽象出非业务的功能,服务于某些业务方法。Spring提供了四种很实用的Advice,分别为:Before Advice, After Returning Advice, Around Advice, After throwing Advice。都是方法级别的,就是在某个方法执行前后插入一些非业务的操作,如打日志或者判断权限等。
对于这四种advice的实现,spring都提供了三种方法,分别为基于接口、基于xml和基于annotation(注释)。Before Advice会在目标对象的方法执行之前被调用;After Advice会在目标方法执行之后被调用;Around Advice则可以在目标方法执行前后同时加上相关服务;Throw Advice是在异常发生后执行某些操作。
1.基于接口的Advice
这个就需要自定义的Aspect实现Spring接口。
BeforeAdvice需要实现org.springframework.aop.MethodBeforeAdvice接口:
1. /**
2. * Advice invoked before a method is invoked. Such advices cannot
3. * prevent the method call proceeding, unless they throw a Throwable.
4. */
5. public interface MethodBeforeAdvice extends BeforeAdvice {
6.
7. /**
8. * Callback before a given method is invoked.
9. * @param method method being invoked
10. * @param args arguments to the method
11. * @param target target of the method invocation. May be <code>null</code>.
12. * @throws Throwable if this object wishes to abort the call.
13. * Any exception thrown will be returned to the caller if it's
14. * allowed by the method signature. Otherwise the exception
15. * will be wrapped as a runtime exception.
16. */
17. void before(Method method, Object[] args, Object target) throws Throwable;
18. }
After Advice实现org.springframework.aop.AfterReturningAdvice接口:
1. /**
2. * After returning advice is invoked only on normal method return, not if an
3. * exception is thrown. Such advice can see the return value, but cannot change it.
4. */
5. public interface AfterReturningAdvice extends AfterAdvice {
6. /**
7. * Callback after a given method successfully returned.
8. * @param returnValue the value returned by the method, if any
spring怎么读中文什么意思9. * @param method method being invoked
10. * @param args arguments to the method
11. * @param target target of the method invocation. May be <code>null</code>.
12. * @throws Throwable if this object wishes to abort the call.
13. * Any exception thrown will be returned to the caller if it's
14. * allowed by the method signature. Otherwise the exception
15. * will be wrapped as a runtime exception.
16. */
17. void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;
18. }
Around Advice需要实现org.aopalliance.intercept.MethodInterceptor接口:
1. /**
2. * <p>The user should implement the {@link #invoke(MethodInvocation)}
3. * method to modify the original behavior.& the following class
4. * implements a tracing interceptor (traces all the calls on the
5. * intercepted method(s)):
6. *
7. * <pre class=code>
8. * class TracingInterceptor implements MethodInterceptor {
9. * Object invoke(MethodInvocation i) throws Throwable {
10. * System.out.println("method "+i.getMethod()+" is called on "+
11. * i.getThis()+" with args "+i.getArguments());
12. * Object ret=i.proceed();
13. * System.out.println("method "+i.getMethod()+" returns "+ret);
14. * return ret;
15. * }
16. * }
17. * </pre> */
18. public interface MethodInterceptor extends Interceptor {
19.
20. /**
21. * Implement this method to perform extra treatments before and
22. * after the invocation. Polite implementations would certainly
23. * like to invoke {@link Joinpoint#proceed()}.
24. *
25. * @param invocation the method invocation joinpoint
26. * @return the result of the call to {@link
27. * Joinpoint#proceed()}, might be intercepted by the
28. * interceptor.
29. *
30. * @throws Throwable if the interceptors or the
31. * target-object throws an exception. */
32. Object invoke(MethodInvocation invocation) throws Throwable;
33. }
类前面的注释说明了该方法的使用,就是要在invoke()方法中调用MethodInvocation.proceed(),将执行传给下一个Interceptor,最终执行目标方法。在proceed()方法前后加操作,到达Aroud advice的作用。
在Aspect定义好后,就需要在bean定义文件中进行配置,通过org.springframework.aop.framework.ProxyFactoryBean的配置,指出接口、目标类和Aspect。如下:
1. <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
2.
3. <property name="proxyInterfaces" value="spring.advice.IHello"/>
4.
5. <property name="target" ref="helloSpeaker"/>
6.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论