index-1-1.jpg
  • Aurora Solutions

Spring Framework: Injecting Beans

In our previous article Spring Framework: declaring beans we discussed the first part of wiring, declaring beans, and in this article we are gonna cover second part that is injecting beans into one another.


In this article we are going to inject values and beans into other beans using their constructors.


Step 1: Create an interface Writer with a single method write() and another interface Poem having a single method recite(). Return type of both these methods are void.


Step 2: Create an application context file ‘application-context.xml’ that resides in src/resources folder of maven.

(We have covered both these steps in previous article).

In first part of this article we will inject values into beans.

Step 3: Create a SentenceWriter class that will write number of sentences on console. It will implement Write interface. As given below:

public class SentenceWriter implements Writer{

	private int sentences = 0;

	public SentenceWriter(int sentences) {
		this.sentences = sentences;
	}

	public void write() {
		System.out.println("Writing " + sentences + " sentences.");
	}

}

This writer has a variable that will store the number of sentences and also a custom constructor through which we will be telling the writer number of sentences to write. Given method write() is also simple that just prints the number of sentences.


Step 4: Now we will have to declare this bean in application context and inject number of sentences in the bean. Following xml line will do this trick.

<bean id="sentence" class="com.aurorasolutions.springframework.beans.SentenceWriter" >
        <constructor-arg value="15" />
</bean>

The only tag that is present here in addition to bean declaration syntax is constructor-arg tag. This tag tells bean that we want to inject something in bean constructor. Now that injection can be some value or some other object. The term ‘value’ indicates that we are going to inject a value into bean.


The above declaration can be written in Java as:

new SentenceWriter(15);

Till now we saw how we can inject values into beans. But now we will see how we can inject other objects/beans into beans.


Step 5: Create a class TheStar that implements interface Poem. This class will only implement recite() method that will print some poem. Like:

public class TheStar implements Poem{
	private static String[] POEM = {
		"Twinkle twinkle little star,",
		"How I wonder what you are,",
		"Up above the world so high,",
		"Like a diamond in the sky"
	};

	public void recite() {
		for(String verse : POEM) {
			System.out.println(verse);
		}
	}	
}

It just prints out a poem when its recite method is called.


Step 6: Now we will create a class PoemWriter that will implement Writer interface. This class will take two arguments in its constructor. 1) An implementation of Poem interface and 2) A value.


Then the write method of this writer will just print out that name and will invoke recite() method of poem on the object that was passed to it. To sum it all up lets take a look at the code below:

public class PoemWriter implements Writer{

	private String poetName;
	private Poem poem;

	public PoemWriter(String poetName, Poem poem) {
		this.poem = poem;
		this.poetName = poetName;
	}

	public void write() {
		poem.recite();
		System.out.println("nThis poem has been written by " + poetName );
	}	
}

Step 7: The next step is to tell spring to inject proper implementation of Poem interface in PoemWriter. Following bean tag is in application-context.xml file for this purpose:

<bean id="poet" class="com.aurorasolutions.springframework.beans.PoemWriter" >
        <constructor-arg value="Jane Taylor" />
        <constructor-arg ref="star" />
</bean>

As you can see we have again used constructor-arg tag. This tag is injecting two things. 1) a value that is name of author and 2) a ref to object that is actually implementation of Poem class. The declaration of Poem class will be as given below:

<bean id="star" class="com.aurorasolutions.springframework.beans.Poem" />

You may notice the order of constructor-arg tag. Its same as the order of arguments in the class constructor.


The next step is to get this bean from context and execute it.


This already has been shown in the Spring Framework: declaring beans and is easy to do. ;)

Code for injecting bean

0 views0 comments

Recent Posts

See All

Aurora Holds Tech Talk At FAST-NU

Industrial seminars and workshops have always been fruitful for academia to understand the importance of specific technology and market trends of what is new and running. Such sessions provide advanta

Facebook Authentication for Spring Security in Grails

image In this article we will use Spring Security Facebook Plugin for Grails to integrate our grails application with facebook. By the end of this article you will be able to login in your grails web