Spring事务处理及其AOP框架的内幕(2)

Spring事务处理及其AOP框架的内幕(2)

[@more@]2)事实上你还有很多这样的对象,现在我们希望在每个对象中添加我们的功能最后修改的时间,功能如下:

//$ID:IAuditable.java Created:2005-11-7 by Kerluse Benn

package com.osiris.springaop.advices.intruduction;

import java.util.Date;

public interface IAuditable {

void setLastModifiedDate(Date date);

Date getLastModifiedDate();

}

3)因为我们使用的切入类型是introduction,Spring AOP为我们提供了一个描述这种类型的接口IntroductionInterceptor,所以我们的切入实现处理,也需要实现这个接口:

//$ID:AuditableMixin.java Created:2005-11-7 by Kerluse Benn

package com.osiris.springaop.advices.intruduction;

import java.util.Date;

import org.aopalliance.intercept.MethodInvocation;

import org.springframework.aop.IntroductionInterceptor;

public class AuditableMixin implements IAuditable,IntroductionInterceptor{

private Date lastModifiedDate;

public Object invoke(MethodInvocation m) throws Throwable {

// TODO Add your codes here

if(implementsInterface(m.getMethod().getDeclaringClass())){

return m.getMethod().invoke(this,m.getArguments());

//invoke introduced mthod,here is IAuditable

}else{

return m.proceed(); //delegate other method

}

}

public Date getLastModifiedDate() {

// TODO Add your codes here

return lastModifiedDate;

}

public void setLastModifiedDate(Date date) {

// TODO Add your codes here

lastModifiedDate=date;

}

public boolean implementsInterface(Class cls) {

// TODO Add your codes here

return cls.isAssignableFrom(IAuditable.class);

}

}

4)ok,现在业务对象BookService类有了,自己希望添加的处理也有了IAuditable,那就剩下使用Spring AOP框架的问题了,配置bean.xml文件:
















singleton="false">


















false






true






com.osiris.springaop.advices.intruduction.IAuditable






AuditableAdvisor




来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7839366/viewspace-919919/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7839366/viewspace-919919/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值