《spring4中文技术手册.docx》由会员分享,可在线阅读,更多相关《spring4中文技术手册.docx(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、spring4 中文技术手册在静态代理和动态代理中提到了面对方面编程,主要就是基于动态代理。单独抽象出非业务的功能,效劳于某些业务方法。Spring 供给了四种很有用的 Advice,分别为:Before Advice, After Returning Advice, Around Advice, After throwing Advice。都是方法级别的,就是在某个方法执行前后插入一些非业务的操作,如打日志或者推断权限等。对于这四种 advice 的实现,spring 都供给了三种方法,分别为基于接口、基于 xml 和基于 annotation(注释)。Before Advice 会在目标对
2、象的方法执行之前被调用; 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 cannot3.* prevent the met
3、hod 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 invoked10.* param args arguments to the method11.* param target target of the method invocation. May be
4、null.12.* throws Throwable if this object wishes to abort the call.13.* Any exception thrown will be returned to the caller if it”s14.* allowed by the method signature. Otherwise the exception15.* will be wrapped as a runtime exception.16.*/17.void before(Method method, Object args, Object target) t
5、hrows Throwable;18. After Advice 实现 org.springframework.aop.AfterReturningAdvice 接口:1./*2.* After returning advice is invoked only on normal method return, not if an3.* exception is thrown. Such advice can see the return value, but cannot change it.4.*/5.public interface AfterReturningAdvice extends
6、 AfterAdvice 6./*7.* Callback after a given method successfully returned.8.* param returnValue the value returned by the method, if any9.* param method method being invoked10.* param args arguments to the method11.* param target target of the method invocation. May be null.12.* throws Throwable if t
7、his object wishes to abort the call.13.* Any exception thrown will be returned to the caller if it”s14.* allowed by the method signature. Otherwise the exception15.* will be wrapped as a runtime exception.16.*/17.void afterReturning(Object returnValue, Method method, Object args, Object target) thro
8、ws Throwable;18. Around Advice 需要实现 org.aopalliance.intercept.MethodInterceptor 接口:1./*2.* The user should implement the link #invoke(MethodInvocation)3.* method to modify the original behavior. E.g. the following class4.* implements a tracing interceptor (traces all the calls on the5.* intercepted
9、method(s):6.*7.* 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.getMe
10、thod+“ returns “+ret);14.*return ret;15.*16.* 17.* */18. public interface MethodInterceptor extends Interceptor 19.20./*21.* Implement this method to perform extra treatments before and22.* after the invocation. Polite implementations would certainly23.* like to invoke link Joinpoint#proceed.24.*25.
11、* param invocation the method invocation joinpoint26.* return the result of the call to link27.* Joinpoint#proceed, might be intercepted by the28.* interceptor.29.*30.* throws Throwable if the interceptors or the31.* target-object throws an exception. */32.Object invoke(MethodInvocation invocation)
12、throws Throwable;33. 类前面的注释说明白该方法的使用,就是要在 invoke方法中调用 MethodInvocation.proceed,将执行传给下一个 Interceptor,最终执行目标方法。在 proceed方法前后加操作,到达 Aroud advice 的作用。在 Aspect 定义好后,就需要在 bean 定义文件中进展配置,通过 org.springframework.aop.framework.ProxyFactoryBean 的配置,指出接口、目标类和 Aspect。如下:1.2.3.4.5.6.7.8.9.throwAdvice10.!- 还可以连续加,
13、如11.logBeforeAdvice -12.13.14. 补充下,其中的目标类、解释器(Aspect)都要在 Bean 定义文件中先进展定义,然后才可以引用的。我们使用时,代码如下:1. IHello helloProxy=(IHello)context.getBean(“helloProxy“);2. helloProxy.hello;在 Bean 定义文件中配置的 Aspect 就会在适宜的 joinPoint 应用到目标方法上。补:IHello 接口中有 hello方法,HelloSpeaker 类实现了 IHello 接口。以免糊涂,给个实例如下,功能是在 hello方法前后参与日
14、志记录。1.public class AroundAdvice implements MethodInterceptor2.private Logger logger=3.Logger.getLogger(this.getClass.getName);4.Override5.public Object invoke(MethodInvocation methodInvocation) throws Throwable 6.7.logger.log(Level.INFO, “method starts.“+methodInvocation.getMethod);8.Object result=methodInvocation.proceed;9.logger.log(Level.INFO,“method ends.“+methodInvocation.getMethod);10.11.return result;12.13. 配置:1. 2. 5.IHello helloProxy=(IHello)context.getBean(“helloProxy“);helloProxy.hello(“ 前后加日志“);就会消灭如下结果:6.7.8.9.10.11.12.13.aroundAdvice14.15.16.17.