Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
The WebApplicationContext
is an extension of the plain ApplicationContext
in Spring, tailored specifically for web applications. It offers a structured way to manage bean definitions and integrates smoothly with Spring MVC's web features.
Let's discuss some key aspects of WebApplicationContext
:
In a typical Spring web application, there's a root application context and one or more child contexts. The root context is often responsible for core services, while a child context, specifically the WebApplicationContext
, handles web-specific components like controllers, view resolvers, and handler mappings.
WebApplicationContext
is bound to the lifecycle of a ServletContext
, which means it exists throughout the duration of a web application. Beans defined within this context can also have a special session
or request
scope, in addition to the standard singleton and prototype scopes.
For applications deployed as a WAR in a servlet container, the WebApplicationContext
is usually bootstrapped by the ContextLoaderListener
. This listener initializes the root application context and places it in the ServletContext
.
Then, for Spring MVC components, a DispatcherServlet
is defined, which initializes its own WebApplicationContext
. This child context can access beans in the root context but not vice versa.
You can configure the WebApplicationContext
using XML, Java configuration, or even Groovy scripts. With Java-based configuration gaining popularity, the @Configuration
classes play a significant role in defining beans and their dependencies.
web.xml:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
In the above configuration, the ContextLoaderListener
will load the root-context.xml
(which contains beans like services, repositories, data sources, etc.), and the DispatcherServlet
will load the servlet-context.xml
(which has beans like controllers, view resolvers, etc.).
The WebApplicationContext
is an essential component of Spring's web module, allowing developers to seamlessly integrate Spring's IoC container with web environments, offering robust configuration and component management features for web applications.
Configuring WebApplicationContext in Spring MVC:
In Spring MVC, the WebApplicationContext
is configured in the web.xml
file or through Java configuration.
<!-- web.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
This configuration loads the WebApplicationContext
from the specified XML file.
Accessing WebApplicationContext in controller:
@Controller public class MyController { @Autowired private WebApplicationContext webApplicationContext; // Controller methods }
The WebApplicationContext
can be injected into a controller using the @Autowired
annotation.
Scoped beans in WebApplicationContext in Spring:
@Service @Scope(WebApplicationContext.SCOPE_REQUEST) public class MyRequestScopedService { // Service logic }
Scoped beans, such as request-scoped beans, can be defined in the WebApplicationContext
using annotations like @Scope
.
WebApplicationContext hierarchy in Spring MVC:
The WebApplicationContext
can be organized into a hierarchy to share common configurations. A parent context may be created for shared beans, and child contexts for specific servlets.
<!-- parent-context.xml --> <beans> <!-- Shared beans --> </beans>
<!-- child-context.xml --> <beans> <!-- Servlet-specific beans --> </beans>
Servlet-specific context declaration:
<!-- web.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/parent-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/child-context.xml</param-value> </init-param> </servlet>
Injecting WebApplicationContext in service:
@Service public class MyService { @Autowired private WebApplicationContext webApplicationContext; // Service logic }
The WebApplicationContext
can also be injected into service classes.
Refreshing WebApplicationContext in Spring:
The WebApplicationContext
can be refreshed programmatically to reload configurations.
((ConfigurableApplicationContext) webApplicationContext).refresh();
This can be useful when dynamic changes to the configuration need to take effect.
ServletContext vs WebApplicationContext in Spring MVC:
ServletContext
: Represents the entire web application. It is available to servlets through the ServletConfig
object and can be used to store global attributes.WebApplicationContext
: Represents the Spring application context for a specific servlet. It is a child context of the global ApplicationContext
.Access ServletContext
in a servlet:
@Override public void init(ServletConfig config) throws ServletException { ServletContext servletContext = config.getServletContext(); }
Access WebApplicationContext
in a Spring bean:
@Autowired private WebApplicationContext webApplicationContext;
Both play essential roles in a Spring MVC web application, with WebApplicationContext
providing a more flexible and feature-rich environment for managing beans and configurations specific to the servlet.