Spring, Apache Struts, JavaServer Faces, RIFE, Xerces, JAX-WS – these are just a few of the many Java frameworks in use around the world. If you haven't dealt with frameworks before, Java or otherwise, consider this article your introduction. You'll get to know what a framework is and what you can do with it, but we won't get so detailed as to bog you down with lots of code. All you need before we start is some Java experience and maybe XML knowledge.
A software framework offers an abstraction layer upon which you can build your software – a skeleton of generic functionality upon which you can modify, improve, and add whatever you feel is necessary. You might think we just gave you the definition of a library or an API, but a framework offers some features that set it apart:
Unlike libraries, with frameworks, control flow is dictated by the framework instead of the user. This inversion of control (IoC) is a central concept for Spring.
A framework always provides a default behavior.
Frameworks are extensible – that is, basically, a framework's primary role. However, the core framework code itself is not modifiable.
IoC is implemented through containers, which are components in which you can store other components, such as, for example, applets. If you've ever had to deal with Java web code, you know that containers are responsible for the management of the objects subordinated to them, their life cycles, creation, and even the glue that brings them together. Objects created by a container are called Java Beans, which is a term widely used in Enterprise Java. Said Beans can be offered by the container via dependency injection – by being called by name – or by dependency lookup, where the caller asks the container for a certain object.
A simple example, although not an easy one, is a compiler. A compiler is a framework because it respects all the four laws above. You don't get to control how the compiler works with data structures or how it generates the assembly code, but you can extend it to support more languages or dialects.
When speaking about software frameworks, we also speak of hot spots, which are parts that are changed by the developer, and frozen spots, which represent remain the same regardless of the usage pattern. Problems appear when the two types of components are out of balance. If there are too many frozen spots, the end result will be bloatware, and if there are too few, the developer has to do too much work, which obviates the whole point of a framework. Since there is no such thing as "one size fits all" when it comes to frameworks, choosing the right framework for your needs can be a challenging task, and one we're too cowardly to advise you on. Instead, let's get more practical and see how to install and use Spring.
If you've never used Spring, go to the Spring website to learn a bit about its layout from the available video tutorials and written docs.
On my Fedora 17 machine, Spring is available from the repositories, but CentOS users must install it from a tarball, which isn't as complicated as you might think. As long as you have Java EE installed, all you have to do is choose a directory where you want Spring to be downloaded and get it. You can choose either a non-installer archive, which you can just unpack and run, or if you want a point-and-click experience, choose the installer version. We chose the former, so we ran the following commands:
wget -c http://download.springsource.com/release/STS/3.0.0/dist/e4.2/spring-tool-suite-3.0.0.RELEASE-e4.2-linux-gtk-x86_64.tar.gz #adjust architecture as needed
tar xzvf spring-tool-suite-3.0.0.RELEASE-e4.2-linux-gtk-x86_64.tar.gz
./STS #this launches the Spring IDE
You will notice two things when you launch Spring: First, the IDE is a memory hog, and second, once it has (finally) started, the environment looks a lot like Eclipse. Both of these things are true because Spring's IDE is built on the Eclipse SDK, which also means that you can easily integrate Spring into Eclipse, if that's the IDE you use every day. Some well-written resources with lots of screenshots can help you with that task.
To get started, go to File -> New -> Spring Template Project from within the Spring IDE, download said template if it isn't already there, set the top-level package (in the project settings window) to something like com.springframework.* – feel free to change that if you like – and start coding.
What better example should we start with than a Hello World program? Let's see:
//put package and import statements here....
public class HelloSpringBean
private String message;
public void setMessage( String message )
this.message = message;
public String getMessage()
public static void main( String args )
ApplicationContext context = new FileSystemXmlApplicationContext( "applicationContext.xml" );
HelloSpringBean helloSpringBean = ( HelloSpringBean )context.getBean( "helloSpringBean" );
System.out.println( helloSpringBean.getMessage() );
As you can see, we need an XML configuration file to specify where you set up various properties and their values. Here's an example that fits the code above:
<?xml version="1.0" encoding="UTF-8"?>
<bean id="helloSpringBean" class="com.myclass.hellospring.HelloSpringBean" >
<property name="message" value="Hello, Spring" />
Once you build the .java file, after some messages, the operation will indeed yield the message "Hello, Spring."
Obviously we've just barely started exploring Spring. The framework requires lots of concepts be mastered, both theoretical and practical, and Spring has other facets you might be interested in, including Roo (a rapid application development subsystem), Security, Mobile, Data, and Batch. But if you find the general concept of a Java framework interesting and efficient, we hope you will like Spring just as much as we do.
Allowed tags: <a> link, <b> bold, <i> italics