In this article, I will show you how to load spring application context XML for a web application. I will be using Maven as the build tool. You can read my Maven Web Application Example for setup details.
Dependencies
Add the following dependencies to pom.xml
.
javax.servlet-ap
– Servlet APIspring-context
– Spring Contextspring-web
– Spring Web Application Contextlog4j
for logging
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javarticles.web</groupId> <artifactId>springWebappExample</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Spring Web Application Context Example</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <finalName>springWebappExample</finalName> </build> </project>
Configure application context in web.xml
In order to load the application context XML files you need to do the following in web.xml
.
- Add spring provided listener class
org.springframework.web.context.ContextLoaderListener
. - Specify the application context files in
<context-param>
.
web.xml:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Spring Web Application Context Example</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/applicationContext.xml, </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
You need to add param-name
with value contextConfigLocation
and param-value
containing the XML file names. If more than one XML, enter them space separated.
Sample Bean
SpringWebAppContextLoaderExample:
package com.javarticles.web; import org.apache.log4j.Logger; public class SpringWebAppContextLoaderExample { public String toString() { LOGGER.info("SpringWebAppContextLoaderExample is called"); return "This is Spring Webapplication Context Loader Example"; } private Logger LOGGER = Logger.getLogger(SpringWebAppContextLoaderExample.class); }
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> <context:annotation-config /> <bean id="springWebApp" class="com.javarticles.web.SpringWebAppContextLoaderExample" scope="singleton"/> </beans>
Let’s now modify index.jsp. It will load spring WebApplicationContext
using the below:
WebApplicationContextUtils.getWebApplicationContext(application)
application
is the jsp’s default variable to access the ServletContext
.
Next, we will get our sample bean and print it.
index.jsp:
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%> <html> <body> <h2><%=WebApplicationContextUtils.getWebApplicationContext(application).getBean("springWebApp")%></h2> </body> </html>
Run the application
Right click on the project. Click on ‘Run AS’->”Run on Server’.
Download the source code
This was an example about loading spring web application context. You can download the source code here: springWebAppContextExample.zip