Java Example of Spring Dependency Injection

05 Jul

This tutorial uses the following:

    • Eclipse IDE
    • Spring Jars
    • commons-logging-api-1.1.jar
  1. Make a new Java Project in Eclipse.
  2. Add the following jars to its classpath:
    • commons-logging-api-1.1.jar
    • org.springframework.core-3.0.2.jar
    • org.springframework.beans-3.0.2.jar

The Spring jars can be taken from the dist folder of the expanded zip file. Here you have to include the two jars one for core and other for beans. Commons-logging is used for logging purpose.

  • Make a new class – It is a 100% Java bean and has the proper getters and setters.
package com.spring.ioc.beans;

public class Person {

	private String name;

	private String address;

	 * @return the name
	public String getName() {
		return name;

	 * @param name the name to set
	public void setName(String name) { = name;

	 * @return the address
	public String getAddress() {
		return address;

	 * @param address the address to set
	public void setAddress(String address) {
		this.address = address;


  • Make an XML file and name it context.xml and save it in the src folder.  This file contains all the meta information needed for bean creation. Here the bean id is person and class has the full qualified name of the class. The “name” property of the class Person is populated with the value Mac.

<?xml version="1.0" encoding="UTF-8"?>
	xmlns:xsi="" xmlns:p=""
	<bean id="person" class="com.spring.ioc.beans.Person">
	<property name="name" value="Mac"></property>

  • Make another class named  Here an object of XmlBeanFactory loads the configurations from contect.xml file. Also its constructor takes an argument of ClassPathResource.
  • To get a bean from XmlBeanFactory object use its reference type to invoke the method getBean(<bean id>) and it will return the bean as defined in the spring context file.  The Object it returns is of type Object so you do have to cast it to the correct type.
package com.spring.ioc.main;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import com.spring.ioc.beans.Person;

 * @author hgupta
public class Main {

	 * @param args
	public static void main(String[] args) {
		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
		Person person = (Person) beanFactory.getBean("person");


  • Run the Main class as Java Application and you will see the following output: The output is the name of the person which you configured in the xml file.


May 27, 2010 6:54:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [context.xml]


Leave a comment

Posted by on July 5, 2010 in Java, Spring


Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: