Home » Spring » Injecting primitive and string-based values by setter method

Injecting primitive and string-based values by setter method

The <property> subelement of <bean> is used for setter injection. In this example we are going to inject primitive and String-based values.


Files required..

  • Employee.java
  • applicationContext.xml
  • Test.java


Tools and Technologies used in this article :

  • Spring
  • Java
  • Eclipse

1. Create a java project in Eclipse

Open Eclipse –> File –> New –> and Select Java project and click on next


Spring Hello World Setter Injection


Assign the name of the project as "SpringSetterDI"

Spring Setter Injection


2. Add jar files to classpath of Project

Right click on the project and create a folder and name it to lib. After creation of lib folder copy spring-2.5.6.jar and commons-logging-1.1.1.jar files into this folder and add these jars to class path of the project.commons-logging and spring jar are required for Spring based application.

3. Create Java class

Create a java class Employee.java and add following code into this class.

It is a simple class containing three fields id, name and city with its setters and getters and a method to display these informations.

Employee.java

package com.javawebtutor.spring;

public class Employee {
	private int id;
	private String name;
	private String city;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	void details() {
		System.out.println(id + " " + name + " " + city);
	}
}

4. Create Spring bean configuration file

Create an xml file (applicationContext.xml) in “src “. This is the Spring’s bean configuration file, which declares all the available Spring beans.

We are providing the information into the bean by this file. The property element invokes the setter method. The value sub element of property will assign the specified value.

applicationContext.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
            "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="helloBean" class="com.javawebtutor.spring.Employee"> 
<property name="id"> 
<value>01</value> 
</property> 
<property name="name"> 
<value>Mukesh</value> 
</property> 
<property name="city"> 
<value>Bangalore</value> 
</property>   
</bean>   
</beans>

5. Create Test Class

This class gets the bean from the applicationContext.xml file and calls the displayMessage() method.

Test.java

package com.javawebtutor.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

	public static void main(String[] args) {
		Resource resource = new ClassPathResource("applicationContext.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		Object obj = factory.getBean("helloBean");
		Employee employee = (Employee) obj;
		employee.details();
	}
}

6. Run It

Right click on the Test.java and select Run as Java Application, you can see following output in console.

Sep 9, 2014 8:47:35 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
1 Mukesh Bangalore

You can download the source code of the example by clicking on the Download link below.

Download


Previous Next Article
comments powered by Disqus