Spring Simple Value Injection

Concept Overview

It is possible to inject simple values like String, Integer, etc. into a bean.

This is done using the
attribute of
tag in Spring configuration xml.

The following sample program provides an overview of injecting simple values into a bean.

Sample Program Overview

In the sample program, we will create a Person bean with name and age as members exposed via accessor methods.

We will configure the spring-config.xml file and assign the value ‘Alba’ to the name and ’20’ to age property.

Finally, we will test our setup using TestSimpleValueInjection class which will load Spring context and get a reference to Person class. We will print the Person class and the output shall display the values ‘Alba’ and ’20’ corresponding to the name and age of the Peron, thereby successfully verifying the injection of simple values.

Note: Although injection of simple values is demonstrated in the sample program using setter injection, it is also possible to use contructor injection for the same.

Required Libraries
  • commons-logging.jar
  • log4j.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.beans.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.core.jar
  • org.springframework.expression.jar

Source Code

Create a Person class (see below).

Create two members called ‘name’ and ‘age’ of type
respectively (see lines 5-6 below).

Provide access methods for ‘name’ and ‘age’ so that setter injection can be performed (see lines 7-18 below).

Override the toString() method to display ‘name’ and ‘age’ (see lines 20-22 below)

Create the spring-config.xml file (see below).

Declare bean for Person class (see line 10 below)

Use the
attribute of
tag to inject the value ‘Alba’ corresponding to property ‘name’ (see line 11).

Use the
attribute of
tag to inject the value ’20’ corresponding to property ‘age’ (see line 12). Note that even though, the type of ‘age’ member is
, Spring automatically converts the value ’20’ into the appropriate type.

Finally, we need a java program to test the our setup.This is done by TestSimpleValueInjection.java (see source code below).

We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans(see line 11 below).

We get the reference to Person class through Spring using the bean name ‘person’ (see line 12 below).

We output the Person object to see the values ‘Alba’ and ’20’ corresponding to ‘name’ and ‘age’ verifying that injection of simple values has executed successfully.

Running Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run Sample Program
  • Save the springsimplevalueinjection-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment

  • (Alternatively you can go the folder containing the springsimplevalueinjection-installer.jar and execute the jar using
    java -jar springsimplevalueinjection-installer.jar

  • You will see a wizard as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

    Leave a Comment