2016-03-03 49 views

回答

1

作爲方法名稱afterPropertiesSet()提示,它將在提供所有屬性後調用BeanFactory,並可用於初始化bean或確保其處於有效狀態。

比方說,我有一個String屬性不能爲null或空的bean。

public class MyBean { 

private String myString; 

    public void afterPropertiesSet() { 
     if(myString == null || myString.isEmpty()){ 
      throw IllegalStateException("myString must have text."); 
     } 
    } 

    public void setMyString(String myString) {  
     this.myString = myString; 
    } 
} 

當然,這只是一個非常簡單的代碼來演示如何在實際應用中使用。

HTH,

何塞·路易斯·

+0

感謝路易斯一個非常明確的答案:) –