Saturday, 14 March 2015

Spring Core

Framework:
Framework is a piece of s/w. There are so many frameworks are available in the market. Some of them are
1. Spring
2. Struts
3. Hibernate
4. JSF etc
-->Frameworks simplify the development process. The popular frameworks like struts and spring comes under the MVC2.
-->we use the framework it will take care of the form validation and forwarding the request  to appropriate resource in the project.
-->Framework take care of handling the errors in the project
-->If we are using framework we no need to take care of complexity and repeated occurred problems across the multiple projects
--> In framework we no need to provide the controllers. Frameworks comes with controllers.
--> If we use the framework like spring we no need to provide the more amount of code to interact with database server. We can use the template classes provided by framework team.
JDBCTempate.save();
HibernaeTemplate.save();
--> The frameworks are designed by experts
--> By using spring framework we can develop standalone and web based applications
--> By using framework we can run application without servlet and jsp container
--> Spring providing such features like IOC and AOP.
IOC-Inversion of control
AOP-Aspect oriented programming
Note: Spring is divided into multiple modules we can use these modules based on our requirement.
Inversion Of Control:
Developer is responsible to carry out the task, instead of developer if somebody has done that task and output is enjoyed by the developer. This is called as IOC
--> Spring framework is divided into multiple modules. Every module is having set of classes and interfaces
--> The following diagram specify the spring module architecture.
--> The advantage of spring framework is based on our requirement we can include the modules.
Core Module:
The core module contain the set of classes and interfaces which are responsible to create IOC container.
--> IOC container is responsible to create the object and establishing the dependincess between the objects.
AOP Module
By using this we can resolve the problem of OOPS.AOP is included part of spring framework
--> AOP is useful for transactions
--> AOP is a concept which is resolve the problems of Object orientation Aspect.
DAO module
--> DAO is takes of the JDBC in project.
--> By using spring DAO we can simplify development of JDBC.
JEE module
JEE contain the set of small API like JMS, JMSN and email

Web Module:
This module is used to develop the web based applications based on MVC2 architecture
-->  Spring framework is integrated with reporting tools as well
EX:PDF generation , excel, Ireports and Jsper ...............etc.
-->  Spring guys are placed classes and interfaces of every module in separated jar files.
 
-->  In the above class when we create Object all instance fields initialized with default values
a.setStreet("nml");
-->   when above line got executed we are establishing the dependencies between the string and address objects.
-->  We can treat above line as address is depends on string object.
-->  In the above code string object is establishing the dependences between string and address pbjects
-->  We can treat setXXX() is establishing the dependencies between the objects we can treat it is dependency injection.
-->  As a developer till now we are creating the object and establishing the dependencies between the objects.
-->  If we use spring framework , framework will takes care of establishing the dependencies between those objects.
-->  If spring framework dose this work we can call it as "dependency Injection or inversion injection".

1.     CORE MODULE

-->  In spring framework we want to create the object to the bean and establishing the dependencies between the objects we should able to create the IOC continer.
what is spring container or IOC container?
Spring container is an object which provide the implementation of "org.springframework.beans.factory.BeanFactory" directly or indirectly.
-->  If we would like to create the spring container we have provide the implementation to BeanFactory interface. If we create object for this class we can call it as spring continer.
-->  We have the implementation of BeanFactory implementation class "XMLBeanFactory".
-->   Spring people has provided a package "org.springframework.core.io". This package is used to read the contents of the resources.{files, xml....}
-->  Spring people has released an interface Resource. Those are provided the implementation classes as well.
1. ClassPathresource.(reead in classpath)
2. FileSystemResource. (file system)
-->  applicationContext.xml file is placed in bin folder.
-->  The following demonstration is for creation of spring container, by using spring container we can establish the dependencies between objects
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class MySpringApp {
      public static void main(String[] args) {
            Resource resource = new ClassPathResource("applicationCOntext.xml");
            BeanFactory continer = new XmlBeanFactory(resource);
            continer.getBean("abd");
      }
}
-->  Here developer is not creating the object and not establishing dependencies b/w the objects. Spring takes care of these process.
-->  whenever we create object for bean factory we can say that we created spring container.
-->  When we got container object we can ask container to create bean object and establish dependencies b/w the objects.
-->   getBean() method will establish the dependencies b/w the objects.
-->  When the given id is not exist in the configuration file we will get the  org.springframework.beans.factory.NoSuchBeanDefinitionException.
public class MySpringApp {
      public static void main(String[] args) {
            Resource resource = new ClassPathResource("applicationContext.xml");
            BeanFactory continer = new XmlBeanFactory(resource);
            Address addr = (Address) continer.getBean("address");
            System.out.println(addr.getAddressId());
            System.out.println(addr.getAddress());
      }
}
--> By  default spring container create only one bean object when we request second time it will return same object.
public class MySpringApp {
      public static void main(String[] args) {
            Resource resource = new ClassPathResource("applicationContext.xml");
            BeanFactory continer = new XmlBeanFactory(resource);
            Address addr = (Address) continer.getBean("address");
            Address addr2 = (Address) continer.getBean("address");
            System.out.println(addr);
            System.out.println(addr2);
      }
}
--> Instead of container controlling this we can control this by configure scope attribute in the applicationContext.xml file
      <bean name="address" class="com.ibm.springcore.Address" scope="prototype">
