Java Reference
In-Depth Information
2.
Add the required Spring web-related dependencies to your POM file.
3.
Create
web.xml
in a new directory
<project_home>/src/main/webapp/WEB-INF
with
ContextLoaderListener
to bootstrap Spring.
4.
Configure
AutomaticJobRegistrar
to register the jobs with the JobRegistry.
You start by updating the POM file. In order for Spring to be bootstrapped in a servlet container like
Tomcat, you need to add the Spring framework's web dependencies. Listing 6-14 shows how to
configure the additional dependencies required for this job including Spring's web module, SLF4J, and
log4j.
Listing 6-14.
Additional Dependencies for
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
Instead of creating a jar file for you to execute independently, you want to create a war file that you
can deploy to Tomcat. To do this, you need to update the packaging in your POM file from jar to war.
You also replace the reference to
maven-jar-plugin
with
maven-war-plugin
. Listing 6-15 shows how to
configure the POM file so your application is packaged correctly.
Listing 6-15.
Revised
pom.xml
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<attachClasses>true</attachClasses>
<warName>deleteFiles</warName>
</configuration>
</plugin>
As Listing 6-15 shows, configuring Maven to generate your war file is very easy. Unfortunately, the
changes to the
pom.xml
file won't work until you create a
web.xml
file and put it in the correct place. For
web.xml
, you configure a single listener, Spring's
org.springframework.web.context.ContextLoaderListener
. It bootstraps Spring as well as the
JobRegistry for you. Listing 6-16 shows the
web.xml
file.