Thursday, October 21, 2010

Spring with in a Day Part-1

For a small scale project, we can use JSP model-I architecture but in case of large scale project, this model is not suitable. We can use the framework like Struts, Spring etc. These framework contains the code i.e. commonly required as part of our application and the framework provides the process the carried out various task in the implementation of our project.

Difference between Struts & Spring
> Spring framework is more flexible than Struts framework.
> Struts can be used for the development of the web application only but Spring can be used for the development of GUI, web application, applets etc.
> EJB containers can automatically control the transaction (the developer need not write the code or start/end the application). The same kind of feature is provided as part of Spring but is not available as part of Struts.
> Spring supports AOP but Struts cannot.

Spring framework is very flexible and it is divided into multiple modules. As part of our project we need not use all the modules of spring. Depending upon our requirement we can choose appropriate module.


=> CX (part of module Y) uses COne and CTwo (part of X module).
=> CA (part of Module Z) uses CThr (part of X module).
=> Module Y and module Z (part of the top layer) uses/takes the supports of/depends upon module X that is the part of bottom layer.

public class CX {
           public void method1(){
                      COne cone=new COne();
                      CTwo ctwo=new CTwo();
public class CA{
           public void method2(){
                      CThr cthr=new CThr();

Spring Diagram

Dependency Injection:
Dependency Injection (DI) is the process to give the dependency to the other object by setter or constructor injection.
As example, Address object depends upon/uses three String objects; the setter provides the dependent objects to the Address object. This process is giving the dependencies to the Address object is called Dependency Injection.

package pack;
public class Address{
           String flatNo;
           String Street;
           String city;
           //add setter and getter for all properties

pack.Address add=new pack.Address();
add.setFlatNo(“D-30/A/12, Bong Appartment”);
add.setStreet(“Park Street”);

Inversion of Control
i. Spring framework can take care of creating the Address object, the three objects and injecting dependencies using setter injection or constructor injection.
ii. Normally the developer need to write the code to create the object and inject the dependencies (we have to provide the code for controlling the process of the setting the dependencies). If we leave the job of setting the dependency to spring and spring will be able to control the process of setting of the dependencies. The process of leaving the job or setting of the dependencies to spring is called as Inversion of Control (IOC).
iii. For using dependency injection (DI) feature provided by spring we must provide the info about the spring bean in applicationContext.xml

Bean for Address object in applicationContext.xml
< bean id=”address” class=”pack.Address”>
        < property name=”flatNo”>
                < value type=”java.lang.String”> D-30/A/12, Bong Appartment< /value>
        < /property>
        < property name=”street”>
               < value type=”java.lang.String”> Park Street< /value>
        < /property>
        < property name=”city”>
               < value type=”java.lang.String”>Kolkata-01< /value>
        < /property>
< /bean>

Spring Container
> As part of spring, spring container is provided.
> Spring Container is an object which provides the implementation of BeanFactory interface.
> Spring Container is a java object that provides the implementation org.springframework.beans.factory.BeanFactory interface.
> We can use object for picking up the resources (applicationContext.xml) from the CLASSPATH.
> ClassPathResource implemants Resource interface.
> The spring container will able to create the objects and able to setup the dependencies between the objects.

*Note: CLASSPATH: CLASSPATH is a list of directoried and jar files that contains the classes.

packatge pack;
public class SprApp{
     public static void main(String[] args){
         Resource res=new ClassPathResource(“applicationContext.xml”);
         BeanFactory sprCont=new XmlBeanFactory(res);
         Object obj=sprCont.getBean(“address”);
         pack.Address add=(pack.Address)obj;

> The method getBean() returns the Address object as Object data type.
> the Spring Container maintains only one Address object with id as address. Even if we call the getBen() multiple times the spring container will returns the reference of same Address object.
           BeanFactory sprCont=new XMLBenaFacotory(new ClassPathResource(“applicationContext.xml”));
           Object objOne=sprCont.getBean(“address”);
           Object objTwo=sprCont.getBean(“address”);
> The above code gets the reference of a single Address object if scope is set to “singleton”.

> The above code gets the refrence of the two Address object if scope is set to “prototype”.

• In case of web application we can use the values request, session for the scope attributes.
• In place of id sttributes we can use name attribute also. The xml editor considers there is an error if we provide the same value for multiple id attributes.

> ApplicationContext is an interface that extends the BeanFactory interface provided by spring.
> Context package build on the top of Bean package and context contains the ApplicationContext is the sub-interface of BeanFactory interface.
> ApplicationContext i.e. Context package provides the advance functionalities like support for i18N application development and Aspect Oriented Programming (AOP).

Code for Spring Container using core module:-
Resource res=new ClassPathResource(“applicationContext.xml”);
BeanFactory sprCont=new XmlBenaFactory(res);
BeanFactory sprCont=new XmlBeanFactory(new ClassPathReaource(“applicationContext.xml”));

Code for Spring Container using context module:-
ApplicationContext sprCont=new ClassPathXmlApplicationContext(“applicationContext.xml”);

Lazy & Eger Bean Creation:-
i. The spring container supports the process of Lazy creation an bean object or eagerly creation bean object.
ii. We can ask the bean container to eagerly created object using the configuration show bellow-
       a) < bean id=”address” class=”pack.Address” scope=”singleton” lazy-init=”false”>< /bean>
When the above configuration is used spring container create the bean object when application creates the container.
       b) < bean id=”address” class=”pack.Address” scope=”singleton” lazy-init=”true”>< /bean>
When we use lazy-init=”true” the container create the bean object when the getBean() will be call the first time.
Note: *Lazy-init attributes will be used when scope=”singleton”.

Wiring or Bean Wiring
i. The process of setting up the dependencies is called as wiring.
ii. Setting the dependency injection between two objects is called the wiring.
iii. The spring container will take care of creating various objects and setting between different object as shown bellow.

package pack;
public class Emp{
      int empNo;
      String empName;
      Address addr;
      // add setter and getter for all properties

< bean id=”eb” class=”pack.Emp”>
        < property name=”empNo”>
               < value type=”int”> 100< /value>
        < /property>
        < property name=”empName”>
               < value type=”String”>EmpName< /value>
        < /property>
        < property name=”addr”>
               < /ref>
        < /property>
< /bean>

Auto Wiring
In this process spring automatically inject or wire the bean properties to create the bean that is using another bean as property. In our bean we are not provide the existing bean as property but provide the configuration using autowire attribute as byname, byTime, byType.

byName=>search the bean with same name of the property and inject it in the bean. If it is not available container throws the exception.
byType=>search the bean with same data type of the property and inject it in the bean. If not available the throws the exception.
byTime=>search the bean with same name of the property if available inject it. If it is not available then it searches the bean by data type. if it is available inject that bean neither throws the exception.

Constructor Injection
Spring container supports constructor injection for the injecting the dependencies.
For using constructor injection add a constructor to the Address class as shown bellow –

package pack;
public class Student {
      String name;
      String address;
      String email;
      public Student(String name, String address, String email){
      //add setter and getters for all properties

< bean id=”student” class=”pack.Student”>
        < constructor-arg index=”0” type=”java.lang.String”>
                < value type=”java.lang.String”> StudentName < /value>
        < /constructor-args>
        < constructor-arg index=”1” type=”java.lang.String”>
                < value type=”java.lang.String”> StudentAddress < /value>
        < /constructor-args>
        < constructor-arg index=”2” type=”java.lang.String”>
                < value type=”java.lang.String”> < /value>
        < /constructor-args>
< /bean>

No comments:

Post a Comment