spring recipes笔记 - 使用经典的spring通知来模块化横切关注点

虽然动态代理在模块化横切关注点方面很有帮助,但编写如此低层次代码对应用开发者来说太过苛刻。

Aop为应用程序开发者定义了一组高层次的概念,用于表达横切关注点。
经典的spring aop支持4种类型的通知:

[b]1前置通知
2返回通知
3异常通知
4环绕通知[/b]

[b]前置通知在方法执行之前执行,可以通过实现MethodBeforeAdvice接口创建它[/b]

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);
}


[b]返回通知在方法执行后记录方法的结束和返回的结果,可以通过实现AfterReturningAdvice接口创建它[/b]


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>


[b]第三种通知是异常通知,要能够产生异常,要为算术计算器增加一个检查,有异常时,将抛出Exception[/b]

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>



[b]环绕通知,在所有通知类型中,它是最强大的,因为它能完全控制方法的执行过程,所以可以把前面所有通知动作合并到一个单独的通知里面。[/b]

环绕通知必须实现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切入点匹配方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值