Spring AOP 之IntroductionInterceptor

     不想说太多的废话,先看代码.

     代码结构图如下:

   

        代码如下:

        BookService.java

 /**
 * IntroductionSample
 */
package com.fhway.spring.aop;

/**
 * @author fuhw 2006-10-5 上午10:29:49
 */
public interface BookService {
 public String OrderComputerMagazine(String userName,String bookName);
 public String OrderBook(String userName,String bookName);

}

BookServiceImpl.java

/**
 * IntroductionSample
 */
package com.fhway.spring.aop;

/**
 * @author fuhw
 * 2006-10-5   上午10:31:10
 */
public class BookServiceImpl implements BookService {

 /* (non-Javadoc)
  * @see com.fhway.spring.aop.BookService#OrderBook(java.lang.String, java.lang.String)
  */
 public String OrderBook(String userName, String bookName) {
  // TODO Auto-generated method stub
  String result=null;
  result="订购"+bookName+"成功";
  return result;
 }

 /* (non-Javadoc)
  * @see com.fhway.spring.aop.BookService#OrderComputerMagazine(java.lang.String, java.lang.String)
  */
 public String OrderComputerMagazine(String userName, String bookName) {
  // TODO Auto-generated method stub
  String result=null;
  result="订购"+bookName+"成功";
  return result;
 }

}

AuditableMixin.java

/**
 * IntroductionSample
 */
package com.fhway.spring.aop.advices.intruduction;

import java.util.Date;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;

/**
 * @author fuhw 2006-10-5 上午10:34:25
 */
public class AuditableMixin implements IntroductionInterceptor, IAuditable {

 private Date lastModifiedDate;

 public boolean implementsInterface(Class cls) {
  // TODO Add your codes here
  return cls.isAssignableFrom(IAuditable.class);
 }

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

}
AuditableMixinDelegating.java

/**
 * IntroductionSample
 */
package com.fhway.spring.aop.advices.intruduction;

import java.util.Date;

import org.springframework.aop.support.DelegatingIntroductionInterceptor;

/**
 * @author fuhw 2006-10-5 上午10:34:25
 */
public class AuditableMixinDelegating extends DelegatingIntroductionInterceptor implements  IAuditable {

 private Date lastModifiedDate;

 public Date getLastModifiedDate() {
  // TODO Add your codes here
  return lastModifiedDate;
 }

 public void setLastModifiedDate(Date date) {
  // TODO Add your codes here
  lastModifiedDate = date;
 }

}

IAuditable.java

/**
 * IntroductionSample
 */
package com.fhway.spring.aop.advices.intruduction;

import java.util.Date;

/**
 * @author fuhw 2006-10-5 上午10:33:19
 */
public interface IAuditable {
 void setLastModifiedDate(Date date);
 Date getLastModifiedDate();

}

MainApp.java

/**
 * IntroductionSample
 */
package com.fhway.spring.aop.test;

import java.util.Date;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;


import com.fhway.spring.aop.BookService;
import com.fhway.spring.aop.advices.intruduction.IAuditable;

/**
 * @author fuhw
 * 2006-10-5   上午11:04:03
 */
public class MainApp {
 public static void main(String[] args) throws Exception{
  // TODO Add your codes here
  BeanFactory factory = new XmlBeanFactory(new FileSystemResource("bean.xml"));

  BookService bookService = (BookService)factory.getBean("BookService");
  
  IAuditable auditable = (IAuditable)bookService;
  
  System.out.print(bookService.OrderBook("Kerluse Benn","Professional C#"));
  auditable.setLastModifiedDate(new Date ());
  System.out.println(" 订购时间为"+auditable.getLastModifiedDate());
  Thread.sleep(10000);
  System.out.print(bookService.OrderBook("Kerluse Benn","Expert j2ee one-on-one"));
  auditable.setLastModifiedDate(new Date());
  System.out.println(" 订购时间为"+auditable.getLastModifiedDate());

 }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 <!-- Beans -->
 <bean id="BookServiceTarget"
  class="com.fhway.spring.aop.BookServiceImpl" singleton="false" />

 <!-- 1.introduction advice -->
 <bean id="AuditableMixin"
  class="com.fhway.spring.aop.advices.intruduction.AuditableMixin"
  singleton="false" />
 <!-- 2.introduction advice -->
 <bean id="AuditableMixinDelegating"
  class="com.fhway.spring.aop.advices.intruduction.AuditableMixinDelegating"
  singleton="false" />

 <!-- Introduction advisor -->
 <bean id="AuditableAdvisor"
  class="org.springframework.aop.support.DefaultIntroductionAdvisor"
  singleton="false">
  <constructor-arg>
 <!-- 1. or 2. -->
   <ref bean="AuditableMixinDelegating" />
  </constructor-arg>
 </bean>

 <bean id="BookService"
  class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="target">
   <ref bean="BookServiceTarget" />
  </property>
  <property name="singleton">
   <value>false</value>
  </property>

  <!-- force to use cglib -->
  <property name="proxyTargetClass">
   <value>true</value>
  </property>

  <!-- introduction methods -->
  <property name="proxyInterfaces">
   <value>
    com.fhway.spring.aop.advices.intruduction.IAuditable
   </value>
  </property>

  <property name="interceptorNames">
   <list>
    <value>AuditableAdvisor</value>
   </list>
  </property>
 </bean>
</beans>

      说明: 本例实现了IntroductionInterceptor的切入,她是AOP中功能最强的一个!你可以从有关资料的介绍里面的得到他的说明;我要在这里要谈的是以下几点:


     Spring比起其他的framework更有优势,因为除了AOP以外,它提供了更多别的功能。作为一个轻型 framework,它在J2EE不同的部分都可以发挥作用。因此,即使不想使用Spring AOP,你可能还是想使用Spring。另一个优点是,Spring并不要求开发团队所有的人员都会用它。学习Spring应该从Spring reference的第一页开始。读了本文后,你应该可以更好地理解Spring reference了。Spring唯一的缺点是缺乏更多的文档,但它的mailing list是个很好的补充,而且会不断地出现更多的文档。

     推荐看首页的Spring的入门联接和下面的一个链接 http://www.bestunix.net/index.php?job=art&articleid=a_20060409_220030

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值