您的位置:首页>软件开发>软件架构>

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

[ 来源:赛迪博客 | 更新日期:2007-7-15 20:17:01 | 评论 0 条 | 我要投稿 ]
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;

字串2


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() {
字串9


// 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 字串3
AuditableAdvisor



Tags:
责任编辑:
您的评论
用户名: 新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为