Install and configure spring framework


- JavaTouch   - Spring Framework

Install and configure spring framework

Introduction

The Spring framework and container is packaged in several JAR files.
Spring is a library of classes that will be packaged with and used by your Spring-enabled applications.
Installing Spring involves adding one or more JAR files to your application’s classpath.
It does not have an executable runtime. Therefore, Spring is more akin to a library like Jakarta Commons than an application server like JBoss.

How you make Spring available to your application’s classpath will vary depending on how you build and
run your application. You may choose to add the Spring JAR files to your system’s classpath or to a project
classpath in your favorite IDE. If you’re building your application using Ant or Maven, then you’ll need to be certain to include Spring in your build’s dependencies so that it will be included in the project’s target deployment.
Regardless of how you package and run your application, you’ll probably want access to Spring’s documentation, JavaDocs, and examples. Therefore, the first thing you’ll want to do is to download the full Spring distribution.

Downloading Spring

You can download the Spring distribution from Spring’s website: http://www.springframework.org. Choose
the “downloads” link from the left-hand menu and look for the Spring 2.0 download (Make sure that you download the current production release).
When downloading Spring, you have two choices: You can download a Spring distribution that comes
with its own dependencies or you can download a distribution that contains only the Spring JAR files. Even
though it’s a much larger download, its better the one that comes with dependencies so that you won’t have to
hunt down the other JAR files that our application needs.
 

Exploring the Spring distribution

Once you’ve downloaded the distribution, unzip it to a directory on your local machine. The Spring distribution is
organized within the following directory structure:
aspectj/ — Contains source files for Spring-specific aspects. If you’re curious about how the Spring framework      makes use of AspectJ you may look here.
dist/ — Contains the Spring distribution JAR files. This directory contains several JAR files, each of
which represents a part of the Spring framework.
docs/ — Contains the Spring API and reference documentation.
lib/ — Contains the JAR files that Spring depends on. There are many JAR files under this directory.
Fear not: Spring doesn’t depend on all of them at once. Many of these dependencies only come into
play when you exercise a specific set of Spring functionality.
mock/ — Contains the source code for Spring’s mock object and unit-testing module.
samples/ — Contains several sample Spring applications.
src/ — Contains the source code for most of the Spring framework.
test/ — Contains source code for the unit-tests that are used to test Spring itself.
tiger/ — Contains source code for Spring that is specific to Java 5. This code is kept separate from the

The “docs/” directory contains two important pieces of documentation. The reference document is an
overview of the entire Spring framework and is a good compliment to this book. Also, the JavaDocs for the
entire Spring framework can be found under “docs/”—you’ll probably want to add this as a bookmark in your
web-browser, because you’ll refer to it often.
The “samples/” directory contains a handful of sample Spring applications. Of particular note are the
“petclinic” and “jpetstore” examples. Both of these applications highlight many important elements of the
Spring framework.


Building your classpath

Directories and jar files of interests

/dist

The most important directory in the Spring distribution is probably the “dist” directory. Using Spring in your application involves choosing one or more JAR files from this directory and making them available in your
application’s classpath.

Although the the large number of JAR files may seem a bit daunting, sorting them out isn’t all that difficult. The JAR files in the “dist/modules/” directory allow you to pick and choose which parts of Spring are needed for your application.To make thing simple, however, you may want to take the lazy way out and simply add spring.jar (from the “dist/” directory) to your application’s classpath. The spring.jar file is a convenient, single-JAR equivalent to all of the JARs in the “dist/modules/” directory.
The JAR files in the “dist/extmodules/” directory represent modules that are considered “external” to the Spring framework.
Here is where you’ll find Spring support for various object-relational mapping (ORM)
solutions such as Hibernate and TopLink. You’ll also find JAR files for Spring’s portlet MVC framework as
well as a set of classes that help you test your Spring applications. The JAR files in this directory are only
necessary if your application will be making use of the external modules that they support.

/lib

If “dist/” is the most important directory, then “lib/” comes in a close second. You will likely need to add other JAR files to your classpath to enable certain Spring features. For example, if you intend to use Spring’s support for AspectJ in your application, you’ll need to add aspectjr.jar and aspectjweaver.jar to the classpath. Likewise, Spring’s Hibernate modules depend on an appropriate version of Hibernate being available in the classpath. If you downloaded the “with dependencies” distribution of Spring, you’ll be able to find most any dependency you need in the “lib” directory.
Although many of the JAR files under the “lib” directory are optional, one is required. Spring relies on Jakarta Commons Logging to log informational and error messages. Therefore, commons-logging.jar (from “lib/jakarta-commons”) must always be in the classpath of any Spring-enabled application.
Once you’ve decided which JAR files your application needs, the next step is to add them to your classpath.

 

@ Add Jars to your CLASSPATH system variable

The naïve way of handling this would be to add them to your CLASSPATH system variable:

 The naïve way of handling this would be to add them to your CLASSPATH system variable:
1.1 set CLASSPATH=%CLASSPATH%;c:/spring/dist/spring.jar
1.2 set CLASSPATH=%CLASSPATH%;c:/spring/lib/Jakarta-commons/commonslogging.jar

 But you’re probably going to use Spring to build fullblown applications more often than one-off trials. You’re likely going to let a build system like Maven or Ant construct your application distribution.


@ Adding Spring as a Maven 2 dependency

If you’re using Maven 2 to build and package your Spring-based application, then you can let Maven download the Spring JAR files for you. With Maven 2, adding Spring to your build is a simple matter of adding dependency entries to your project’s pom.xml file.
Spring and its modules are available in the Maven repository under the group ID “org.springframework”.
Within this group are the modules listed in the following Table.

