ConfigurableApplicationContext与ApplicationContext [英] ConfigurableApplicationContext vs ApplicationContext

查看:418
本文介绍了ConfigurableApplicationContext与ApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习Spring框架已有一段时间了,但是每当我寻找初始化和销毁​​回调时,每个示例都使用ConfigurableApplicationContext而不是ApplicationContext.有人可以解释为什么吗?

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?

推荐答案

因为ApplicationContext的所有常见实现实际上都是ConfigurableApplicationContext.从javadoc中提取ConfigurableApplicationContext:

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

大多数(如果不是全部)应用程序上下文都将实现SPI接口.

配置和生命周期方法封装在此处,以避免对ApplicationContext客户端代码显而易见.本方法仅应由启动和关闭代码使用

这意味着,当您使用一个应用程序上下文时,即从中获取bean,您只能将其用作ApplicationContext,但是在管理其生命周期(初始化和销毁​​)时,可以使用ConfigurableApplicationContext

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

这篇关于ConfigurableApplicationContext与ApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