Introducing the Spring Framework

In this discussion, I will be focusing on the Spring Framework. The first section will be about the whys and wherefores of the Spring Framework. In the second and third sections, I will explain how to use the Spring Framework to implement business logic. In the fourth and fifth sections, I will develop an application that uses the Spring Framework.

In an enterprise application, business logic forms the basis of transforming the persisted data. That is the reason why the business logic layer is considered the most important layer of any enterprise application. Typically, the Java Enterprise Edition (JEE) component that was used to implement business logic was Enterprise Java Beans (EJB). However, EJBs are heavy weight components that require application servers to run.

This was the scenario before the Spring Framework came into the picture. The Spring Framework provides a lightweight container to run the objects implementing business logic. In other words, Spring Framework-based business objects do not require an application server to run.

Spring Framework: What is it?

By definition the Spring Framework is “An open-source layered Java/J2EE application framework having a light-weight container implementing Inversion-of-Control and Aspect Oriented Programming.” The key points here are “layered application framework” and “Inversion of Control and Aspect Oriented Programming.” These key points can be divided into two broad categories:

  1. Patterns

  2. Components of the Framework

The former refers to the design patterns supported by Spring Framework and the latter refers to the components that build up the Framework. 

Spring has two patterns at its core. They are:

    • Inversion-of-Control

    • Aspect Oriented Programming

The former is also called IoC and the latter is known as AOP. Here are the details.

Inversion-of-Control is called IoC for short. IoC “is a concept, and an associated set of programming techniques, in which the control flow is inverted compared to the traditional interaction model expressed in imperative style by a series of procedure calls.” In other words, the framework calls the procedures created by the programmer, rather than programmer calling the procedures or methods of the framework. IoC is also known as the “Hollywood Principle.” The Hollywood Principle says “Don’t call me, I will call you.”

IoC is used to ‘inject’ dependencies at runtime. When one uses this pattern, the dependencies of a particular object are not satisfied at compile time. Instead, the framework provides the required dependencies to the object at runtime. Thus, this pattern releases the programmer from the chore of providing all the dependencies at compile time.

Aspect Oriented Programming or AOP for short, is by definition “An approach to programming that attempts the separation of concerns, specifically cross-cutting concerns, as an advance in modularization.” Here, the key point is separation of concerns. Concern is another term for a module of an application, where module refers to a functionality of the application.

Separation of concerns means that the applications are divided into modules that do not overlap in terms of functionality. However, some functionalities or modules/concerns always overlap other modules/concerns. Logging is an example of such a concern. AOP provides techniques to encapsulate cross cutting or overlapping concerns. In other words, by using AOP a developer can encapsulate cross cutting concerns.

Spring provides both IoC as well as AOP. They are built into the core of the Spring Framework. The next aspect of the Spring Framework worth noting is its support for layered architecture. It provides this support through its various components. Some of them are:

  1. MVC Component

  2. IOC container

  3. DAO support

These three cater to the Presentation, Business, and Persistence layers. Here is a brief overview:


  1. MVC Component: The flexibility of the Spring Framework is that one can use any MVC framework with it. However, it also provides its own framework, called Spring MVC. Spring MVC implements MVC-Model 2 for JSP. It does this by providing controller Servlet and JSP taglib for you to view.

  1. IoC Container: The business logic layer can be developed without coupling it with any other layers by using IoC provided by the Spring Framework. The IoC container is the core component of the framework. IoC lets the developer implement the business logic as Plain Old Java Objects (POJO) and wire them up so that they can be injected or called at runtime. This approach lets you avoid specifying the business objects as dependencies at compile time.

  1. DAO Support: Any of the technologies, including JDBC, Hibernate, and iBatis, can be used with the Spring Framework to access persisted data. The Spring Framework provides this functionality through its DAO support component. A developer can plug in almost any of the persistence framework by using the DAO component.

That completes the bird’s eye view of the Spring Framework. Next, let us have a look at the steps involved in using the Spring Framework, specifically the IoC capability of the Spring Framework.

{mospagebreak title=Using Spring Framework, Step by Step}

To use the Spring Framework in an application, there are three main steps. They are:

  1. Developing the POJO

  2. Mapping the POJO

  3. Developing the Client

The second step involves an XML file that the Spring Framework uses to understand the configuration. These are the details:

Developing the POJO: POJO or Plain Old Java Objects are, essentially, simple JavaBeans. The first step in using the Spring Framework is to wrap the business logic in a JavaBean. For example, if the logic just says hello, the bean will be as follows:

public class Hello


{


private String greeting;

public Hello()


{


}


public Hello(String a)


{


greeting=a;


}


public String sayhello(String s)


{


return greeting+s;


}


public void setGreeting(String a)


{


greeting=a;


}


}


It is a simple JavaBean with getters and setters. That completes the first step.

Mapping the Beans: The beans need to be mapped using XML so that the IoC container can ‘inject’ the required dependency at runtime. The configuration file contains the following entries. They are:

  • beans
  • bean
  • property
  • value

These are the most common entries. They are arranged in a hierarchical manner. Here are the details:

Beans is the first entry or tag. It is the root tag/element of the configuration file. It encapsulates the bean tags.