Maven Artifact ID  What it provides
spring Almost all of the Spring framework.
spring-aop The Spring AOP framework
spring-aspects AspectJ aspect library including the Spring configured and AspectJ transactional aspects.
spring-beans The Spring bean factory and related classes.
spring-context The Spring application context and related classes.
spring-core The core Spring framework.
spring-dao Spring’s DAO abstraction framework.
spring-hibernate2 Spring’s support for Hibernate 2.
spring-hibernate3 Spring’s support for Hibernate 3.
spring-ibatis Spring’s support for iBATIS.
spring-jca Spring’s support for the Java Connector API.
spring-jdbc Spring’s JDBC abstraction framework.
spring-jdo Spring’s support for Java Data Objects.
spring-jms Spring’s support for the Java Messaging API.
spring-jmx Spring’s support for Java management extensions.
spring-jpa Spring’s support for the Java Persistence API.
spring-mock Spring’s unit-testing and mock-object extensions.
spring-ojb Spring’s support for Apache’s Object Relational Bridge.
spring-portlet Spring’s Portlet MVC framework.
spring-remoting Spring’s remoting support.
spring-struts Spring-Struts integration.
spring-support Support and utility classes
spring-toplink Spring’s support for Oracle TopLink.
spring-web Spring’s web container and related utility classes.
spring-webmvc Spring’s web MVC framework.

 

Although your application may start small and have only a small dependency on Spring, most applications grow to depend on the bulk of the Spring framework. Therefore, it is often easier to declare a dependency on the entirety of Spring:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring</artifactId>
  <version>2.0</version>
</dependency>

This dependency will make the bulk of the Spring framework available to your application. Even if you’re not using it all right away, there’s little harm (and plenty of convenience) is using this dependency.

You may also choose to add each module as a dependency as it’s needed. For example, if your application depends only on the Spring AOP module, you might add the following <dependency> to your pom.xml
file:

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-aop</artifactId>
 <version>2.0</version>
</dependency>

This dependency declaration will make Spring AOP available for building your application. In addition, the Spring Beans module will also be made available thanks to Maven 2’s support for transitive dependency resolution.

If your application will be using a third-party ORM solution or you will be building a portlet MVC application, you may need to add additional dependencies to pom.xml. For example, if your application is using Hibernate 3 for persistence, you’ll want to add the Spring Hibernate 3 module as follows:

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-hibernate3</artifactId>
 <version>2.0</version>
</dependency>


WHAT ABOUT MAVEN 1?
Perhaps you’re building your application with the original Maven. For the most part, dependency declaration isn’t much different between Maven 1 and Maven 2. How Maven resolves those dependencies is slightly different, however. Maven 2 supports transitive dependency resolution, which means that if your application depends on Spring, then Maven 2 is smart enough to know that it also depends on Jakarta Commons Logging without having to be told. With Maven 1, you may have to explicitly add Commons Logging as a dependency.
Maven 2 is far superior to Maven 1 in many ways, including transitive dependency resolution.

 

@ Using Spring with Ant

Although we prefer to use Maven 2 for our builds, many developers will want to use Apache Ant. If you’re using Ant to build your Spring project, you’ll need to add the Spring JAR files and their dependencies to the appropriate paths in your project’s build.xml file.

We recommend declaring an Ant <path> element that will contain all of your application’s dependencies, including the Spring JAR files. Listing 1.1 shows a small section of an Ant build file that manages Spring dependencies in this way.
Listing 1.1 Building a Spring application with Ant:

<project name="training" default="init">
  <property name="spring.home"
    location="/opt/spring-framework-2.0"/>   | #1
  <property name="target.dir" location="target"/>
  <property name="classes.dir" location="${target.dir}/classes"/>
  <property name="src.dir" location="src"/>
  <property name="java.src.dir" location="${src.dir}/java"/>
  <property name="webapp.dir" location="${src.dir}/webapp"/>
  <property name="app.lib.dir" location="lib"/>
  <property name="spring.lib.dir"  location="${spring.home}/dist"/>
  <property name="spring.depends.dir" location="${spring.home}/lib"/>

  <path id="dependency.path">
    <fileset dir="${spring.lib.dir}" includes="*.jar"/>                   | #2
    <fileset dir="${spring.depends.dir}" includes="**/*.jar"/>   | #2
    <fileset dir="${app.lib.dir}" includes="*.jar"/>
  </path>

  <target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac destdir="${classes.dir}"  classpathref="dependency.path">   | #3
        <src path="${java.src.dir}"/>
    </javac>
  </target>

  <target name="war" depends="compile">
    <war destfile="${target.dir}/${ant.project.name}.war"
            webxml="${webapp.dir}/web.xml">
        <lib dir="${spring.lib.dir}"/>      | #4
        <lib dir="${app.lib.dir}"/>
        <classes dir="${classes.dir}"/>
     </war>
   </target>
    ...
 </project>


#1 Define Spring distribution location.>
#2 Include Spring dependencies.>
#3 Set classpath for javac.>
#4 Include Spring dependencies.>

With the Ant build file now in place, there is one final thing you will want to do. When you first start using Spring, one feature you will almost certainly find useful is logging. The easiest way to set this up is to include a simple log4j configuration file. Assuming the project structure defined in the build.xml file in listing 1.1, you will need to create a file called log4j.properties in /src/webapp/WEB-INF/classes. The following snippet of log4j.properties configures log4j to log all of Spring’s logging messages to the console:

 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.stdout.layout.ConversionPattern=%d %p %c - %m%n
 log4j.rootLogger=INFO, stdoutlog4j.logger.org.springframework=DEBUG

Your project is now set up and ready to go. All you have to do is start coding, put Ant to work, and you will have a working Spring application in no time.