728x90 AdSpace

Wednesday, 9 April 2014

Spring Setter DI – Injecting Primitive and String-based values

In this tutorial we are going to see about Spring Dependency Injection to inject Primitive and String based values using Setter-Injection.
Note : JavaBeans PropertyEditors are used to convert the string values from a String to the actual type of the property or argument
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringSetterDI.
3. Add the following jars into your build path.
4. Now create package com.javatutorialscorner.spring under SpringSetterDI Project
5. Next create PrimitiveSetterDI class under com.javatutorialscorner.spring package
package com.javatutorialscorner.spring;

public class PrimitiveSetterDI {

private int id;
private String name;
private String job;
private double salary;

public void getId() {
System.out.println("int value ID :" + id);

public void setId(int id) {
this.id = id;

public void getName() {
System.out.println("String value name :" + name);

public void setName(String name) {
this.name = name;

public void getJob() {
System.out.println("String value job :" + job);

public void setJob(String job) {
this.job = job;

public void getSalary() {
System.out.println("double value salary :" + salary);

public void setSalary(double salary) {
this.salary = salary;


6. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file


package com.javatutorialscorner.spring;

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

public class RunApp {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

ApplicationContext context = new ClassPathXmlApplicationContext(
PrimitiveSetterDI setterDI = (PrimitiveSetterDI) context



In above program

First step I created Application context using ClassPathXmlApplicationContext  which load configuration from bean file  which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml

Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.

Once Object Created you can access any method from that class.

7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="primitiveSetterDI" class="com.javatutorialscorner.spring.PrimitiveSetterDI">
<property name="id" value="5005"></property>
<property name="name" value="Appu.T"></property>
<property name="job" value="Software Engineer"></property>
<property name="salary" value="45000.00"></property>


The value attribute of the <property/> element specifies a property or constructor argument as a human-readable string representation. JavaBeans PropertyEditors are used to convert these string values from a String to the actual type of the property or argument

Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.

Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to  variables  without changing the class file.

Now you can run the program see the following output in console.

int value ID :5005
String value name :Appu.T
String value job :Software Engineer
double value salary :45000.0
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Spring Setter DI – Injecting Primitive and String-based values Rating: 5 Reviewed By: Annamalai Thangaraj