Spring Boot 中的 ApplicationContext

Ste*_*ng 0 spring-mvc spring-boot

我有一个 Spring Boot 应用程序,它与 Spring Data、MySQL、Spring Security 和 MVC 一起运行。该应用程序对我来说运行得很好。

然而,我不断听到ApplicationContext很多消息,我想知道我什么时候需要使用它,并且想知道它的作用。有人可以给我一个例子并概述ApplicationContext其用途吗?

Sac*_*lla 5

ApplicationContext是 Spring 框架构建的核心接口。如果您正在构建 Spring 应用程序,那么您已经在使用ApplicationContext. 您可以通过Spring 框架参考文档对此有深入的了解。根据本文档,Spring 框架由以下模块组成;

Spring框架概述

Context ( ) 模块构建在Core 和 Beans模块提供的坚实基础上:它是一种以类似于 JNDI 注册表的框架样式方式访问对象的方法。Context 模块继承了 Beans 模块的功能,并添加了对国际化(例如使用资源包)、事件传播、资源加载以及通过 Servlet 容器透明创建上下文的支持。Context 模块还支持 Java EE 功能,例如 EJB、JMX 和基本远程处理。ApplicationContext 接口是Context模块的焦点。 支持将常见的第三方库集成到 Spring 应用程序上下文中,特别是缓存(EhCache、JCache)和调度(CommonJ、Quartz)。spring-contextspring-context-support

SpringApplicationContext也继承了BeanFactorysuper接口。因此,从技术上讲ApplicationContext,它能够完成所有的事情,BeanFactory界面也有能力,等等​​。BeanFactory接口以及ApplicationContext提供 Spring IoC 容器(核心容器)的骨干。这是Bean您的应用程序的管理。

该接口org.springframework.context.ApplicationContext 代表 Spring IoC 容器,负责实例化、配置和组装上述 bean。容器通过读取配置元数据来获取要实例化、配置和组装哪些对象的指令。配置元数据以 XML、Java 注释或 Java 代码表示。它允许您表达组成应用程序的对象以及这些对象之间丰富的相互依赖性。

ApplicationContext使用急切加载机制。因此,应用程序中声明的每个 bean 在应用程序启动后立即初始化,此后该ApplicationContext作用域几乎是只读的。

使用自定义 bean 定义启动 Spring IoC 容器几乎是直截了当的。

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"daos.xml"});
Run Code Online (Sandbox Code Playgroud)

以下文件显示了该daos.xml文件的内容;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="accountDao"
        class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>
    <!-- more bean definitions for data access objects go here -->
</beans>
Run Code Online (Sandbox Code Playgroud)

之后,您可以像.xml这样访问定义的bean;

JpaItemDao obj = (JpaItemDao) factory.getBean("itemDao");
Run Code Online (Sandbox Code Playgroud)

这些实例现在正在由ApplicationContext. 但大多数用户更喜欢使用@Bean注释定义 bean 来进行绑定,并使用@Autowired注释来进行依赖项注入。因此,无需手动将 bean 提供.xml给自定义初始化ApplicationContext

@Configuration
class SampleConfig {

    @Bean
    public JpaItemDao getJpaItemDao() {
        return new JpaItemDao();
    }
}
Run Code Online (Sandbox Code Playgroud)

并注入;

@Component
class SampleComponent {

    @Autowired
    private JpaItemDao itemDao;

    public void doSomething() {
        itemDao.save(); // Just an example.
    }
}
Run Code Online (Sandbox Code Playgroud)

除了 bean 管理之外,ApplicationContextSpring 核心容器中还做了一些其他重要的事情。根据ApplicationContect javadoc,它们是;

  • 用于访问应用程序组件的 Bean 工厂方法。继承自ListableBeanFactory。
  • 以通用方式加载文件资源的能力。继承自ResourceLoader接口。
  • 能够向注册的侦听器发布事件。继承自ApplicationEventPublisher接口。
  • 解析消息的能力,支持国际化。继承自MessageSource接口。
  • 从父上下文继承。后代上下文中的定义将始终优先。例如,这意味着整个 Web 应用程序可以使用单个父上下文,而每个 servlet 都有自己的子上下文,该子上下文独立于任何其他 servlet。

ApplicationContext另外,请检查专门设计用于不同用例(例如WebApplicationContext )的子接口。