URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Java
  HTML https://java.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: General Discussion
       *****************************************************
       #Post#: 4--------------------------------------------------
       Dependencies in the form of Objects
       By: shekar Date: September 3, 2014, 4:19 am
       ---------------------------------------------------------
       While constructing Spring Beans (Pojo Classes), one Spring bean
       class depends on another Spring Bean class, for performing some
       business operations.
       If one bean class is depending on another bean class object then
       we call it as an object  dependency.
       If one bean class is depending on another bean class object then
       the Spring IoC container is responsible for creating and
       injecting the dependencies.
       In Spring Configuration file we have two ways to inform the
       container about this object dependency.
       1. By using inner beans.
       2. By using <ref> element.
       By using inner beans:
       Inner bean means a bean which is added for a property by without
       an id in the xml file.
       In case of inner bean definition with setter injection, we
       should add the <bean> element, inside the <property> tag in xml.
       Example:
       public class ObjectDemo1{
       private ObjectDemo2 od;
       public void setOd(ObjectDemo2 od){
       this.od=od;
       }
       }
       xml file:
       <bean id =od1 class ="ObjectDemo">
       <property name=od>
       <bean class ="ObjectDemo2 "/>
       </property>
       </bean>
       In the client application, if we call factory.getBean(“od1”)
       then internally the Spring Framework will do the following
       operations.
       ObjectDemo1 od1 = new ObjectDemo1 ();
       ObjectDemo2 od2 = new ObjectDemo2();
       od1.setOd(od2);
       In the above example, ObjectDemo1 object is depending on
       ObjectDemo2 object. So the Spring container first created the
       ObjectDemo2 object and after that the container created
       ObjectDemo1 object.
       Drawbacks of InnerBeans:
       An Inner bean does not have any id. So it is not possible to get
       that bean object individually from the IoC container.
       If another bean class is also depending on the same bean then in
       the xml file , again we need to add the inner bean definition.
       In order to overcome the above two problems of inner beans, we
       need to use <ref> element in the Spring configuration file.
       <ref> element:
       When dependencies are in the form of objects then to inform the
       IoC container, in Spring configuration xml file, we need to
       configure <ref> element.
       <ref> element is associated with either local or parent and bean
       attributes.
       When we add <ref> element then we need to pass id of
       collaborator bean to its attribute, because the dependency is in
       the form of objects.
       Local:
       If local attribute is used with the <ref> element then the
       Spring IoC container will verify for the collaborator bean
       within same container.
       In general, we try to configure all Spring beans into a single
       Spring configuration file. But it is not mandatory, because we
       can create multiple Spring configuration files also.
       Example:
       public class ObjectDemo1{
       private ObjectDemo2 od;
       public void setOd(ObjectDemo2 od){
       this.od=od;
       }
       }
       xml file:
       <bean id =od1 class ="ObjectDemo">
       <property name=od ref ="od">
       </bean>
       <bean id="od" class ="ObjectDemo2 ">
       </bean>
       In the above xml file, both the dependent bean and its
       collaborator bean are configured in the same xml file. It means
       into the same Spring IoC container. So we can use local
       attribute with <ref> element.
       By loading this above xml file, we will get the Spring IoC
       container object called BeanFactory.
       Syntax
       Resource resource = new ClassPathResource(“spconfig.xml”);
       BeanFactory beanFactory = new XmlBeanFactory (resource, null);
       In Spring IoC, it is possible to create parent and child
       factories.
       Syntax
       Resource resource = new ClassPathResource(“parent.xml”);
       BeanFactory beanFactory = new XmlBeanFactory (resource, null);
       Resource resource1 = new ClassPathResource (“child.xml”);
       BeanFactory beanFactory 1= new XmlBeanFactory (resource1,
       beanFactory);
       In the above syntax, beanFactory1 is a child container of
       beanFactory.
       DependentBean is available in beanFactory (Parent Container). It
       means both dependent bean and its collaborator bean are not
       available in same container. So we cannot use local attribute
       with <ref> element.
       If parent attribute is used with <ref> element then the Spring
       IoC container will search for the collaborator bean always at
       parent container. But not in the Same container.
       parent.xml:
       <bean id="ltyUslServiceBR"
       class="com.macys.platform.providers.checkout.businesslogic.LtyUs
       lServiceBR">
       <property name="ltyUslRedeemRestClient"
       ref="ltyUslRedeemRestClient" />
       </bean>
       child.xml:
       <bean id="ltyUslRedeemRestClient"
       class="com.macys.platform.providers.checkout.rest.LtyUslRedeemRe
       stClient">
       
       </bean>
       In the above xml files, dependent bean is available in
       beanFactory1 (child container) and collaborator bean is
       available in beanFactory (parent container). So parent attribute
       is suitable with <ref> element.
       Bean:
       In this attribute is used with <ref> element then the Spring IoC
       container first verifies for the collaborator bean in the same
       factory. If not available then it will search in the parent
       factory.
       Bean is the combination of both local and parent.
       Bean first works like local and otherwise it works like parent.
       *****************************************************