The details of the bean are given to the Spring Framework. The details are passed using the attributes of the bean tag. The most commonly used attributes are:

  • id – id by which the bean will be called. It has to be unique.
  • class – the fully qualified name of the bean.

For example, to provide the details for the Hello bean, the bean tag will be as follows:


<beans>

<bean id=”Hello” class=”org.me.Hello”>

:

:

</bean>

<beans>

{mospagebreak title=Mapping the Beans continued}

Property is the child tag of the bean tag. It is used to provide the details of the property of the bean that needs to be accessed or populated. Property is the member variable of the bean that has been mapped using a bean tag. It is done using the name attribute of the property tag. To access the greeting property of the SimpleInterest bean, the property tag will be:


<beans>

<bean id=”Hello” class=”org.me.Hello”>

<property name=”greeting”>

:

</property>

</bean>

<beans>


The value tag is the child of property tag. The value to be given to the property is passed as the child node of the value tag. To pass Good Morning to the greeting property as the value, the code will be:


<beans>

<bean id=”Hello” class=”org.me.Hello”>

<property name=”greeting”>

<value>Good Morning<value>

</property>

</bean>

<beans>


Next, we will see what is required to implement the client.

Developing the Client: Clients need to do the following to access the Spring Framework service:

  • Instantiate the configuration file as a resource
  • Instantiate an implementation of BeanFactory
  • Call the business method

Here are the details.

The first step is to instantiate the configuration file as a resource. To access the business methods, first the configuration file needs to be passed to the framework. Spring accepts the configuration file as an instance of the Resource interface’s implementation class. The most commonly used implementation class is ClassPathResource. It is instantiated using the name of the configuration file. For example, to create an instance of ClasspathResource with the filename beans.xml and assign it to the reference of the Resource interface, the statement will be:

Resource resource=new ClassPathResource(“beans.xml”);

Now we are ready for the second step. To access the beans populated with values, the second step is to get a reference for the BeanFactory interface. The reference would contain the instance of an implementation of the BeanFactory interface. The commonly used implementation is XmlBeanFactory. To get an instance of XmlBeanFactory, the reference of the Resource interface has to be passed to the constructor of XmlBeanFactory. For example, to get an instance of XmlBeanFactory and assign it to a reference of BeanFactory, the statement will be:

BeanFactory factory=new XmlBeanFactory(resource);

Finally, to call the business methods, the instance of bean needs to be retrieved using the getBean() method of BeanFactory. The method return Object instance needs to be typecast into the POJO class. Once that is done, the business objects can be called. In code, it will be:


Hello hello=( Hello)factory.getBean(“Hello”);

System.out.println(hello.sayHello());


That completes the steps for using the Spring Framework. Next, let us look at a real world example.

{mospagebreak title=Spring Framework in the Real World}

In the previous section, the steps required to use the IoC component of the Spring Framework were enumerated. Now, it’s time to develop a real world example based upon these steps. The application we are going to develop will wrap the logic to calculate simple interest. The application will have the following files:

  1. SimpleCalculatorBean.java – POJO that wraps the logic for calculating simple interest.

  2. bean.xml – The configuration file that contains details about the POJO

  3. Client.java – The client for the simple interest calculation


First comes the SimpleCalculatorBean.java. It is a simple Java Bean that has getters and setters for rate, years, and principle. It also contains the calculate method for calculating simple interest. Here is the code:


package org.me;

class SimpleInterestCalculatorBean{


float years;

float principle;

float rate;


SimpleInterestCalculatorBean(){

}


public void setYears(float years){

this.years=years;

}


public float getYears(){

return years;

}

 

public void setPrinciple(float principle){

this. principle = principle;

}


public float getPrinciple(){

return principle;

}

 

public void setRate(float rate){

this. rate=rate;

}

 

public float calculate(){

return (float)((principle*rate*years)/100);

}


public float getInterest(){

return calculate();

}

 

}

{mospagebreak title=Beans and Client}

Next is the beans.xml file. It contains configuration information for the POJO and its properties.


<beans>

<bean id=”SimpleInterestBean”

class=”org.me.SimpleInterestCalculatorBean”>

<property name=”principle”>

<value>10000.00<value>

</property>

<property name=”years”>

<value>10.00<value>

</property>

<property name=”rate”>

<value>9.50<value>

</property>


</bean>

<beans>


Last is the code for Client.java. It first loads the configuration, then gets a reference for BeanFactory and retrieves the instance of SimpleInterestCalculatorBean. After that, it calls the calculate method to get the calculated simple interest. Here is the code:


import java.io.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;


public class Client


{


public static void main(String args[]) throws Exception


{


try


{


System.out.println("please Wait.");

Resource res = new ClassPathResource("beans.xml");

BeanFactory factory = new XmlBeanFactory(res);

SimpleInterestCalculatorBean interest=

(SimpleInterestCalculatorBean)factory.getBean(“SimpleInterestBean);

System.out.println(interest.getInterest());

}


catch(Exception e1)


{ System.out.println(""+e1); }


}


}


That completes the introduction to Spring Framework. What has been described here is just the tip of the iceberg. In the future, I will be discussing each of the components of Spring Framework in depth. Till then…

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort