虽然动态代理在模块化横切关注点方面很有帮助,但编写如此低层次代码对应用开发者来说太过苛刻。
Aop为应用程序开发者定义了一组高层次的概念,用于表达横切关注点。
经典的spring aop支持4种类型的通知:
1前置通知
2返回通知
3异常通知
4环绕通知
前置通知在方法执行之前执行,可以通过实现MethodBeforeAdvice接口创建它
public class LoggingBeforeAdvice implements MethodBeforeAdvice{
private Log log = LogFactory.getLog(this.getClass());
public void before(Method method, Object[] arg1, Object target)
throws Throwable {
log.info("the method "+method.getName()+"() start");
}
}
接下来,为每个计算器Bean创建一个代理以应用该通知,在spring aop里,代理的创建是通过一个叫ProxyFactryBean的工厂bean完成
<bean id="loggingBeforeAdvice" class="com.netease.z3.LoggingBeforeAdvice"></bean>
<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="compute"></property>
<property name="interceptorNames">
<list>
<value>loggingBeforeAdvice</value>
</list>
</property>
</bean>
ProxyFactoryBean只为实现了任意接口的目标bean创建jdk代理。如果目标bean没有实现任何接口,那么ProxyFactoryBean将创建CGLIB代理。
在Main类里,应用从IOC容器获取应用了日志代理通知的代理Bean
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("z3.xml");
Compute compute = (Compute)context.getBean("computeProxy");
compute.add(10,10);
}
返回通知在方法执行后记录方法的结束和返回的结果,可以通过实现AfterReturningAdvice接口创建它
public class LoggingAfterAdvice implements AfterReturningAdvice{
private Log log = LogFactory.getLog(this.getClass());
public void afterReturning(Object returnValue, Method method, Object[] arg,
Object target) throws Throwable {
// TODO Auto-generated method stub
log.info("the method "+method.getName()+" end "+returnValue);
}
}
要使这个通知生效,需要在IOC容器里声明一个它的实例,然后在interceptorNames属性里面增加一项对它的引用
<bean id="loggingAfterAdvice" class="com.netease.z3.LoggingAfterAdvice"></bean>
<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="compute"></property>
<property name="interceptorNames">
<list>
<value>loggingBeforeAdvice</value>
<value>loggingAfterAdvice</value>
</list>
</property>
</bean>
第三种通知是异常通知,要能够产生异常,要为算术计算器增加一个检查,有异常时,将抛出Exception
public class ComputeImpl implements Compute {
private Log log = LogFactory.getLog(this.getClass());
/* (non-Javadoc)
* @see com.netease.dao.Compute#add(double, double)
*/
public void div(double a,double b){
if(b==0){
throw new IllegalArgumentException("by zero");
}
}
}
对于异常通知类型,必须实现ThrowsAdvice接口,每个处理方法的程序必须是afterThrowing.异常的类型由方法的参数类型指定。
public class LoggingThrowsAdvice implements ThrowsAdvice{
private Log log = LogFactory.getLog(this.getClass());
public void afterThrowing(IllegalArgumentException e)throws Throwable{
log.info("Illegal argument");
}
}
在ioc容器声明一个该通知的实例,然后在interceptorNames属性增加一项对它的引用
<bean id="loggingThrowsAdvice" class="com.netease.z3.LoggingThrowsAdvice"></bean>
<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="compute"></property>
<property name="interceptorNames">
<list>
<value>loggingBeforeAdvice</value>
<value>loggingAfterAdvice</value>
<value>loggingThrowsAdvice</value>
</list>
</property>
</bean>
环绕通知,在所有通知类型中,它是最强大的,因为它能完全控制方法的执行过程,所以可以把前面所有通知动作合并到一个单独的通知里面。
环绕通知必须实现MethodIntercepor接口,如果要继续执行原始方法那么必须调用methodInvocation.proceed(),如果忘记这步,原始的方法是不会被调用,下面环绕通知合并了前面的前置,后置,异常通知.
public class LoggingAroundAdvice implements MethodInterceptor {
private Log log = LogFactory.getLog(this.getClass());
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
log.info("the method "+methodInvocation.getMethod().getName()+"() start"+
" with "+Arrays.toString(methodInvocation.getArguments()));
try{
Object result = methodInvocation.proceed();
log.info("the method "+methodInvocation.getMethod().getName()+"() end "+result);
return result;
}catch(Exception e){
log.error("error");
throw e;
}
}
}
<bean id="loggingAroundAdvice" class="com.netease.z3.LoggingAroundAdvice"></bean>
<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="compute"></property>
<property name="interceptorNames">
<list>
<value>loggingAroundAdvice</value>
</list>
</property>
</bean>
下一篇:spring recipes笔记 - 使用经典的spring切入点匹配方法
分享到:
- 2009-10-26 00:19
- 浏览 1465
- 评论(1)
- 论坛回复 / 浏览 (1 / 1903)
- 查看更多
相关推荐
获取Spring Boot 2微框架的可重用代码配方和代码段 了解Spring Boot 2如何与其他Spring API,工具和框架集成 访问Spring MVC和新的Spring Web Sockets,以实现更简单的Web开发 使用微服务进行Web服务开发并与Spring ...
Spring Recipes: A Problem-Solution Approach, Second Edition With over 3 Million users/developers, Spring Framework is the leading “out of the box” Java framework. Spring addresses and offers simple...
Spring Recipes A Problem-Solution Approach (Books for Professionals by Professionals).rar
关注最新Spring 2.5构建企业Java应用。 This book focuses on the latest Spring 2.5 features for building enterprise Java applications.
Spring Recipes A Problem-Solution Approach
Spring Recipes A Problem-Solution Approach Third Edition 覆盖Spring 4.0
赠送jar包:curator-recipes-2.6.0.jar; 赠送原API文档:curator-recipes-2.6.0-javadoc.jar; 赠送源代码:curator-recipes-2.6.0-sources.jar; 赠送Maven依赖信息文件:curator-recipes-2.6.0.pom; 包含翻译后...
Spring Recipes: A Problem-Solution Approach, Third Edition builds upon the best-selling success of the previous editions and focuses on the latest Spring Framework features for building enterprise ...
Spring 5 Recipes A Problem-Solution Approach(4th).pdfSpring 5 Recipes A Problem-Solution Approach(4th).pdf
Using a problem-solution approach, Spring Boot 2 Recipes quickly introduces you to Pivotal's Spring Boot 2 micro-framework, then dives into code snippets on how to apply and integrate Spring Boot 2 ...
Spring Recipes, 3rd Edition.pdf
Spring Recipes 3rd Edition Sources === These are the sources belonging to Spring Recipes 3rd Edition. Each chapter has its own sources and each chapter can contain multiple source snippets TOC --- ...
One of the first (if not the first) books on the latest Spring 3.x, and the first Spring code Recipes book focused on Spring Web-tier development The Spring framework is growing. It has always been ...
The recipes in Spring 5 Recipes cover Spring fundamentals such as Spring IoC container, Spring AOP/ AspectJ, and more. Other recipes include Spring enterprise solutions for topics such as Spring Java ...
The recipes in Spring 5 Recipes cover Spring fundamentals such as Spring IoC container, Spring AOP/ AspectJ, and more. Other recipes include Spring enterprise solutions for topics such as Spring Java ...
SPRING RECIPES-A PROBLEM SOLUTION APPROACH
个人认为相当好的一半讲解spring的书籍,英文版
Spring 5 Recipes A Problem-Solution Approach
赠送jar包:curator-recipes-4.3.0.jar; 赠送原API文档:curator-recipes-4.3.0-javadoc.jar; 赠送源代码:curator-recipes-4.3.0-sources.jar; 赠送Maven依赖信息文件:curator-recipes-4.3.0.pom; 包含翻译后...