我想在contextDestroyed中獲取bean,所以我有這段代碼。如何在上下文中從ServletContextListener中獲取beanDestroyed
public class MyContextListener implements ApplicationContextAware, ServletContextListener {
private final Logger logger = LogManager.getLogger(getClass());
private ApplicationContext appContext;
@Override
public void contextInitialized(ServletContextEvent event) {
logger.warn("Start");
}
@Override
public void contextDestroyed(ServletContextEvent event) {
logger.warn("End" + appContext);
appContext.getBean("myBean")
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.appContext = applicationContext;
}
}
問題是它記錄了null
。但當我改變它
private static ApplicationContext appContext;
然後我可以看到我的appContext。爲什麼?獲取豆類的正確選擇是什麼
hm thx。我不知道2個實例。 – hudi