This article shows how to setup and use the Spring Framework using Eclipse.
A small application is developed to illustrate some of the facilities offered by Spring. There are many options (security, web flow, batch, orm support...). Hopefully after reading this article, the reader should be confident enough to seek out and install any option that is needed.
This article uses Spring 3.0.5.
Spring and Dependency Injection
Spring is really about dependency injection. In a nutshell, this is the principle of using Spring:
- Applications avoid instantiating classes using the "new" keyword. Class instances and properties are managed by Spring.
- All classes, with the exception of very simple Java beans, have a corresponding interface.
- In the code, all references to other classes use the interfaces, not the classes. Spring knows about which classes go where, the Java code does not. The Java code only knows the interfaces.
Spring helps develop applications which are de-coupled:
- A file parser can be written in two flavours: random access file, and memory based. All the code that needs to parse data need not be aware of which implementation it is using. This encourages modular design.
- Theoretically, it is easier and quicker to write Unit Tests, individual classes can be tested without having to instantiate a complete tree of classes, mock classes can replace those we do not want in the unit test.
Modules of Spring
Spring is composed of several modules,which are briefly covered below:
- Core Container is needed by even the simplest Spring application
- Data Access/Integration provides a Spring view of how to interact with databases using JDBC, Hibernate ....
- Web provides its own MVC framework, and also allows existing frameworks (Struts) to be used alongside Spring.
- AOP allows aspect oriented programming principles to be used. This can be used to change the behaviour of existing classes, by injecting code to run before or after methods are invoked.
- Instrumentation provides functionality for certain application servers.
- Test allows Spring components to be tested with JUnit or TestNG, providing loading of Spring Application Contexts (the runtime environment provided by Spring), and mock objects.
Additional modules are available, such as Spring Security, Batch, HIbernate, JSF support.... These can be downloaded separately.
The web site for the Spring framework is http://www.springsource.org It is usually easier to find where the download of Spring is from a search engine, as the Spring site's home page does not always provide easy navigation to the download area. At the time of writing, the following link navigates to the download area: http://www.springsource.org/download.
The latest GA release with docs is downloaded, as a zip archive, and extracted on the local machine. The archive downloaded contains the jar files that implement the modules described in the previous section (code, data access, web, aop, instrumentation and test). In the docs section, a Spring API reference is supplied along with a detailed document on Spring, available as a PDF and as HTML.
In addition to the above download, it is also necessary to download a dependency Spring has on commons logging, available from the Apache Software Foundation. The commons logging package is downloaded from http://commons.apache.org/logging/download_logging.cgi. Of interest is the jar file called commons-logging-1.1.1.jar
Springsource Tool Suite
There is an eclipse distribution available that provides extra support for Spring. To keep this document short and focused on how to configure and use Spring, the SpringsourceTool Suite is not used.
The Eclipse IDE (www.eclipse.org) is available in a variety of distributions. The distribution used here is the Java EE distribution. It contains functionality to work with databases and web servers. The plain Java Development version is good enough for this article though.
In the next section, a console application is developed.