资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

获取Spring的ApplicationContext的方式有哪些

本篇内容介绍了“获取Spring的ApplicationContext的方式有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联秉承实现全网价值营销的理念,以专业定制企业官网,成都做网站、成都网站制作,微信平台小程序开发,网页设计制作,手机网站开发,成都营销网站建设帮助传统企业实现“互联网+”转型升级专业定制企业官网,公司注重人才、技术和管理,汇聚了一批优秀的互联网技术人才,对客户都以感恩的心态奉献自己的专业和所长。

Application Context定义

简单来说就是Spring中的高级容器,可以获取容器中的各种bean组件,注册监听事件,加载资源文件等功能。

Understanding Application Context

The ApplicationContext is the central interface within a Spring application for providing configuration information to the application. It is read-only at run time, but can be reloaded if necessary and supported by the application. A number of classes implement the ApplicationContext interface, allowing for a variety of configuration options and types of applications.

The ApplicationContext provides:

  • Bean factory methods for accessing application components.

  • The ability to load file resources in a generic fashion.

  • The ability to publish events to registered listeners.

  • The ability to resolve messages to support internationalization.

  • Inheritance from a parent context.

Application Context获取的几种方式

1、直接注入

@Resource

private ApplicationContext ctx;

2、实现ApplicationContextAware接口(推荐)

获取Spring的ApplicationContext的方式有哪些

WebApplicationObjectSupport extends ApplicationObjectSupport implements ServletContextAware

从上面的继承关系看,获取Application Context还可以继承WebApplicationObjectSupport 、ApplicationObjectSupport,继承加强耦合性不推荐。

3、WebApplicationContextUtils工具类

ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());

4、从当前线程绑定获取(Spring boot不支持)

ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();

“获取Spring的ApplicationContext的方式有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


名称栏目:获取Spring的ApplicationContext的方式有哪些
本文地址:http://cdkjz.cn/article/ppihgd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220