ConfigurableApplicationContext vs ApplicationContext

☆樱花仙子☆ 提交于 2019-12-18 05:00:10

问题


I have been learning spring framework for some time.But whenever I look for initialization and destruction callbacks, every example uses ConfigurableApplicationContext instead of ApplicationContext. Can someone explain why?


回答1:


Because all common implementation of ApplicationContext are in fact ConfigurableApplicationContext. Extract from javadoc for ConfigurableApplicationContext :

SPI interface to be implemented by most if not all application contexts.

and

Configuration and lifecycle methods are encapsulated here to avoid making them obvious to ApplicationContext client code. The present methods should only be used by startup and shutdown code

That means that when you use an application context, i.e. get beans from it you only use it as an ApplicationContext but when you manage its life cycle (initialization and destruction) you use methods from ConfigurableApplicationContext




回答2:


ApplicationContext: This the most powerful Container, compare to Bean-factory (Core container).

ConfigurableApplicationContext: One of the implementation of the ApplicationContext container. This container is used for Event Handling propose.

If you implement the following methods and your Bean Class extends the ApplicationListner interface, then when these methods are executed, your Bean will get related Events.

void refresh()
void close();
void shutdown();


来源:https://stackoverflow.com/questions/30861709/configurableapplicationcontext-vs-applicationcontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!