URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Java
  HTML https://java.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: General Discussion
       *****************************************************
       #Post#: 3--------------------------------------------------
       Instantiating a container && Types of Dependency Injecti
       on
       By: shekar Date: August 25, 2014, 7:13 am
       ---------------------------------------------------------
       The Spring core container can be instantiated by creating an
       object of any one of the BeanFactory or ApplicationContext
       classes supplying the Spring beans configurations.
       The various implementation of BeanFactory are mainly
       differentiated based on the Spring beans configuration format
       they understand and the way they locate the configurations.
       The Spring container using the Spring Beans XML configuration
       file as configuration metadata.
       Syntax:
       BeanFactory beans = new XmlBeanFactory (new FileSystemResource
       (“beans.xml”));
       In the preceding code snippet we are using XmlBeanFactory
       implementation for instantiating the Spring container with
       “beans.xml” file as a configuration file, as the name describes
       the ClassPathXmlApplicationContext locates the XML configuration
       file in the classpath
       We can even use ApplicationContext to instantiate the container;
       ApplicationContext context = new
       ClassPathXmlApplicationContext(“beans.xml”);
       In code snippet we are using ClassPathXmlApplicationContext
       implementation to instantiate the Spring container with
       “beans.xml” file as a configuration file, as the name describes
       the ClassPathXmlApplicationContext locates the XML configuration
       file in the
       classpath.
       Types of Dependency Injection:
       Dependency Injection classified as the following three types.
       i. Setter Injection
       ii. Constructor Injection
       iii. Interface Injection
       In Spring Framework we use Setter and Constructor Injection but
       not Interface Injection.
       In Struts 2.X, we have Interface Injection.
       Setter Injection:
       The Spring (IoC) container uses setter method in the dependent
       class for injecting its dependencies (Collaborators).
       Spring Container knows whether to perform Setter Injection or
       Constructor Injection by reading information from an external
       file called as Spring Configuration file.
       In case of Setter Injection, the class must contain a setter
       method to get the dependencies; otherwise Spring container does
       not inject the dependencies to the dependent object.
       Example:
       public class A{
       B obj;
       public void setObj(B obj){
       this.obj=obj;
       }
       public void m1{
       //business logic here
       }
       }
       public class B{
       //business logic here
       }
       In the above example, Class A is called Dependent and Class B is
       called Collaborator.
       In the Dependent class there is a setter method for getting
       collaborator object. So we call it as Setter Injection.
       In Spring Framework, we call each class as a “Spring Bean” .This
       Spring Bean is no where related with Java Bean.
       Spring Bean and Java Bean both are not same because a Java bean
       needs definitely a public default constructor. But in a Spring
       bean sometimes we include default constructor and sometimes we
       do not.
       In Spring bean classes; there exists the following three types
       of dependency values.
       i. Dependency in the form of Primitive Values
       ii. Dependency in the form of Objects
       iii. Dependency in the form of Collections
       Example:
       public class TestBean{
       //Dependency in the form of primitives
       private int i;
       //Dependency in the form of objects
       private SampleBean sampleBean;
       //Dependency in the form of collections
       private List list;
       }
       Dependency in the form of Primitives:
       The Spring container understands whether dependency is in the
       form of primitive or not, whether setter injection or
       constructor injection is required by reading Spring
       configuration file.
       Spring Configuration file is identified with <any name>.xml
       If the dependency in the form of primitives then we can directly
       provide value in the xml file for that property.
       In Spring configuration file, we used <property> element. So
       Spring container understands that there is a Setter Injection in
       the bean class.
       Inside <property> element, we have used <value> element. So
       Spring container understands that the dependency is in the form
       of Primitives.
       Syntax:
       <property name=”k”>
       <value>100</value>
       </property>
       In Spring Configuration file, we can use <value> as a sub
       element of <property> tag or value as an attribute of <property>
       tag
       Syntax:
       <property name=”k” value=”100”/>
       Example:
       public class primitivesDemo{
       private String name;
       private int id;
       public void setName( String name){
       this .name=name;
       }
       public void setId(int id){
       this .id =id;
       }
       public void getDetails(){
       system.out.println("Name"+name);
       system.out.println("ID"+id);
       }
       }
       package com.macys.platform.services.customer.v1.businesslogic;
       import org.springframework.context.ApplicationContext;
       import
       org.springframework.context.support.ClassPathXmlApplicationConte
       xt;
       public class PrimitiveClient {
       public static void main(String &#91;]args){
       ApplicationContext context=new
       ClassPathXmlApplicationContext("primitive.xml");
       PrimitivesDemo
       primitivesDemo=(PrimitivesDemo)context.getBean("pd");
       primitivesDemo.getDetails();
       
       }
       }
       primitive.xml(Configuration file)
       <beans>
       <bean id ="pd" classs
       ="com.macys.platform.services.customer.v1.businesslogic.primitiv
       esDemo">
       <property name ="name" value="shekar"/>
       <property name ="id" value="1140"/>
       </bean>
       </beans>
       Description of PrimitivesClient:
       Step 1:
       A. Spring environment starts by loading Spring Configuration
       file into Resource Object.
       B. We call this Stpe1 as a bootstrapping of Spring Framework
       C. Resource is an interface and classpathResource is an
       implementation class given by
       Spring Framework.
       D. Both Resource interface and classpathResource class are given
       in org.springframewok.core.io package
       Syntax:
       Resource resource = new ClassPathResource (“beans.xml”);
       Step 2:
       A. Spring IoC container object will be created by reading bean
       definitions from the Resource object.
       B. Spring IoC container is called BeanFactory and this container
       is responsible for creating the bean objects and for assigning
       its dependencies.
       C. BeanFactory is an interface and XmlBeanFactory is an
       implementation class of it.
       D. BeanFactory is an interface given in
       org.springframework.beans.factory and XmlBeanFactory is a class
       given in org.springframework.beans.factory.xml package
       Syntax:
       BeanFactory beanFactory = new XmlBeanFactory(res);
       Step 3:
       A. Get the bean object from the Spring container by calling
       getBean() method.
       B. While calling getBean() method, we need to pass the bean id
       as a parameter.
       C. getBean() always return object.
       D. We need to type cast the object into our Spring Bean type.
       Syntax:
       Object obj = beanFactory.getBean(“pd”);
       PrimitivesDemo pd = (PrimitivesDemo)obj;
       E ) Call the business method of the SpringBean by using the
       object.
       Pd.getDetails ()
       *****************************************************