How to use ref element in Spring Framework

We have seen how to configure object reference using ref attribute of property element in previous blog

We will configure object reference using <ref> element.
We need to use one of the following attribute in <ref> element
1. local
2. parent
3. bean

It is possible to create more than one container by making one container as parent for another container. All container will have separate Spring configuration file(XML file). Child container will load its own configs and reference of parent container in constructor. Below example loads parent and its own configs.

ApplicationContext appParentContext = 
        new ClassPathXmlApplicationContext("application-parent.xml");
String configs[] = {"application.xml"};
ApplicationContext applicationContext = 
   new ClassPathXmlApplicationContext(configs, appParentContext);


1. local:

If local attribute is used in ref element than Spring IoC container will try to find dependency in local container but not in parent container. If bean does not exists in child container then IoC container will throw exception.

application-parent.xml :

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="address" 
  class="com.javagladiator.spring.core.trainning.Address">
  <property name="city" value="Mumbai"/>
 </bean>
</beans>

application.xml :


<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="employee" 
     class="com.javagladiator.spring.core.trainning.Employee">
      <property name="empId" value="1001" />
      <property name="name" value="Parthiv Singh" />
      <property name="address">
       <ref local="address"/>
      </property>
 </bean>
 <bean id="address" 
  class="com.javagladiator.spring.core.trainning.Address">
  <property name="city" value="Pune"/>
 </bean> 
</beans>


We have to 2 xml file - application.xml as child and application-parent.xml as parent. If we try to load these files and get employee bean, We will get Employee bean associated with Address bean of child container which has city=Pune.


package com.javagladiator.spring.core.trainning;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args ){    
      ApplicationContext appParentContext = 
         new ClassPathXmlApplicationContext("application-parent.xml");
      String configs[] = {"application.xml"};
      ApplicationContext applicationContext = 
     new ClassPathXmlApplicationContext(configs, appParentContext); 
      Employee employee = 
    (Employee) applicationContext.getBean("employee");
      System.out.println(employee);
     
   }
}

Output: Employee [empId=1001, name=Parthiv Singh, address=Address [city=Pune]]

If we remove Address bean from child xml then we will get exception.

2. parent:

parent attribute tries to find bean in parent container. If bean is available in both local and parent, container will ignore local bean.
If bean does not exits in parent bean, container will throw exception.

application.xml :


<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="employee" 
     class="com.javagladiator.spring.core.trainning.Employee">
      <property name="empId" value="1001" />
      <property name="name" value="Parthiv Singh" />
      <property name="address">
       <ref parent="address"/>
      </property>
 </bean>
 <bean id="address" 
  class="com.javagladiator.spring.core.trainning.Address">
  <property name="city" value="Pune"/>
 </bean> 
</beans>

Load this XML file with application-parent.xml file(as parent) using application context as we did in local attribute section above.

Now see the difference in output. It will print address with city=Mumbai but city=Pune. It confirms that the container has loaded Address bean which is defined in parent container.

Output:
Employee [empId=1001, name=Parthiv Singh, address=Address [city=Mumbai]]

3 bean:

The bean attribute tries to inject bean from load container. If local container does not contains that bean, it will tries to find in parent. If it does not found in both location then container will throw exception.

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="employee" 
     class="com.javagladiator.spring.core.trainning.Employee">
      <property name="empId" value="1001" />
      <property name="name" value="Parthiv Singh" />
      <property name="address">
       <ref bean="address"/>
      </property>
 </bean>
 <bean id="address" 
  class="com.javagladiator.spring.core.trainning.Address">
  <property name="city" value="Pune"/>
 </bean> 
</beans>

ref attribute explained in previous blog, works like bean attribute.

Comments

Popular posts from this blog

Data types and Literals in Java

How to define Auxiliary Constructor in case class in Scala

equals() method of Object class in Java