--> we have 4 values to the scope attribute
·         singleton(default value)
·         prototype
·         request
·         session
-->  When scope="prototype" every time container creates new object
-->  We can change the spring configuration file........me.xml
      Resource resource = new ClassPathResource("me.xml");
--> we can read contents of configuration file from the
--> In spring core module we have two different packages they are beans and context packages
--> In beans package we have an interface BeanFaactory
--> In context package we have another interface "ApplicationContext". it inherit the properties of BeanFaactory interface.
--> ApplicationContext is having some of the methods which are use full to deal with I18N applications
--> ClassPathXmlApplicationContext is the implementation class for the ApplicationContext interface.
--> In case of web based application we really no need to create the object for "XMLWebAPplicationCOntext".  The internal code of web application takes care of the creating the container object.
public class MySpringApp {
      public static void main(String[] args) {
           
            ApplicationContext continer = new ClassPathXmlApplicationContext("me.xml");
            Address addr = (Address) continer.getBean("address");
            Address addr2 = (Address) continer.getBean("address");
            System.out.println(addr);
            System.out.println(addr2);
      }
}
--> when we create application context based on the ApplicationContext  it read contents of the configuration file. This will read all the beans tags and find the scope attribute. If scope is singleton it will create all the bean objects.
--> If type is prototype spring container will not create the bean object at the time of container object is created. In this case spring container create the bean object when we call getBean()
      <bean name="address" class="com.ibm.springcore.Address" scope="singleton" lazy-init="true|false|default">
--> For the above configuration when we create container object based on the ApplicationContext spring container create all the bean objects at the time of container object is created. To prevent this we configure lazy-init attribute. For this if we pass true container won't create the bean object at the time container object is created. this attribute will work with singleton scope only.
--> In configuration file if we doesn't specify the type attribute spring container use the spring bean type.
-->  In spring 3.0 thy are added new feature pnamespace. This is used to reduce the spring configuration file.
      <bean id="addresses" class="com.ibm.springcore.Address" p:addressId="20" p:address="nml"/>

-->In spring we can configure reference types as well
--> reference type means "the property of one bean is object of the another bean".
 In the above file configuration is
<bean id="employee" class="com.ibm.springcore.Employee" lazy-init="true" autowire="default">
            <property name="id" value="1"></property>
            <property name="name" value="chandra"></property>
            <property name="address" ref="address"></property>
      </bean>
      <bean name="address" class="com.ibm.springcore.Address" scope="singleton" lazy-init="true">
            <property name="addressId" value="1"></property>
            <property name="address" value="nal"></property>
      </bean>
Main class
public class MySpringApp {
      public static void main(String[] args) {
           
            ApplicationContext continer = new ClassPathXmlApplicationContext("applicationContext.xml");
            Employee e = (Employee) continer.getBean("employee");
            System.out.println(e.getId());
            System.out.println(e.getName());
            System.out.println(e.getAddress().getAddressId());
            System.out.println(e.getAddress().getAddress());
      }
}
wiring:
establishing dependencies b/w objects is called as wiring.
--> In spring bean configuration file when we add a bean it add an attribute auto wire. By default spring won't use this property so it set default.
--> Auto wire takes 3 values byName, byValue, no
-->    <bean id="employee" class="com.ibm.springcore.Employee" autowire="byName">
            <property name="id" value="1"></property>
            <property name="name" value="chandra"></property>
      </bean>
