Saturday, August 2, 2008

Spring Framework - First Steps

This post teaches you some useful basic information and function of Spring framework. Writing XML configuration files, wiring beans, loading XMLs, using ApplicationContext. Creating, publishing and listening events are discussed in this post too.

TaskbarNotifier example application
The functions above are represented through an example application. This application has a GUI frame, where we can create animals, given the genus, the name and the color:


and when user click to the Create and notify button, then the created animal properties are showed in a tooltip message on taskbar:


Step 1: Creating the project
Create a new java application project with your favorite IDE and add the following files from Spring to the application classpath:
  • spring.jar
  • spring-core.jar
  • spring-context.jar
  • commons-logging.jar
  • log4j-1.2.xy.jar
Step 2: Creating the model
Now we create the model, the objects, on our application operates. We have a simple JavaBean, named Animal with three simple properties:
  • genus - genus of the animal
  • name - name of the animal
  • color - color of the animal
Our Animal JavaBean:


Step 3: Creating the service interfaces
Services are very important in Spring. They implement the business logic, operate with the application data. To create a service, first we create an interface with methods, that the service will implement (it's function will be discussed in another post).

Our example application has two service interfaces:
  • AnimalService - Creates Animal instance from its properties.
  • TaskbarService - Shows the taskbar icon, displays tooltip message on taskbar and closes the taskbar icon.
Our AnimalService interface:

Our TaskbarService interface:

Step 4: Implementing the service interfaces
Implementing the service interface is a simple interface implementation, its complexity depends on the complexity of the business logic.
Our application has two service interfsce implementations:
  • AnimalServiceImp
  • TaskbarServiceImp

AnimalServiceImp source code:


TaskbarServiceImp source code:

Step 5: Creating and publishing events
Events can be very useful when we want to inform other parts of our application, something interesting happened.

To create an event, we have to extend the org.springframework.context.ApplicationEvent abstract class and create a constructor, what call a super() with a parameter. This parameter is usually the object, what is behind to the published event.

If we have an event instance then we can publish it through an ApplicationContext instance, using the publishEvent() method, like this (where context is an ApplicationContext instance):
context.publishEvent(new TaskbarNotificationEvent(animal)); //This code segment is from AnimalServiceImp class

Working of events is represented by the following chart:


Our application have only one publishable event:
  • TaskbarNotificationEvent - This event is published when a new Animal instance created.
TaskbarNotificationEvent Source code:


Step 6 : Creating the Listener class
As I discussed above, ApplicationListeners are waiting for published events and they (of course we, programmers) decide, is the actual event important to this listener or not. If it is then the listener can run some event-specific code, otherwise it should do nothing (but can, if it wants).

To create an own listener, we need implement the org.springframework.context.ApplicationListener interface. This interface has a single method:
public void onApplicationEvent(ApplicationEvent event);


Through the event parameter we can access the actual published event. With the instanceof operator we can decide the type of the event.

The example application has only one Listener:
  • TaskbarNotifier.
TaskbarNotifier Source Code:


Step 7: Creating GUI
There's a simple thing, that I discuss in this step:
I write the windowClosing event to my Frame:
private void formWindowClosing(java.awt.event.WindowEvent evt) {
/**
* Close the application, using the AppController.exit() method.
* The defaultCloseOperation of form is set to EXIT_ON_CLOSE.
*/
AppController.exit();
}


I did it because i want to run some code before the application is closed (remove the taskbar icon for example).

Our application has only one frame:
  • MainFrame.
MainFrame Source Code:


Step 8: The XML configuration file
As I said, we can use XML configuration files to define beans, wire beans, set configurations. In a separated post I will write only about the XML configuration file, now see only the required information to create our sample application.

Our sample application's XML configuration file looks like this:



XML Scheme
To use the XML file with Spring framework, we have to start our XML file like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
These lines mean our XML file version is 1.0 and the encoding of the file is UTF-8. It's recommended keep these settings.

The next two lines is a DTD import. It can be used to check the validation of our XML file.

Root element
All XML file have to be a root element. If we create XML configuration file to Spring framework then the root element must be:
<beans>
...
</beans>
All bean definition, configuration code and everything have to be between the beans open and close tag.

Simple bean definition
The most easiest way to define a bean is the following:
<bean id="animalService" class="example.animal.AnimalServiceImp" />
This definition means:
  • create an instance of class example.animal.AnimalServiceImp
  • assign the animalService identification to the created instance
  • if we refering the created instance through the animalService identification, we always access to the same object instance (singleton) it can be changed, it will be discussed later
Wiring beans, dependency injection
We define a bean to access an example.taskbar.TaskbarServiceImp instance:
<bean id="taskbarService" class="example.taskbar.TaskbarServiceImp" />

And now, we define an other bean, what is a little more complex, because we set the taskbarService properties of the created bean:
<bean id="taskbarNotifier" class="example.taskbar.TaskbarNotifier">
<property name="taskbarService">
<ref bean="taskbarService" />
</property>
</bean>

This definition means:
  • create an instance of class example.taskbar.TaskbarNotifier
  • assign the taskbarNotifier identification to the created instance
  • if we refering the created instance through the taskbarNotifier identification, we always access to the same object instance (singleton) it can be changed, it will be discussed later
  • get the example.taskbar.TaskbarServiceImp instance with taskbarService identification and set the created bean's taskbarService property through its setter method to this taskbarService object
This way to creating and connecting beans can be little strange, but it is very very useful, there are so many thing, that we can't make without this solution.

Summary
If your read this post, you can
  • add the required files to use Spring framework
  • create service interfaces
  • implement service interfaces
  • create and publish events
  • listen and handle events
Using XML configuration files can be strange first, but I will create a post only about it. Understand using these files is very important to understand Spring framework itself.

Downloads
You can download example code here as NetBeans project.

No comments: