Happy Diwali
happy diwali

Saturday, 5 July 2014

Spring Ioc



Spring Framework
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 Java EE 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.
The Spring Framework comprises several modules that provide a range of services:

DAO & JDBC

ORM


JEE

WEB MVC Module

AOP
IOC
IOC (Inversion Of Control:
For all modules IOC (Inversion Of Control) is the base Module.
IOC will take care of creating an object to the class and setting values of the attributes of the object.
IOC container mainly used for configuration. Data is supplied to the framework through xml file. Whatever we set inside xml file, will be used inside java program.
The IOC module provides configuration to the other modules of spring framework.

AOP(Aspect Oriented Programming):
It is anti Object oriented programming. Oop concepts mainly used for hierarchical (inherited) classes, the classes which are under one hierarchy. AOP is for any classes, horizontally no relation is required among the classes. After developing we the aspect we can apply that aspect horizontally to any of the classes. No relation is required among the classes.

JDBC DAO (Java Enterprise Edition and Data Access Object) module:
Used for interacting with the database using JDBC API. In order to minimize development work while interacting with the database by using JDBC, we use DAO module. DAO module internally uses pure JDBC API.
No additional features like ORM or cache Mechanisms are used. It receives just the SQL query and does all the necessary steps to send the query to the database.

ORM (Object Relational Mapping):
If we are using hibernate framework to interact with the database from a java program, then the interaction procedure is simplified by using Spring ORM. The ORM tools popularly used today are hibernate and iBatis. The ORM tools usage is simplified by the ORM module.
ORM module straightaway gives Session object by starting the Transaction to the java program.

WEB MVC (Model View Controller) module:
Almost similar to struts and JSF. Used only for developing web applications.
JEE:
Simplifying development of EJB, JMS (Java Messaging Service).

IOC Module

In eclipse go to FileàNew àProjectàJava project: app1
First let’s see one simple program without using Spring framework.
app1/src/com/lara/Person.java
packagecom.lara;

publicclass Person {
      private String firstName, lastName;
     
      public Person()
      {
            System.out.println("Person");
      }

      public String getFirstName() {
            System.out.println("getFirstName");
            returnfirstName;
      }

      publicvoidsetFirstName(String firstName) {
            System.out.println("setFirstName");
            this.firstName = firstName;
      }

      public String getLastName() {
            System.out.println("getLastName");
            returnlastName;
      }

      publicvoidsetLastName(String lastName) {
            System.out.println("setLastName");
            this.lastName = lastName;
      }
}

Manager.java
packagecom.lara;

publicclass Manager {
      publicstaticvoid main(String[] args) {
            Person p1 = new Person();
            p1.setFirstName("Ramu");
            p1.setLastName("B");
            System.out.println("object is ready withteh state");
            System.out.println(p1.getFirstName());
            System.out.println(p1.getLastName());
      }
}

Output:
Person
setFirstName
setLastName
object is ready withteh state
getFirstName
Ramu
getLastName
B
Without framework we are managing one object to bean class
Create a xml file to store the configuration data.
Right click on srcàNewàFile: create on .xml file, Eg. beans.xml (can be any name).

beans.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!--
  - DispatcherServlet application context for the image database.
  -->
<beansxmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

      <beanid="person1"class="com.lara.Person">
            <propertyname="firstName"value="Sagar"/>
            <propertyname="lastName"value="B"/>
      </bean>
     
</beans>



Manager2.java
packagecom.lara;

importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.xml.XmlBeanFactory;
importorg.springframework.core.io.ClassPathResource;

publicclass Manager2 {
      publicstaticvoid main(String[] args) {
            BeanFactory factory = newXmlBeanFactory(newClassPathResource("beans.xml"));
           
            Person p1 = (Person) factory.getBean("person1");
            System.out.println("Object is ready with the state");
            System.out.println(p1.getFirstName());
            System.out.println(p1.getLastName());
      }
}

No comments:

Post a Comment

happy diwali
happy diwali