--> In the above configuration we configured tow beans id and name. When we call the getBean(). Now spring container creates employee object and it checks the properties of employee object and it find the properties for the id and name in the spring configure file so establish the dependencies b/w then. We have another property address. We haven't configure this in the spring configuration file. Now spring checks for the autowire attribute. This value is byname Now spring get the property name and checks with that property any beans are configured if available establish the dependencies.[Here it will check with reference value of employee object for example Address "addr"  we need to configure "addr" in configuration file address bean id ]
      <bean id="employee" class="com.ibm.springcore.Employee" autowire="byType">
            <property name="id" value="1"></property>
            <property name="name" value="chandra"></property>
      </bean>
--> For the above configuration container get address property and checks weather user provided wiring or not if not container checks for the autowire attribute. If it is byType container checks for the class type in the bean configurations and it establish the dependencies.
spring types:
In spring we are using following types.
·         value
·         ref
·         null
·         list
·         set
·         map
·         properties


<bean abstract="true"
      autowire-candidate="default"
      autowire="default"
      class=""
      dependency-check="default"
      depends-on=""
      destroy-method=""
      factory-bean=""
      factory-method=""
      init-method=""
      lazy-init="default"
      name=""
      parent=""
      primary="true"
      scope=""
      id="">
      <constructor-arg index="" ref="" type="" value="">
            <bean></bean>
            <description></description>
            <idref bean="" local=""></idref>
            <list merge="default" value-type="">
                  <bean></bean>
                  <idref/>
                  <list></list>
                  <map></map>
                  <null></null>
                  <props></props>
                  <ref/>
                  <set></set>
                  <value></value>
            </list>
            <map key-type=""
                  merge="default"
                  value-type="">
                  <entry key="" key-ref="" value="" value-ref=""></entry>
            </map>
            <null></null>
            <props merge="default">
                  <prop key="" ></prop>
            </props>
            <ref bean="" local="" parent=""/>
            <set merge="default" value-type="">
                  <bean></bean>
                  <idref/>
                  <list></list>
                  <map></map>
                  <null></null>
                  <props></props>
                  <ref/>
                  <set></set>
                  <value></value>
            </set>
            <value type=""></value>
      </constructor-arg>
      <lookup-method bean="" name=""/>
      <meta key="" value=""/>
      <property name="" ref="" value="">
            <bean></bean>
            <description></description>
            <idref/>
            <list></list>
            <list></list>
            <map></map>
            <meta key="" value=""/>
            <null></null>
            <props></props>
            <ref/>
            <set></set>
            <value></value>
      </property>
      <qualifier type="org.springframework.beans.factory.annotation.Qualifier" value="">
            <attribute key="" value=""/>
      </qualifier>
      <replaced-method name="" replacer="">
            <arg-type match=""></arg-type>
      </replaced-method>
      <description></description>
</bean>    


Injection types:
In spring we can establish dependencies in two ways
·         method injection
·         constructor injection
If we establish the dependencies by using setter method we can say it method injection
If we establish the dependencies by using constructor injection we can say it constructor injection
public class Address {
     
      String street;
      String city;
      String state;
     
      public Address() {
            this.state = "";
            this.street = "";
            this.city = "";
      }
     
      public Address(String street, String state, String city) {
            this.street = street;
            this.state = state;
            this.city = city;
      }
}
<bean name="add" class="com.ibm.springcore.Address" scope="singleton" lazy-init="true">
            <constructor-arg index="0" type="java.lang.String" value="abc"></constructor-arg>
            <constructor-arg index="1" type="java.lang.String" value="bcd"></constructor-arg>
            <constructor-arg index="2" type="java.lang.String" value="cde"></constructor-arg>
      </bean>
--> For constructor args we no need to specify the index and type. Even though we need to specify it because if we have multiple constructors with different types at that time this type and index values will use full to container.


No comments:

Post a Comment