Sunday, 11 August 2013

Spring core module

Features of Spring core Module:

  1. Gives BeanFactory container
  2. Base module for other Spring modules
  3. Allows to develop local logics and Stand alone applications
  4. Can not be used to develop web applications, distributes applications.
  5. Logics of Spring core module applications can be accessed only from local clients and can’t be accused from remote clients.
  6. If application allows only local clients to access application logic then that application is called local application. Such logic is called local logic.
  7. If application allows both local and remote clients to access application logic and provides location transparency for clients, then that application is called Distributed application and such logic is called distributed logic.
  8. Location transparency means if server application location is changed that will be recognized by client application dynamically without modifying code in client application.
  9. Spring core module applications are local applications. So they allow only local clients
  10. Spring JEE module can develop distributed applications. So that these applications can have with local and remote client.

Spring Bean Class

 
    The java class on which spring container can perform instantiation dependency injection and other life cycle operations is called Spring Bean class.  Any pre-defined or user-defined or third party supplied concrete java class can be configured as Spring Bean for Spring application. Spring Bean need not to be a java bean. But Java Bean can also act as Spring Bean. Distributed applications are client-server applications having location transparency. RMI, EJB, Spring JEE module, web services etc are the distributed technologies to develop these distributed applications.

 

 

Resources for developing Spring core module application


 
       Spring Interface (It is a POJI) (Optional): Spring Interface is the common understanding document between Spring client application developer, spring bean class developer containing declaration of Business method. But it is optional in core module applications. Because the spring Bean class and client application resides and execute from the same JVM in core module applications
 
       Spring Bean class (It is a POJO): Implements spring Interface (optional). Contains Bean properties (member variables) to hold values injected through Dependency Injection. Contains setter methods, constructors supporting Dependency Injection. Contains user defined methods as life cycle methods (optional).Definitions of business methods having business logic that are declared in Spring interface. Other utility/helper methods (Optional).
 
       Spring Configuration file (xml file): Any <filename>.xml can act as Spring configuration file and this file name must be informed to spring container. There is no default name for spring configuration file expected by Spring container when no file name is specified explicitly. Contains Spring Bean classes’ configurations. Contains Spring Bean properties configuration. Contains Dependency injection configurations.
 
       Client Application (Stand alone java application): Must be local client Application. (Must reside in the same JVM where Spring Bean class executes). Activates Spring container by passing Spring Configuration file. Makes spring container performing dependency injection on Spring Bean class properties. Gets access to Spring Bean class object from Spring Container. Calls business methods on Spring Bean class object.
 

Note:

  • Member variables declared in Spring Bean class are called Bean properties.
  • A java bean class can act as Spring Bean class. But it is not mandatory that every spring bean class must be a java bean.
  • In one spring application, we can see multiple Spring interfaces, Spring Bean classes. But all the Spring Bean classes must be configured in Spring configuration file.
  • Creating, managing Spring Bean class object, calling life cycle methods, calling methods related to Dependency injection in Spring Bean class will be taken care by Spring container.
 

references:

Spring reference manual
 
 

Sunday, 14 April 2013

Setter Injection in spring

Setter Injection:

If Spring container uses setter methods of spring bean class to inject dependent values of Bean properties, then it is called “setter injection”. In this process spring container uses zero argument constructor to create Spring Bean class object.

Lets discuss this process with an example


public class DemoBean {

  String message;

  // Setter Method
  public void setMessage(String message) {
   this.message = message;
  }

  // Getter Method.
  public String getMessage() {
   return message;
  }

}

Actually getter method is not necessary for injecting dependencies using setter injection. For performing setter injection setter method is enough. Getter method is only used when we want to access the value of that particular variable otherwise there is no need to write getter method.


Lets see how we can map the above class as a bean in xml file

<bean id="demoBean" class="DemoBean">
  <property name="message" value="Hello"/>
</bean>

As we are performing setter injection properties (Variables in class) of class are defined in <propertry/> tag inside <bean> tag.

In the above bean definition we have given the name of property as message hence spring container searches for a proper setter method for the given property "message". It places 'set' before the property name and makes first letter of the property capital, hence the appropriate setter method for the above property "message" is setMessage(). So spring container searcher for a method with the name setMessage() in DemoBean class.

If there is no proper setter method specified for the given property then it generates an exception.

You can also observe that we have specified an attribute "value" in <property/> tag. That means when setMessage() method is called the value "Hello" is passed as an argument to that method.

Note: setter injection only searches for appropriate setter method of the property name specified in <property/> tag. It doesn't matter which name of variable we have specified in the class. In other words variable names in the class should not be same as the property name of the bean.

For instance in the above example it is not mandatory to have the name of the variable as "message" in DemoBean. But we must have a proper setter method setMessage(String message).


Saturday, 6 April 2013

Inversion Of Control and Dependency Injection in Spring

IOC

    IOC stands for Inversion of Control. Before going to know about what is inversion of control we need to know why inversion of control was introduced.

Tight coupling : The problem

    Inversion of control was introduced to overcome the problem of tight coupling between java objects. Let us have a look at the concept of tight coupling with an example.

