當destroy被調用實現ApplicationContextAware的類時,我們可以調用getBean嗎?破壞時獲取Bean
class Abc implements ApplicationContextAware{
private static ApplicationContext applicationContext = null;
MyTestClass obj = null;
public void destroy(){
System.out.print("Calling destroy ");
obj = (MyTestClass)applicationContext.getBean("myTestClassObject");
obj.showMsg(); // Calling show msg of MyTestClass.
}
}
在上面的例子中'applicationContext'爲null,所以'applicationContext.getBean'會拋出一個NPE –
其中是setApplicationContext方法?爲什麼你只是簡單地嘗試一下 – Panther