How to inject value into Bean properties in Spring

With this example we shall show you how to inject values to Spring Beans properties. Spring’s XML-based configuration metadata supports sub-element types within its <property/> elements for this purpose.

Our preferred development environment is Eclipse. We are using Eclipse Juno (4.2) version, along with Maven Integration plugin version 3.1.0. You can download Eclipse from here and Maven Plugin for Eclipse from here. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. We are also using Spring version 3.2.3 and the JDK 7_u_21.

Let’s begin.

1. Create a new Maven project

Go to File -> Project ->Maven -> Maven Project.


In the “Select project name and location” page of the wizard, make sure that “Create a simple project (skip archetype selection)” option is checked, hit “Next” to continue with default values.


In the “Enter an artifact id” page of the wizard, you can define the name and main package of your project. We will set the “Group Id” variable to "com.javacodegeeks.snippets.enterprise" and the “Artifact Id” variable to "springexample". The aforementioned selections compose the main project package as "com.javacodegeeks.snippets.enterprise.springexample" and the project name as "springexample". Hit “Finish” to exit the wizard and to create your project.


The Maven project structure is shown below:


    It consists of the following folders:

  • /src/main/java folder, that contains source files for the dynamic content of the application,
  • /src/test/java folder contains all source files for unit tests,
  • /src/main/resources folder contains configurations files,
  • /target folder contains the compiled and packaged deliverables,
  • the pom.xml is the project object model (POM) file. The single file that contains all project related configuration.

2. Add Spring 3.2.3 dependency

  • Locate the “Properties” section at the “Overview” page of the POM editor and perform the following changes:
    Create a new property with name org.springframework.version and value 3.2.3.RELEASE.
  • Navigate to the “Dependencies” page of the POM editor and create the following dependencies (you should fill the “GroupId”, “Artifact Id” and “Version” fields of the “Dependency Details” section at that page):
    Group Id : org.springframework Artifact Id : spring-web Version : ${org.springframework.version}

Alternatively, you can add the Spring dependencies in Maven’s pom.xml file, by directly editing it at the “Pom.xml” page of the POM editor, as shown below:

<project xmlns=""; xmlns:xsi=""



As you can see Maven manages library dependencies declaratively. A local repository is created (by default under {user_home}/.m2 folder) and all required libraries are downloaded and placed there from public repositories. Furthermore intra – library dependencies are automatically resolved and manipulated.

3. Create a simple Spring Bean with properties

We create a simple Spring Bean, that is HelloWorld, and has two properties, id and name.


public class HelloWorld {
	private String id;

	private String name;
	public String getId() {
		return id;

	public void setId(String id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;
	public String toString(){
		return " HelloWorld! id: " + id + " \n name: " + name;


4. XML-based approach for value injection into bean properties

In order to inject the values to helloWorldBean we will use the <property/> element and its capabilities.

4.1 Using the value attribute of the <property/> element

The value attribute of the <property> element specifies a property as a String representation. This String representation is converted to the actual type of the property. The value attribute can be either be used as a tag enclosed inside the property tag, or as an attribute of property tag, as shown below:
applicationContext.xml value as tag:

<beans xmlns=""
	xmlns:xsi="" xmlns:p=""
	xmlns:aop="" xmlns:context=""
	xmlns:jee="" xmlns:tx=""
	xmlns:task="" xsi:schemaLocation="">
	<bean id="helloWorldBean" class="">
		<property name="id">
		<property name="name">


applicationContext.xml value as attribute of property tag:

	<bean id="helloWorldBean" class="">
		<property name="id" value="12345"/>
		<property name="name" value="John"/>

4.2 Using the p-namespace

The p-namespace enables as to use the beans element’s attributes instead of nested property elements to describe the property values.
The configuration file that uses namespaces is shown below:

	<bean id="helloWorldBean" class=""
	p:id="12345" p:name="John"/>

5. Run the application

Through the ApplicationContext the helloWorldBean is loaded to App.class.

package com.javacodegeeks.snippets.enterprise;

import org.springframework.context.ApplicationContext;


public class App {
	public static void main(String[] args) {
			ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
			HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorldBean");

6. Output

When you execute the application you should see something like the output presented below:

 HelloWorld! id: 12345 
 name: John

This was an example of how to inject a value into Bean properties in Spring 3.2.3.
Download the Eclipse project of this part :

Theodora Fragkouli

Theodora has graduated from Computer Engineering and Informatics Department in the University of Patras. She also holds a Master degree in Economics from the National and Technical University of Athens. During her studies she has been involved with a large number of projects ranging from programming and software engineering to telecommunications, hardware design and analysis. She works as a junior Software Engineer in the telecommunications sector where she is mainly involved with projects based on Java and Big Data technologies.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button