Thursday 29 March 2012

Spring Core (use of dependency injection and Inversion of Control)


 The most fundamental feature of the Spring Framework is IOC and Dependency Injection. Container  resolves dependent components by setting implementation Object  instead of Component Instantiation.Most of the code in Spring is reduced using Dependency Injection and some part of the code (Exception logic ,Transaction Logic )required in Service and DAO classes is  reduced using AOP(Aspect Oriented Programming).Even AOP can simplify Security and Logging Logic etc..

Spring does not provide its own ORM but we can integrate spring with any existing ORM’s. Generally ORM can simplify Persistence logic (JDBC Code) if we are integrating ORM with Spring we can get all features that are provided by Spring(Dependency Injection, Exception Logic, Txn Logic ,Security ,Logging etc..)


Spring provides its own MVC and it is completely based on Dependency Injection
Spring can also be integrated with J2EE Technologies such as EJB,RMI,JMS etc… .


org.springframework.beans.factory.BeanFactory is the actual representation of the Spring IOC container that is responsible for containing and otherwise managing the configured beans. BeanFactory Provides implementation of FactroryPattern  which removes the need of programmatic singleton.


Instantiating a Spring IoC container :
Resource resource = new FileSystemResource(“Spring-config.xml");   
BeanFactory factory = new XmlBeanFactory(resource);
(OR) 
ClassPathResource resource = new ClassPathResource(“spring-config.xml");
BeanFactory factory = new XmlBeanFactory(resource); 


The most commonly used BeanFactory implementation is the XmlBeanFactory class. In terms of XML The XmlBeanFactory takes this XML configuration meta data and uses it to create a fully configured system or application. BeanFactory reads spring-config.xml file and checks if singleton is true or false for configured beans (default is true) if it is true it will create the object once and kept that object In static HashMap and whenever the object is required return the object from Hashmap , if it is false create the object every time and return that  object directly .After Reading config file Container create objects  using Reflection API and while creating to the constructor it will pass argument values using java.lang.reflect.Constructor then once the object is created container will call the setter methods using that object


The ApplicationContext interface builds on top of the BeanFactory (it is a sub-interface) and adds other functionality such as easier integration with Spring's AOP features, message resource handling (for use in internationalization), event propagation, and application-layer specific contexts such as the WebApplicationContext for use in web applications.

Bean Class:-

package com.springcore;
public class Person
{  
         private String  personID;
         private String name;

         Person(String  id)
        {
            this.personID=id;          
        }
        public void setName(String name)  //-------------setter method for Name
       {   
            this.name=name;
       }
       public String getName()
       { 
            return name;
        } 
         
      // Here Setter and Getter for personID field

       public void message() //------we are calling this method using spring given object.
       {                  
           s.o.p(“Welcome “+name);                                                        
       }                              |
}                                  // Here value for the name field is configured in xml file so spring can inject this value to the property while creating the object

   spring-config.xml:--

 <beans>
     <bean id=“person”  class=“com.springcore.Person”>
        <constructor-arg  index=“0” type=“java.lang.String”>
           <value>Trigyn171</value>
         </constructor-arg>
        <property name=“name”>
          <value>Rammohan</value>
       </property>
     </bean>
</beans> 

   Test class :-
        Class Test
{
         public static void main(String[ ] args)
        {
              //Activating Spring Container
             BeanFactory factory=new XmlBeanFactory(“spring-config.xml”);
              //creating and getting the bean object
            Person person=(Person)factory.getBean(“person”);
                                |                                                |…….//bean id
                                |….//Spring creates the object and given to the user
           person.message();
       }
         }

 
  • If the setter method arg type is List we can use <list> tag inside  <property> tag
  • If the setter method arg type is Set then we can use <set> tag inside <property> tag
  • If the setter method argument  type is Map then we can use <map> and <entry>combination inside <property> tag

Ex:-
       <property name=“argument”>
              <map>
                <entry key=“id” value=“1”/>
                <entry key=“name” value=“ram”/>
              </map>
        </property>

      If the setter method argument  type is Properties then we can use 
     <prop> tag inside props> tag

      //private Properties p;

     <property name=“p” >
         <props>   
         <prop key=“name”>   <value>Ram</value>    </prop>
            <prop key=“marks” value=“100”>
         </props>
     </properties>



2 comments:

  1. Thanks Rammohan ,its really useful and helpful for me.
    Please provide some information about spring annotation.

    ReplyDelete
    Replies
    1. Thanks for your suggestion,

      My self i need to upgrade the latest things of spring.
      sure you will get annotation stuff very soon.

      Delete