yuyee

          2010年11月3日 #

          適配器

          適配器模式:將一個現有類實現的功能接口轉變為客戶希望的接口

          場景:你想使用一個已經存在的類,但是這個類的接口不符合需求,所以需要適配

          2中實現:一種是繼承,一種是委托,先來看看繼承

             

          第一步:系統現有功能

          package com.google.desginpattern.adapter;
          /**
           * 現有系統提供的功能
           * 
           * 
          @author Administrator
           * 
           
          */
          public class BMWCar {
          public void quickDriver() {
          System.out.println(
          "寶馬太快");
          }
          }

           

          第二步:客戶需要的接口

          package com.google.desginpattern.adapter;
          /**
           * 客戶需要的接口
           * 
          @author Administrator
           *
           
          */
          public interface Car {
          public void driver();
          public void brake();
          }

           

          第三步:實現客戶需要的功能

          package com.google.desginpattern.adapter;
          /**
           * 匹配客戶需求的實現
           * 
          @author Administrator
           *
           
          */
          public class CarAdapter extends BMWCar implements Car {
          @Override
          public void brake() {
          System.out.println(
          "剎車");
          }
          @Override
          public void driver() {
          quickDriver();
          }
          }

           

          測試類:

           

          package com.google.desginpattern.adapter;


          public class Test {

          public static void main(String[] args) {


          Car car 
          = new CarAdapter();

          car.brake();

          car.driver();

          }


          }

          輸出:

          剎車

          寶馬太快

          如果是委托的方式,改寫adapter

          package com.google.desginpattern.adapter;
          /**
           * 匹配客戶需求的實現
           * 
           * 
          @author Administrator
           * 
           
          */
          public class CarAdapter implements Car {
          private BMWCar car;
          @Override
          public void brake() {
          System.out.println(
          "剎車");
          }
          @Override
          public void driver() {
          car.quickDriver();
          }
          public BMWCar getCar() {
          return car;
          }
          public void setCar(BMWCar car) {
          this.car = car;
          }
          }

           

          posted @ 2010-11-29 22:28 羔羊| 編輯 收藏

          裝飾器


          裝飾器:裝飾器模式主要用于系統擴張功能用,在系統原有的功能上,擴展出其他的功能,JDKIO包用到很多,比如datainputstream之類,需要用其他流進行構造的上層類,符合面向對象設計的開閉原則

          下面我來寫個例子:

          首先,寫一個Car模版,定義基本屬性及行為功能driver

          package com.google.desginpattern.decoration;
          //其實這是個模版
          public abstract class Car {
          private int spreed;
          public int getSpreed() {
          return spreed;
          }
          public void setSpreed(int spreed) {
          this.spreed = spreed;
          }
          public abstract void driver();
          }

           

          第二步:具體車比如寶馬,這是目前系統中這個類能提供的功能

          package com.google.desginpattern.decoration;
          //目前系統中此類的功能
          public class BMWCar extends Car {
          @Override
          public void driver() {
          System.out.println(
          "我開著寶馬車");
          }
          }

           

          現在我想在這個類上擴展出其他功能,比如:泡妞

          第三步:定義一個裝飾模板,為什么給他定義個模板呢~因為可以給這個BMWCar類裝飾很不同的功能,不只泡妞一個~

          繼承Car父類,覆蓋driver功能,調用Car引用完成driver功能

          package com.google.desginpattern.decoration;
          //裝飾器父類
          public abstract class DecorationCar extends Car {
          // 引入car
          private Car car;
          @Override
          public void driver() {
          car.driver();
          // 調用此car來完成裝飾器的功能
          }
          public Car getCar() {
          return car;
          }
          public void setCar(Car car) {
          this.car = car;
          }
          }

           

          第四步:具體的裝飾功能,添加一個構造函數,參數為Car,為裝飾父類Car引用賦值,其實就是原來具體的功能類,回想下IO包里經常new的代碼段就明白~~

          package com.google.desginpattern.decoration;
          //具體的裝飾類,添加額外的泡妞功能
          public class DecorationBMWCar extends DecorationCar {
          public DecorationBMWCar(Car car) {
          super.setCar(car);
          }
          @Override
          public void driver() {
          // TODO Auto-generated method stub
          super.driver();// 調用原來的功能
          System.out.println("泡妞");// 添加額外的功能
          }
          }

           

          測試類:構造的方法很像IO包里的流

          package com.google.desginpattern.decoration;
          public class Test {
          public static void main(String[] args) {
          Car car 
          = new DecorationBMWCar(new BMWCar());
          car.driver();
          }
          }

           

          輸出:

          我開著寶馬車

          泡妞

          posted @ 2010-11-29 21:36 羔羊| 編輯 收藏

          IOC初始化和互相引用解決

               摘要: 觀察IOC中容器初始化某個Bean順序,現先一個JAVABean類,看看控制臺輸出:package com.google.aop.exception.ioc; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory...  閱讀全文

          posted @ 2010-11-10 16:27 羔羊| 編輯 收藏

          AbstractQueuedSynchronizer看看


          API中的解釋:為實現依賴于先進先出 (FIFO) 等待隊列的阻塞鎖定和相關同步器(信號量、事件,等等)提供一個框架。此類的設計目標是成為依靠單個原子 int 值來表示狀態的大多數同步器的一個有用基礎。子類必須定義更改此狀態的受保護方法,并定義哪種狀態對于此對象意味著被獲取或被釋放。假定這些條件之后,此類中的其他方法就可以實現所有排隊和阻塞機制。子類可以維護其他狀態字段,但只是為了獲得同步而只追蹤使用 getState()getState()getState()setState(int)compareAndSetState(int, int) 方法來操作以原子方式更新的 int 值。
          此類采用模板模式設計,此類為一個抽象類,但是沒抽象方法,每個sync子類需要實現5個受保護的方法

          這個5個方法在AbstractQueuedSynchronizer 都拋出throw new UnsupportedOperationException();
          AbstractQueuedSynchronizer 中有3個屬性:主要聲明一個狀態和一個wait queue,通過

          wait queue中Node 為一個雙向鏈表,需要去理解Node中幾個靜態字段值的意義,下面為他的源碼:
          static final class Node {
                  /** waitStatus value to indicate thread has cancelled */
                  static final int CANCELLED =  1;
                  /** waitStatus value to indicate successor's thread needs unparking */
                  static final int SIGNAL    = -1;
                  /** waitStatus value to indicate thread is waiting on condition */
                  static final int CONDITION = -2;
                  /** Marker to indicate a node is waiting in shared mode */
                  static final Node SHARED = new Node();
                  /** Marker to indicate a node is waiting in exclusive mode */
                  static final Node EXCLUSIVE = null;

                  /**
                   * Status field, taking on only the values:
                   *   SIGNAL:     The successor of this node is (or will soon be)
                   *               blocked (via park), so the current node must
                   *               unpark its successor when it releases or
                   *               cancels. To avoid races, acquire methods must
                   *               first indicate they need a signal,
                   *               then retry the atomic acquire, and then,
                   *               on failure, block.
                   *   CANCELLED:  This node is cancelled due to timeout or interrupt.
                   *               Nodes never leave this state. In particular,
                   *               a thread with cancelled node never again blocks.
                   *   CONDITION:  This node is currently on a condition queue.
                   *               It will not be used as a sync queue node until
                   *               transferred. (Use of this value here
                   *               has nothing to do with the other uses
                   *               of the field, but simplifies mechanics.)
                   *   0:          None of the above
                   *
                   * The values are arranged numerically to simplify use.
                   * Non-negative values mean that a node doesn't need to
                   * signal. So, most code doesn't need to check for particular
                   * values, just for sign.
                   *
                   * The field is initialized to 0 for normal sync nodes, and
                   * CONDITION for condition nodes.  It is modified only using
                   * CAS.
                   */
                  volatile int waitStatus;

                  /**
                   * Link to predecessor node that current node/thread relies on
                   * for checking waitStatus. Assigned during enqueing, and nulled
                   * out (for sake of GC) only upon dequeuing.  Also, upon
                   * cancellation of a predecessor, we short-circuit while
                   * finding a non-cancelled one, which will always exist
                   * because the head node is never cancelled: A node becomes
                   * head only as a result of successful acquire. A
                   * cancelled thread never succeeds in acquiring, and a thread only
                   * cancels itself, not any other node.
                   */
                  volatile Node prev;

                  /**
                   * Link to the successor node that the current node/thread
                   * unparks upon release. Assigned once during enqueuing, and
                   * nulled out (for sake of GC) when no longer needed.  Upon
                   * cancellation, we cannot adjust this field, but can notice
                   * status and bypass the node if cancelled.  The enq operation
                   * does not assign next field of a predecessor until after
                   * attachment, so seeing a null next field does not
                   * necessarily mean that node is at end of queue. However, if
                   * a next field appears to be null, we can scan prev's from
                   * the tail to double-check.
                   */
                  volatile Node next;

                  /**
                   * The thread that enqueued this node.  Initialized on
                   * construction and nulled out after use.
                   */
                  volatile Thread thread;

                  /**
                   * Link to next node waiting on condition, or the special
                   * value SHARED.  Because condition queues are accessed only
                   * when holding in exclusive mode, we just need a simple
                   * linked queue to hold nodes while they are waiting on
                   * conditions. They are then transferred to the queue to
                   * re-acquire. And because conditions can only be exclusive,
                   * we save a field by using special value to indicate shared
                   * mode.
                   */
                  Node nextWaiter;

                  /**
                   * Returns true if node is waiting in shared mode
                   */
                  final boolean isShared() {
                      return nextWaiter == SHARED;
                  }

                  /**
                   * Returns previous node, or throws NullPointerException if
                   * null.  Use when predecessor cannot be null.
                   * @return the predecessor of this node
                   */
                  final Node predecessor() throws NullPointerException {
                      Node p = prev;
                      if (p == null)
                          throw new NullPointerException();
                      else
                          return p;
                  }

                  Node() {    // Used to establish initial head or SHARED marker
                  }

                  Node(Thread thread, Node mode) {     // Used by addWaiter
                      this.nextWaiter = mode;
                      this.thread = thread;
                  }

                  Node(Thread thread, int waitStatus) { // Used by Condition
                      this.waitStatus = waitStatus;
                      this.thread = thread;
                  }


              }
          獲取鎖定調用的方法,其實這個方法是阻塞的:
            public final void acquire(int arg) {
                  if (!tryAcquire(arg) &&
                      acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
                      selfInterrupt();
              }
          如果獲取不成功則調用如下方法:
             final boolean acquireQueued(final Node node, int arg) {
                  try {
                      boolean interrupted = false;
                      for (;;) {
                          final Node p = node.predecessor();
                          if (p == head && tryAcquire(arg)) {//當節點是頭節點且獨占時才返回
                              setHead(node);
                              p.next = null; // help GC
                              return interrupted;
                          }
                          if (shouldParkAfterFailedAcquire(p, node) &&
                              parkAndCheckInterrupt())//阻塞并判斷是否打斷,其實這個判斷才是自旋鎖真正的猥瑣點,
          意思是如果你的前繼節點不是head,而且當你的前繼節點狀態是Node.SIGNAL時,你這個線程將被park(),直到另外的線程release時,發現head.next是你這個node時,才unpark,你才能繼續循環并獲取鎖
                              interrupted = true;
                      }
          shouldParkAfterFailedAcquire這個方法刪除所有waitStatus>0也就是CANCELLED狀態的Node,并設置前繼節點為signal
           private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
                  int s = pred.waitStatus;
                  if (s < 0)
                      /*
                       * This node has already set status asking a release
                       * to signal it, so it can safely park
                       */
                      return true;
                  if (s > 0) {
                      /*
                       * Predecessor was cancelled. Skip over predecessors and
                       * indicate retry.
                       */
             do {
          node.prev = pred = pred.prev;
             } while (pred.waitStatus > 0);
             pred.next = node;
          }
                  else
                      /*
                       * Indicate that we need a signal, but don't park yet. Caller
                       * will need to retry to make sure it cannot acquire before
                       * parking.
                       */
                      compareAndSetWaitStatus(pred, 0, Node.SIGNAL);
                  return false;
              }


          使用LockSupport.park(this),禁用當前線程
          private final boolean parkAndCheckInterrupt() {
                  LockSupport.park(this);//block
                  return Thread.interrupted();
              }
          釋放鎖:
              public final boolean release(int arg) {
                  if (tryRelease(arg)) {
                      Node h = head;
                      if (h != null && h.waitStatus != 0)
                          unparkSuccessor(h);//unblock
                      return true;
                  }
                  return false;
              }
          private void unparkSuccessor(Node node) {
                  /*
                   * Try to clear status in anticipation of signalling.  It is
                   * OK if this fails or if status is changed by waiting thread.
                   */
                  compareAndSetWaitStatus(node, Node.SIGNAL, 0);

                  /*
                   * Thread to unpark is held in successor, which is normally
                   * just the next node.  But if cancelled or apparently null,
                   * traverse backwards from tail to find the actual
                   * non-cancelled successor.
                   */
                  Node s = node.next;
                  if (s == null || s.waitStatus > 0) {
                      s = null;
                      for (Node t = tail; t != null && t != node; t = t.prev)
                          if (t.waitStatus <= 0)
                              s = t;
                  }
                  if (s != null)
                      LockSupport.unpark(s.thread);
              }


                  } catch (RuntimeException ex) {
                      cancelAcquire(node);
                      throw ex;
                  }
              }
            

          看下ReentrantLock鎖中sync的實現:
           static abstract class Sync extends AbstractQueuedSynchronizer {
                  private static final long serialVersionUID = -5179523762034025860L;

                  /**
                   * Performs {@link Lock#lock}. The main reason for subclassing
                   * is to allow fast path for nonfair version.
                   */
                  abstract void lock();

                  /**
                   * Performs non-fair tryLock.  tryAcquire is
                   * implemented in subclasses, but both need nonfair
                   * try for trylock method.
                   */
                  final boolean nonfairTryAcquire(int acquires) {
                      final Thread current = Thread.currentThread();
                      int c = getState();
                      if (c == 0) {
                          if (compareAndSetState(0, acquires)) {
                              setExclusiveOwnerThread(current);
                              return true;
                          }
                      }
                      else if (current == getExclusiveOwnerThread()) {
                          int nextc = c + acquires;
                          if (nextc < 0) // overflow
                              throw new Error("Maximum lock count exceeded");
                          setState(nextc);
                          return true;
                      }
                      return false;
                  }

                  protected final boolean tryRelease(int releases) {
                      int c = getState() - releases;
                      if (Thread.currentThread() != getExclusiveOwnerThread())
                          throw new IllegalMonitorStateException();
                      boolean free = false;
                      if (c == 0) {
                          free = true;
                          setExclusiveOwnerThread(null);
                      }
                      setState(c);
                      return free;
                  }

                  protected final boolean isHeldExclusively() {
                      // While we must in general read state before owner,
                      // we don't need to do so to check if current thread is owner
                      return getExclusiveOwnerThread() == Thread.currentThread();
                  }

                  final ConditionObject newCondition() {
                      return new ConditionObject();
                  }

                  // Methods relayed from outer class

                  final Thread getOwner() {
                      return getState() == 0 ? null : getExclusiveOwnerThread();
                  }

                  final int getHoldCount() {
                      return isHeldExclusively() ? getState() : 0;
                  }

                  final boolean isLocked() {
                      return getState() != 0;
                  }

                  /**
                   * Reconstitutes this lock instance from a stream.
                   * @param s the stream
                   */
                  private void readObject(java.io.ObjectInputStream s)
                      throws java.io.IOException, ClassNotFoundException {
                      s.defaultReadObject();
                      setState(0); // reset to unlocked state
                  }
              }
          非公平規則下nonfairTryAcquire,獲取當前鎖的state,通過CAS原子操作設置為1,并將當前線程設置為獨占線程,如果當前線程已經拿了鎖,則state增加1
          公平鎖中 有如下判斷:
          if (isFirst(current) &&//判斷頭元素
                              compareAndSetState(0, acquires)) {
                              setExclusiveOwnerThread(current);
                              return true;
                          }
          在獲取鎖步驟:
          1.調用tryAcquire來獲取,如果失敗,則進入2
          2.調用addWaiter,以獨占模式將node放到tail位置
          3.調用acquireQueued方法,此方法阻塞,直到node的pre為head,并成功獲取鎖定,也可能存在阻塞并打斷情況
          釋放鎖的步驟:
          1.放棄排他鎖持有權
          2.unpark 節點的下一個blocked節點

          公平鎖與非公平鎖:從代碼上看,非公平鎖是讓當前線程優先獨占,而公平鎖則是讓等待時間最長的線程優先,非公平的可能讓其他線程沒機會執行,而公平的則可以讓等待時間最長的先執行,但是性能上會差點

          posted @ 2010-11-09 15:30 羔羊| 編輯 收藏

          linkedhashmap看看

          linkedhashmap繼承自hashmap,他的底層維護的一個鏈表,    private transient Entry<K,V> header 來記錄元素的插入順序和訪問順序;
          hashmap的構造函數中調用init()方法,而linkedhashmap中重寫了init(),將head元素初始化
             void init() {
                  header = new Entry<K,V>(-1, null, null, null);
                  header.before = header.after = header;
              }

          private final boolean accessOrder這個屬性表示是否要根據訪問順序改變線性結構
          在linkedhashmap中改寫了hashmap的get()方法,增加了 e.recordAccess(this),這個方法主要是根據accessOrder的值判斷是否需要實現LRU,
           void recordAccess(HashMap<K,V> m) {
                      LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
                      if (lm.accessOrder) {
                          lm.modCount++;
                          remove();
                          addBefore(lm.header);
                      }
                  }

          addBefore這個方法是把剛訪問的元素放到head的前面
           private void addBefore(Entry<K,V> existingEntry) {
                      after  = existingEntry;
                      before = existingEntry.before;
                      before.after = this;
                      after.before = this;
                  }
          put方法繼承自hashmap,hashmap預留了 e.recordAccess(this)這個方法:
               public V put(K key, V value) {
                  if (key == null)
                      return putForNullKey(value);
                  int hash = hash(key.hashCode());
                  int i = indexFor(hash, table.length);
                  for (Entry<K,V> e = table[i]; e != null; e = e.next) {
                      Object k;
                      if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                          V oldValue = e.value;
                          e.value = value;
                          e.recordAccess(this);
                          return oldValue;
                      }
                  }

                  modCount++;
                  addEntry(hash, key, value, i);
                  return null;
              }

          并通過重寫 addEntry(hash, key, value, i)這個方法,實現LRU中的刪除動作:
              void addEntry(int hash, K key, V value, int bucketIndex) {
                  createEntry(hash, key, value, bucketIndex);

                  // Remove eldest entry if instructed, else grow capacity if appropriate
                  Entry<K,V> eldest = header.after;//找到最老的元素,這個在addBefore里確定,初次賦值是當只有一個head時候,你插入一個元素
                  if (removeEldestEntry(eldest)) {//這個是受保護的方法,需要自己制定刪除策略,比如size() > 最大容量,可自己實現,默認為false,也就是不開啟
                      removeEntryForKey(eldest.key);
                  } else {
                      if (size >= threshold)
                          resize(2 * table.length);
                  }
              }
          自己重寫這個方法,指定刪除策略:
           protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
                  return false;
              }
          因此,可用linkedhashmap 構建一個基于LRU算法的緩存。


          package com.google.study.cache;

          import java.util.LinkedHashMap;
          import java.util.concurrent.locks.ReentrantLock;

          public class SimpleCache<K, V> extends LinkedHashMap<K, V> {

          private int maxCapacity;
          private ReentrantLock lock = new ReentrantLock();

          public SimpleCache(int maxCapacity, float load_factory) {
          super(maxCapacity, load_factory, true);
          this.maxCapacity = maxCapacity;
          }

          public int getMaxCapacity() {
          return maxCapacity;
          }

          public void setMaxCapacity(int maxCapacity) {
          this.maxCapacity = maxCapacity;
          }

          @Override
          protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
          // TODO Auto-generated method stub
          return super.removeEldestEntry(eldest);
          }

          public V get(Object key) {
          lock.lock();
          try {
          return super.get(key);
          } finally {
          lock.unlock();
          }

          }

          public V put(K k, V v) {
          lock.lock();
          try {
          return super.put(k, v);
          } finally {
          lock.unlock();
          }
          }

          @Override
          public void clear() {
          lock.lock();
          try {
          super.clear();
          } finally {
          lock.unlock();
          }
          }

          @Override
          public int size() {

          lock.lock();
          try {
          return super.size();
          } finally {
          lock.unlock();
          }
          }

          }



          posted @ 2010-11-08 23:14 羔羊| 編輯 收藏

          IOC監管Bean

          1.InitializingBean接口,在初始化Bean時容器會調用前者的afterPropertiesSet()方法

          2.DisposableBean接口,在析構Bean時容器會調用destroy()方法

          3.BeanFactoryAware接口,當它被BeanFactory創建后,它會擁有一個指向創建它的BeanFactory的引用

          4.BeanPostProcessor接口,這個接口兩個方法,postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName) 在其他Bean構造前后執行

          5.BeanFactoryPostProcessor接口,Spring IoC容器允許BeanFactoryPostProcessor在容器實際實例化任何其它的bean之前讀取配置元數據,并有可能修改它

          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class A implements BeanPostProcessor, InitializingBean,
          BeanFactoryAware, BeanFactoryPostProcessor,DisposableBean {

          public A() {
          System.out.println("Class A");
          }
          private void init(){
          System.out.println("Class A init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          // TODO Auto-generated method stub
          System.out.println("Class A afterPropertiesSet()");

          }

          @Override
          public void destroy() throws Exception {
          System.out.println("Class A destroy()");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A setBeanFactory()");
          }

          @Override
          public Object postProcessAfterInitialization(Object bean, String beanName)
          throws BeansException {
          System.out.println("Class A postProcessAfterInitialization");
          return null;
          }

          @Override
          public Object postProcessBeforeInitialization(Object bean, String beanName)
          throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A postProcessBeforeInitialization");
          return null;
          }


          @Override
          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A postProcessBeanFactory");
          }

          }
          執行結果:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          創建一個B類,由A來監管B類,B實現 InitializingBean,BeanFactoryAware,BeanFactoryPostProcessor
          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class B implements InitializingBean, BeanFactoryAware,
          BeanFactoryPostProcessor {

          public B() {
          System.out.println("Class B");
          }

          public void init() {
          System.out.println("Class B init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          System.out.println("Class B afterPropertiesSet");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          System.out.println("Class B beanFactory");

          }

          @Override
          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class B postProcessBeanFactory");
          }

          }
          執行結果:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class B
          Class B beanFactory
          Class B afterPropertiesSet
          Class B init
          Class A postProcessBeanFactory
          Class B postProcessBeanFactory

          可以看出A并沒有監管B類,也就是沒調用BeanPostProcessor這個接口的2個方法
          再來去掉B上的BeanFactoryPostProcessor接口
          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class B implements InitializingBean,
          BeanFactoryAware{

          public B() {
          System.out.println("Class B");
          }

          public void init() {
          System.out.println("Class B init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          System.out.println("Class B afterPropertiesSet");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          System.out.println("Class B beanFactory");

          }

          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class B postProcessBeanFactory");
          }


          }
          執行輸出:Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          2010-11-3 21:33:31 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8916a2: defining beans [A,B]; root of factory hierarchy
          Class B
          Class B beanFactory
          Class A postProcessBeforeInitialization
          2010-11-3 21:33:31 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
          信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8916a2: defining beans [A,B]; root of factory hierarchy
          Class A destroy()
          Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'B' defined in class path resource [icoContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
          at java.security.AccessController.doPrivileged(Native Method)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
          at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
          at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
          at com.google.springioctest.Test.main(Test.java:8)
          Caused by: java.lang.NullPointerException
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1393)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
          ... 15 more
          拋異常了。。。。。
          原因是A類里的postProcessBeforeInitialization,postProcessAfterInitialization2個方法沒有返回bean,修改下
          執行輸出:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          2010-11-3 21:37:10 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1193779: defining beans [A,B]; root of factory hierarchy
          Class B
          Class B beanFactory
          Class A postProcessBeforeInitialization
          Class B afterPropertiesSet
          Class B init
          Class A postProcessAfterInitialization
          在B類被初始化之后,也就是調用afterPropertiesSet之前那段時間,屬性初始化完成后,進行了回調,控制B類

          注意:在寫被監控的Bean的時候,不能實現BeanFactoryPostProcessor這個接口,沒看源碼,其實也不知道是什么原因,哈哈,只能硬記了

          posted @ 2010-11-03 17:04 羔羊| 編輯 收藏

          嵌套事務

          在所有使用 spring 的應用中, 聲明式事務管理可能是使用率最高的功能了, 但是, 從我觀察到的情況看, 絕大多數人并不能深刻理解事務聲明中不同事務傳播屬性配置的的含義, 讓我們來看一下TransactionDefinition 接口中的定義 ,在 spring 中一共定義了六種事務傳播屬性, 如果你覺得看起來不夠直觀, 那么我來轉貼一個滿大街都有的翻譯 
          PROPAGATION_REQUIRED -- 支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。 
          PROPAGATION_SUPPORTS 
          -- 支持當前事務,如果當前沒有事務,就以非事務方式執行。 
          PROPAGATION_MANDATORY 
          -- 支持當前事務,如果當前沒有事務,就拋出異常。 
          PROPAGATION_REQUIRES_NEW 
          -- 新建事務,如果當前存在事務,把當前事務掛起。 
          PROPAGATION_NOT_SUPPORTED 
          -- 以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。 
          PROPAGATION_NEVER 
          -- 以非事務方式執行,如果當前存在事務,則拋出異常。 
          PROPAGATION_NESTED 
          -- 如果當前存在事務,則在嵌套事務內執行。如果當前沒有事務,則進行與PROPAGATION_REQUIRED類似的操作。 
          前六個策略類似于EJB CMT,第七個(PROPAGATION_NESTED)是Spring所提供的一個特殊變量。 
          它要求事務管理器或者使用JDBC 
          3.0 Savepoint API提供嵌套事務行為(如Spring的DataSourceTransactionManager) 

          在我所見過的誤解中, 最常見的是下面這種: 
          假如有兩個業務接口 ServiceA 和 ServiceB, 其中 ServiceA 中有一個方法實現如下 
          /** 
          * 事務屬性配置為 PROPAGATION_REQUIRED 
          */
           
          void methodA() 
          // 調用 ServiceB 的方法 
          ServiceB.methodB(); 
          }
           
          那么如果 ServiceB 的 methodB  如果配置了事務, 就必須配置為 PROPAGATION_NESTED 


          這種想法可能害了不少人, 認為 Service 之間應該避免互相調用, 其實根本不用擔心這點,PROPAGATION_REQUIRED 已經說得很明白, 
          如果當前線程中已經存在事務, 方法調用會加入此事務, 果當前沒有事務,就新建一個事務, 所以 ServiceB#methodB() 的事務只要遵循最普通的規則配置為 PROPAGATION_REQUIRED 即可, 如果 ServiceB#methodB (我們稱之為內部事務, 為下文打下基礎) 拋了異常, 那么 ServiceA#methodA(我們稱之為外部事務) 如果沒有特殊配置此異常時事務提交 (即 +MyCheckedException的用法), 那么整個事務是一定要 rollback 的, 什么 Service 只能調 Dao 之類的言論純屬無稽之談, spring 只負責配置了事務屬性方法的攔截, 它怎么知道你這個方法是在 Service 還是 Dao 里 ? 

          最容易弄混淆的其實是 PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED, 那么這兩種方式又有何區別呢? 我簡單的翻譯一下 Juergen Hoeller 的話 : 
          PROPAGATION_REQUIRES_NEW 啟動一個新的, 不依賴于環境的 "內部" 事務. 這個事務將被完全 commited 或 rolled back 而不依賴于外部事務, 它擁有自己的隔離范圍, 自己的鎖, 等等. 當內部事務開始執行時, 外部事務將被掛起, 內務事務結束時, 外部事務將繼續執行. 

              另一方面, PROPAGATION_NESTED 開始一個 "嵌套的" 事務,  它是已經存在事務的一個真正的子事務. 潛套事務開始執行時,  它將取得一個 savepoint. 如果這個嵌套事務失敗, 我們將回滾到此 savepoint. 潛套事務是外部事務的一部分, 只有外部事務結束后它才會被提交. 

              由此可見, PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED 的最大區別在于, PROPAGATION_REQUIRES_NEW 完全是一個新的事務, 而 PROPAGATION_NESTED 則是外部事務的子事務, 如果外部事務 commit, 潛套事務也會被 commit, 這個規則同樣適用于 roll back.

           那么外部事務如何利用嵌套事務的 savepoint 特性呢, 我們用代碼來說話

           

          ServiceA {       
              
          /**  
               * 事務屬性配置為 PROPAGATION_REQUIRED  
               
          */
            
              
          void methodA() {   
                  ServiceB.methodB();    }
             
           }
               
          ServiceB 
          {      
              
          /**  
               * 事務屬性配置為 PROPAGATION_REQUIRES_NEW  
               
          */
              
              
          void methodB() {   
              }
                   
          }
             

          這種情況下, 因為 ServiceB#methodB 的事務屬性為 PROPAGATION_REQUIRES_NEW, 所以兩者不會發生任何關系, ServiceA#methodA 和 ServiceB#methodB 不會因為對方的執行情況而影響事務的結果, 因為它們根本就是兩個事務, 在 ServiceB#methodB 執行時 ServiceA#methodA 的事務已經掛起了 (關于事務掛起的內容已經超出了本文的討論范圍, 有時間我會再寫一些掛起的文章) . 

          ServiceA {      
              
          /**  
               * 事務屬性配置為 PROPAGATION_REQUIRED  
               
          */
            
              
          void methodA() {   
                  ServiceB.methodB();   
              }
             
          }
               
          ServiceB 
          {         
              
          /**  
               * 事務屬性配置為 PROPAGATION_NESTED  
               
          */
              
              
          void methodB() {   
              }
                   
          }
           

          ServiceB#methodB 如果 rollback, 那么內部事務(即 ServiceB#methodB) 將回滾到它執行前的 SavePoint(注意, 這是本文中第一次提到它, 潛套事務中最核心的概念), 而外部事務(即 ServiceA#methodA) 可以有以下兩種處理方式: 
          1. 改寫 ServiceA 如下 

          ServiceA {      
              
          /**  
               * 事務屬性配置為 PROPAGATION_REQUIRED  
               
          */
            
              
          void methodA() {   
                  
          try {   
                      ServiceB.methodB();   
                  }
           catch (SomeException) {   
                      
          // 執行其他業務, 如 ServiceC.methodC();   
                  }
             
              }
              
          }
            

          這種方式也是潛套事務最有價值的地方, 它起到了分支執行的效果, 如果 ServiceB.methodB 失敗, 那么執行 ServiceC.methodC(), 而 ServiceB.methodB 已經回滾到它執行之前的 SavePoint, 所以不會產生臟數據(相當于此方法從未執行過), 這種特性可以用在某些特殊的業務中, 而 PROPAGATION_REQUIRED 和 PROPAGATION_REQUIRES_NEW 都沒有辦法做到這一點. (題外話 : 看到這種代碼, 似乎似曾相識, 想起了 prototype.js 中的 Try 函數 ) 

          2. 代碼不做任何修改, 那么如果內部事務(即 ServiceB#methodB) rollback, 那么首先 ServiceB.methodB 回滾到它執行之前的 SavePoint(在任何情況下都會如此), 外部事務(即 ServiceA#methodA) 將根據具體的配置決定自己是 commit 還是 rollback (+MyCheckedException). 
          上面大致講述了潛套事務的使用場景, 下面我們來看如何在 spring 中使用 PROPAGATION_NESTED, 首先來看 AbstractPlatformTransactionManager 

          JdbcTransactionObjectSupport 告訴我們必須要滿足兩個條件才能 createSavepoint : 
            
          2. java.sql.Savepoint 必須存在, 即 jdk 版本要 1.4+ 
          3. Connection.getMetaData().supportsSavepoints() 必須為 true, 即 jdbc drive 必須支持 JDBC 3.0 


          確保以上條件都滿足后, 你就可以嘗試使用 PROPAGATION_NESTED 了.

          posted @ 2010-11-03 00:11 羔羊| 編輯 收藏

          主站蜘蛛池模板: 武陟县| 迭部县| 西城区| 丹凤县| 大新县| 焦作市| 剑河县| 建昌县| 醴陵市| 加查县| 丹东市| 黄山市| 剑河县| 河北省| 曲周县| 定兴县| 固原市| 隆子县| 五莲县| 谷城县| 桦川县| 宁蒗| 丘北县| 舟山市| 永寿县| 南昌市| 保定市| 隆安县| 巴彦淖尔市| 靖州| 抚顺市| 祁东县| 榆树市| 景德镇市| 盐山县| 义乌市| 焦作市| 肥东县| 武定县| 钦州市| 大关县|