//$ID:BookService.java Created:2005-11-6 by Kerluse Benn package com.osiris.springaop;
public interface BookService { public String OrderComputerMagazine(String userName,String bookName); public String OrderBook(String userName,String bookName); }
//$ID:BookServiceImpl.java Created:2005-11-6 by Kerluse Benn package com.osiris.springaop;
public class BookServiceImpl implements BookService{ public String OrderBook(String name,String bookName) { // TODO Add your codes here String result=null; result="订购"+bookName+"成功"; return result; }
public String OrderComputerMagazine(String userName, String bookName) { // TODO Add your codes here String result=null; result="订购"+bookName+"成功"; return result; } }
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(); }
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); }