ApplicationContext种类

5. Types of ApplicationContext

Spring provides different types of ApplicationContext containers suitable for different requirements. These are implementations of the ApplicationContext interface. So let's take a look at some of the common types of ApplicationContext.

5.1. AnnotationConfigApplicationContext

AnnotationConfigApplicationContext class was introduced in Spring 3.0.

It can take classes annotated with @Configuration, @Component, and JSR-330 metadata as input.

dart 复制代码
ApplicationContext context = new AnnotationConfigApplicationContext(AccountConfig.class);
AccountService accountService = context.getBean(AccountService.class);

5.2. AnnotationConfigWebApplicationContext

AnnotationConfigWebApplicationContext is a web-based variant of AnnotationConfigApplicationContext.

We may use this class when we configure Spring's ContextLoaderListener servlet listener or a Spring MVC DispatcherServlet in a web.xml file.

Moreover, from Spring 3.0 onward, we can also configure this application context container programmatically. All we need to do is implement the WebApplicationInitializer interface:

dart 复制代码
public class MyWebApplicationInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(AccountConfig.class);
    context.setServletContext(container);

    // servlet configuration
  }
}

5.3. XmlWebApplicationContext

If we use the XML based configuration in a web application, we can use the XmlWebApplicationContext class.

As a matter of fact, configuring this container is like the AnnotationConfigWebApplicationContext class only, which means we can configure it in web.xml, or implement the WebApplicationInitializer interface:

dart 复制代码
public class MyXmlWebApplicationInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocation("/WEB-INF/spring/applicationContext.xml");
    context.setServletContext(container);

    // Servlet configuration
  }
}

5.4. FileSystemXMLApplicationContext

We use the FileSystemXMLApplicationContext class to load an XML-based Spring configuration file from the file system or from URLs. This class is useful when we need to load the ApplicationContext programmatically. In general, test harnesses and standalone applications are some of the possible use cases for this.

For example, let's see how we can create this Spring container and load the beans for our XML-based configuration:

dart 复制代码
String path = "C:/myProject/src/main/resources/applicationcontext/account-bean-config.xml";

ApplicationContext context = new FileSystemXmlApplicationContext(path);
AccountService accountService = context.getBean("accountService", AccountService.class);

5.5. ClassPathXmlApplicationContext

In case we want to load an XML configuration file from the classpath, we can use the ClassPathXmlApplicationContext class. Similar to FileSystemXMLApplicationContext, it's useful for test harnesses, as well as application contexts embedded within JARs.

So let's see an example of using this class:

dart 复制代码
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/account-bean-config.xml");
AccountService accountService = context.getBean("accountService", AccountService.class);

参考:
The Spring ApplicationContext

相关推荐
知其然亦知其所以然几秒前
RAG 结果太水?用 RRF + Reranker 重排,效果翻倍提升!
java·后端·llm
SimonKing2 分钟前
吊打面试官系列:Spring为什么不推荐使用字段依赖注入?
java·后端·架构
魔镜魔镜_谁是世界上最漂亮的小仙女9 分钟前
java-集合
java·后端·程序员
真实的菜10 分钟前
消息队列高级特性与原理:解锁分布式系统的底层逻辑
java
若水不如远方12 分钟前
java范型
java
凌辰揽月14 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
lifallen20 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
长安不见22 分钟前
背景知识: 理解LimitLatch背后的AQS
java
小吕学编程24 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
weixin_4383354030 分钟前
Spring Boot实现接口时间戳鉴权
java·spring boot·后端