用spring的InitializingBean的afterPropertiesSet來初始化

org.springframework.beans.factory包下有一個接口是InitializingBean 只有一個方法:

/**
  * Invoked by a BeanFactory after it has set all bean properties supplied
  * (and satisfied BeanFactoryAware and ApplicationContextAware).
  * <p>This method allows the bean instance to perform initialization only
  * possible when all bean properties have been set and to throw an
  * exception in the event of misconfiguration.
  * @throws Exception in the event of misconfiguration (such
  * as failure to set an essential property) or if initialization fails.
  */
 void afterPropertiesSet() throws Exception;

 

這個方法將在所有的屬性被初始化後調用。

但是會在init前調用。

但是主要的是如果是延遲加載的話,則馬上執行。

所以可以在類上加上註解:

import org.springframework.context.annotation.Lazy;

@Lazy(false)

這樣spring容器初始化的時候afterPropertiesSet就會被調用。

只需要實現InitializingBean接口就行。

如果代碼在這裏初始化的話,半天都找不到。。

轉載:http://www.cnblogs.com/java-boy/archive/2012/12/21/2827729.html

發佈了0 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章