Spring Framework Dependency Injection Part 2 : Configure Object in Spring Bean

We have seen how to configure primitives of class in previous blog.

We will see how to configure objects of Class. If ClassA whats to call method of ClassB to complete its business logic, ClassA has to create object of ClassB. So ClassA is depends upon ClassB. Let's see how Spring helps us to inject object of ClassB into ClassA.

We will create 2 classes i.e. Employee and Address in our example. We will see how Spring framework helps us to inject object of Address into Employee. Employee class has primitive as well as object.

package com.javagladiator.spring.core.trainning;

public class Employee {
 private Integer empId;
 private String name;
 private Address address;
 public Integer getEmpId() {
  return empId;
 }
 public void setEmpId(Integer empId) {
  this.empId = empId;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Address getAddress() {
  return address;
 }
 public void setAddress(Address address) {
  this.address = address;
 }
 
 @Override
 public String toString() {
  return "Employee [empId=" 
    + empId + ", name=" 
    + name + ", address=" 
    + address + "]";
 }
}

package com.javagladiator.spring.core.trainning;

public class Address {
 private String city;

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 @Override
 public String toString() {
  return "Address [city=" + city + "]";
 }
}


There are 2 ways to configure dependency of a class in Spring
1. Inner Bean
2. ref Attribute

1. Inner Bean

We can define bean within a bean to provide object dependency. Inner bean will be sub tag of property tag. Inner bean does not have id attribute.


<?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" >
 <bean class="com.javagladiator.spring.core.trainning.Address">
       <property name="city" value="Pune"/>
 </bean>
      </property>
 </bean>
</beans>

Drawbacks of Inner beans:
1. Inner bean does not contain id so it is not possible to get this bean individually. I don't have any way to get Address bean in my client program.
2. If multiple beans depend on Inner bean then we need to configure that inner bean in all beans. For example if Employee and Student beans depend on Address bean then we need to configure Address bean in both Employee and Student bean. We have to duplicate Address bean configuration in all outer beans.
3. If we have to make change in Address bean then we have to change in all outer beans. If we forget to change in one location, it will to inconsistency.

2. ref Attribute:

We can overcome above drawback of inner bean using ref attribute. Let's see in below example - 

<?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="address"/>
 </bean>
 <bean id="address" 
  class="com.javagladiator.spring.core.trainning.Address">
  <property name="city" value="Pune"/>
 </bean>
 <bean id="student" 
  class="com.javagladiator.spring.core.trainning.Student">
  <property name="address" ref="address"/> 
 </bean>
</beans>

As you can see in above program, We have created separate bean for Address class. We have reused address in Student and Employee beans.

Since Address bean has id so we can get this bean from container using id. Please find below App class-


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 applicationContext = 
     new ClassPathXmlApplicationContext("application.xml"); 
     Employee employee = 
    (Employee) applicationContext.getBean("employee");
     System.out.println(employee);
     
     Address address = 
        (Address) applicationContext.getBean("address");
         System.out.println(address);
         
        Student student = 
           (Student) applicationContext.getBean("student");
             System.out.println(student);
    }
}

Note: We have declared Student bean in xml file so don't forget to create corresponding Student class.

package com.javagladiator.spring.core.trainning;

public class Student {
 private Address address;

 public Address getAddress() {
  return address;
 }

 public void setAddress(Address address) {
  this.address = address;
 }

 @Override
 public String toString() {
  return "Student [address=" + address + "]";
 } 
}

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