public class Customer {


private Address address; 

public Customer() {
  address = new Address(); // Problem: object creation
}

}

The major problem in the above example is object of address class is created in Customer class constructor. In other words Customer class is controlling object creation process of Address class. If there are any changes in Address class then we have to change the code in Customer class.
For example say there is only a parameterized constructor in class Address then we should change the code of Customer class constructor. Hence we can say that both Customer and Address classes are tightly coupled.

Inversion of Control : The Solution
    Definitely the solution resides in shifting the object creation control form Customer class to someone else. If we are able to invert this control to a third party we have found the solution. So the name the solution is Inversion Of Control (IOC)


Above Customer class can be modified to support Inversion of control as shown below




In the above example we have given the object creation control to a IOC Framework. The functionality of IOC Framework are shown below
  • Creates the address object
  • Creates the customer object
  • Injects address object into the customer object 


Dependency Injection:


Above diagram shows how IOC and DI are organised. Concluding the above discussion we can say that IOC is a principle and Dependency Injection is a way of implementing IOC principle

From the above diagram we can find there are three types of Dependency Injections

  • Setter injection
  • Constructor injection
  • Interface injection
Spring supports only Setter and Constructor type of injections

I will discuss in details regarding setter and constructor injection in my next post.

Wednesday, 3 April 2013

Installation of Spring software


1. You can download the spring software from the link http://www.springsource.org/download 

2. To install spring 2.x software, extract spring-framework-2.5.x-with-dependencies.zip file to a folder.

3. Spring-home\changelog.txt file contains differences between various versions of spring software

4. Spring-home\dist\spring.jar file represents the whole spring API and it has one dependent jar file, that is     commnons-logging.jar. Note that there is no spring.jar file from spring 3.x on wards. In that case you have to choose jar file independently which you want to work on.

Installation in stand alone application:
When stand alone java application uses spring API, then the Sprig API related main and dependent jar files must be added to the CLASSPATH.

Installation in web application:
If java web application uses Spring API in its web resource programs, then the Spring API related main and dependent jar files should be added in the WEB-INF\lib folder of web application.
 
Spring API is third party API, because it is coming from a third party organization called Interface 21. When java stand alone application uses third party API then the third party API related main and dependent jar files must be added in the classpath.

Introduction to Spring Framework

In this post I am going to discuss about Spring Framework.

What is Spring Framework?

Spring Framework is an open source application framework for the Java platform. The first version was written by Rod Johnson, who released the framework with the publication of his book Expert One-on-One J2EE Design and Development in October 2002. The framework was first released under the Apache 2.0 license in June 2003. The first milestone release, 1.0, was released in March 2004, with further milestone releases in September 2004 and March 2005. The Spring 1.2.6 framework won a Jolt productivity award and a JAX Innovation Award in 2006.  Spring 2.0 was released in October 2006, and Spring 2.5 in November 2007. In December 2009 version 3.0 GA was released. The current version is 3.0.6.

The core features of the Spring Framework can be used by any Java application, but there are extensions for building web applications on top of the JavaEE platform. Although the Spring Framework does not impose any specific programming model, it has become popular in the Java community as an alternative to, replacement for, or even addition to the Enterprise JavaBean (EJB) model.

Spring is a framework which can develop all kinds of java/J2EE applications in framework style based on dependency injection principle. Spring framework S/W not only provides abstraction layer on Java/JEE core technologies but also provides abstraction layer on some other framework s/w like hibernate, toplink etc.

Overview of Spring Framework:

Spring Framework contains a lot of features, which are well-organized in six modules shown in the diagram below.

The Core package is the most fundamental part of the framework and provides the IOC and Dependency Injection features. The basic concept here is the BeanFactory, which provides a sophisticated implementation of the factory pattern which removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic.

The Context package build on the solid base provided by the Core package: it provides a way to access objects in a framework-style manner in a fashion somewhat reminiscent of a JNDI-registry. The context package inherits its features from the beans package and adds support for internationalization (I18N) (using for example resource bundles), event-propagation, resource-loading, and the transparent creation of contexts by, for example, a servlet container.

The DAO package provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes. Also, the JDBC package provides a way to do programmatic as well as declarative transaction management, not only for classes implementing special interfaces, but for all your POJOs (plain old Java objects).

The ORM package provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis. Using the ORM package you can use all those O/R-mappers in combination with all the other features Spring offers, such as the simple declarative transaction management feature mentioned previously

Spring's AOP package provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, method-interceptors and pointcuts to cleanly decouple code implementing functionality that should logically speaking be separated. Using source-level metadata functionality you can also incorporate all kinds of behavioral information into your code, in a manner similar to that of .NET attributes

Spring's Web package provides basic web-oriented integration features, such as multipart file-upload functionality, the initialization of the IOC container using servlet listeners and a web-oriented application context. When using Spring together with WebWork or Struts, this is the package to integrate with.



Definition of Spring:

Spring is an open source, light weight, loosely coupled, Aspect oriented, Dependency injection based framework software of java environment to develop all kinds of java/J2EE applications having good productivity.