Spring Injection – Properties

Concept Overview

Sometimes, it is necessary to inject a Collection of type
java.util.Properties
into a bean using Spring injection. e.g. A Car has many technical specifications associated with in the form of name-value pairs (e.g. Weight, Maximum Torque, No. of cylinders, etc).

This is achieved by using the
props
tag within the
property
tag in Spring configuration.

Note: Injecting properties is similar to inject
Map
. The only difference is that in
Properties
, only
String
names and values are allowed while in
Map
, object of any type can be used as key and values.

The following sample program provides an overview of injecting the car specification properties into the Car bean.

Sample Program Overview

We will create a Car class.

We will also create members of car called chassisNumber and name.

Additionally, we will define a member of Car class called ‘specifications’ of type
java.util.Properties
.

Using Spring’s configuration, we will inject some name-value properties into ‘specifications’. (e.g. cylinder=>4, bodyType=>Sedan, maxTorque=>465Nm @ 5250 rpm and weight=>1500 Kg)

Finally, we will test our setup using TestInjectionProperties class which will load Spring context and get a reference to Car class. We will fetch the specification from Car class and print them in the console to verify that the name-value defined in the
Properties
have been successfully injected.

Note:

  • Although we have used setter injection in this example, it is also possible to inject java.util.Properties using constructor injection.
  • Although we used simple types within the list using value tag, we can also use bean references within list using ref tag.

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 the Car class (see below).

Create members called chassisNumber and name (see lines 7-8 below) along with their accessor methods (see lines 10-21 below).

Create a member called ‘specifications’ of type
java.util.Properties
(see line 9 below)

Create accessor methods for ‘specifications’ (see lines 22-27 below)

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

Declare bean for Car class (see line 10 below)

Define values for ‘chassisNumber’ and ‘name’ (see line 11-12 below).

For the ‘specifications’ property (see line 14 below), we define a
props
tag (see line 15 below).

We populate the keys and values of the Properties using the
prop
tag (see lines 16-19 below).


Finally, we need a java program to test the our setup.This is done by TestInjectionProperties.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 Car class through Spring using the bean name ‘car’ (see line 12 below).

We access the Car.getSpecifications() method and output the contents to verify that the properties have been injected successfully (see line 13 below).


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 springinjectionproperties-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


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

  • 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
    springinjectionproperties
    . 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