ApplicationContext and WebApplicationContext
`ApplicationContext`
The `Application Context` is the central interface to the Spring container. It is used to manage beans in a Spring application. It provides configuration for the beans, handles dependency injection, and supports various functionalities like event propagation, declarative mechanisms, and internationalization.
- Scope: It is used in all types of Spring applications, not just web applications. It can be a standalone application or a web application.
- Features: Includes support for bean lifecycle management, message source, and more.
`WebApplication Context`:
- The `WebApplication Context` is a specialized extension of `Application Context` designed for web applications. It provides additional features specific to web applications, such as integration with the servlet context and web-specific beans like filters and listeners.
- Scope: It is used specifically for web applications and is managed by the `DispatcherServlet` in Spring MVC.
- Features: Includes support for web-specific components, such as request and session scopes, and the ability to integrate with the servlet API.
Table of Contents
ApplicationContext Example
Non-Web Application Example
AppConfig.java
```java
import org.springframework.context.Application Context;
import org.springframework.context.annotation.AnnotationConfigApplication Context;
public class AppConfig {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplication Context(AppConfiguration.class);
MyService myService = context.getBean(MyService.class);
myService.doSomething();
}
}
```
AppConfiguration.java
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfiguration {
@Bean
public MyService myService() {
return new MyService();
}
}
```
MyService.java
```java
public class MyService {
public void doSomething() {
System.out.println("Service is doing something...");
}
}
```
In this example:
Application Context
is used to manage beans in a standalone application. TheAppConfig
class initializes the context and retrieves a bean of typeMyService
.
WebApplication Context Example
Web Application Example
WebAppConfig.java
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
public class WebAppConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
```
WebApplicationInitializer.java
```java
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{WebAppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
```
UserController.java
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/info")
public String getUserInfo() {
return "User Info";
}
}
```
In this example:
WebApplication Context
is used in a web application.WebApplicationInitializer
sets up theDispatcherServlet
and configures theWebAppConfig
class to handle web-specific beans and settings.- The
UserController
handles HTTP requests, andWebAppConfig
configures the view resolver.
Summary of Differences
- `Application Context`: Used in both web and non-web applications. It provides general support for managing beans and application contexts.
- `WebApplication Context`: A specialized version of `ApplicationContext` for web applications. It provides additional functionality for handling web components and integrates with the servlet context.