Pin It

How to provide object dependency injection to a bean using Configuration file in Spring Framework ?.


A simple example to demonstrate, how to provide object dependency injection to a bean using Configuration file in Spring Framework

1. Create a Coat class

package com.hubberspot.spring;

public class Coat {

 private String color;
 private String type;

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 } 

}




2. Create a Tail class

package com.hubberspot.spring;

public class Tail {

 private int length;
 private String color;

 public int getLength() {
  return length;
 }

 public void setLength(int length) {
  this.length = length;
 }

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 } 
}




3. Create a Dog class

package com.hubberspot.spring;


public class Dog {

 private Tail tail;
 private Coat coat;


 public Tail getTail() {
  return tail;
 }

 public void setTail(Tail tail) {
  this.tail = tail;
 }

 public Coat getCoat() {
  return coat;
 }

 public void setCoat(Coat coat) {
  this.coat = coat;
 }


 public void move() {

  System.out.println("A Dog having "+ getCoat().getColor() +
   " color coat \nand "+ getTail().getLength() + " cms" +
   " tail moves slowly ...");

 }

}




4. Create a Spring configuration file placed in the classpath of Spring project


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

 <!-- In order to create an object of bean we define its properties in bean 
  tag. The 'id' attribute value can be thought of as a reference to 'class' 
  attribute value -->

 <bean id="dog" class="com.hubberspot.spring.Dog">

  <!-- dog has a property called as tail and coat which are objects of class 
   Tail and Coat. In order to have Object injection i.e Tail and Coat to class 
   Dog we use ref attribute of property tag which points to the id of bean that 
   we want to inject -->

  <property name="tail" ref="tail"></property>
  <property name="coat" ref="coat"></property>

 </bean>

 <bean id="tail" class="com.hubberspot.spring.Tail">

  <property name="length" value="30"></property>
  <property name="color" value="white"></property>

 </bean>

 <bean id="coat" class="com.hubberspot.spring.Coat">

  <property name="type" value="stripes"></property>
  <property name="color" value="white"></property>

 </bean>



</beans>

5. Create a Test class and Run it to see the output below -

package com.hubberspot.spring;

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


public class WildLifeApplication {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application. 
  ApplicationContext context = 
    new ClassPathXmlApplicationContext(("spring.xml"));

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Dog object. Without implementing new keyword we 
  // have injected object of Dog just by reading an xml 
  // configuration file.
  Dog dog = (Dog)context.getBean("dog");

  // Calling our functionality
  dog.move();

 }

}





6. How it exactly works see below diagram - 





























Output of the program - 


 


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot