Spring Auto-Wiring Using JSR 330 Annotations: Wiring String using @Value

Pre-requisite trails

Spring Auto-Wire Using JSR 330 Annotations

Concept Overview

Typically in Spring, dependency injection is achieved using
bean
,
constructor-arg
and
property
tags.

However, in large applications, the number of beans will increase and the corresponding XML written to configure the numerous beans will become very large and unwieldly.

Spring provides a feature called ‘Auto-Wiring’ that minimizes the XML to be written provided that certain assumptions are made about the nomenclature of beans and properties. Using ‘Auto-Wiring’ feature, the number of tags like
constructor-arg
and
property
is significantly minimized and the size of XML is reduced considerably for large applications.

Another driving factor is the emergence of number of dependency injection containers besides Spring like Pico-Container, Guice, etc. An effort is underway to standardize the way annotation based auto-wiring is supported by these dependency injection containers so that the application code is loosely coupled to these containers.

The standardization of annotation based dependency injection is provided by
JSR 330
.

Spring provides auto-wiring based on both XML and JSR 330 Annotations like
@Inject
and
@Named
.

In this tutorial we will look into auto-wiring based on
@Value
annotation defined in JSR 330 which is used to inject String values into members of a bean.

Auto-Wiring based on JSR 330 annotations is achieved as follows:

  • Use the context:annotation-config tag in spring-context.xml
  • Use the @Value annotation defined in JSR 330 to qualify either the member or a corresponding method (usually the setter method) of type String.

Sample Program Overview

The sample program describes the wiring of String values into the Person class.

We will create the Person class with members as name and email.

We will also create the spring-config.xml and declare the person bean.

We will use the
context:annotation-config
tag in spring-context.xml to let Spring know that annotation based auto-wiring will be used.

We will then use Spring’s auto-wiring feature and use
@Value
annotation to inject values of name and email into Person bean.

Finally, we will test our setup using TestAutoWireAnnotationValueJSR330 class which will load Spring context and get a reference to Person class. We will print name and email of the Person bean on the console to verify that auto-wiring of
String
type using
@Value
annotation has occured successfully.

Required Libraries
  • com.springsource.javax.inject-0.9.0.PFD.jar
  • 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 Person (see sample code below).

Create members name and email (see lines 8-9 below)

Create accessor methods for name and email (see lines 14-23 below).

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

Let Spring know that annotation based auto-wiring will be used by declaring the tag
context:annotation-config
tag(see line 14 below)

Declare bean for Person (see lines 16-18 below)

Note that no dependency injection is configured in spring-config.xml file

Note that we did not declare the
property
tag with
bean
tag of ATM thereby reducing the XML size.


Finally, we need a java program to test the our setup.This is done by TestAutoWireAnnotationValueJSR330 (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 access the getName() and getEmail() methods of Person class and print the output to verify that auto-wiring of
String
type using
@Value
annotation has occured successfully (see lines 13-14 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 springautowireannotationvaluejsr330-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springautowireannotationvaluejsr330-installer.jar and execute the jar using
    java -jar springautowireannotationvaluejsr330-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
    springautowireannotationvaluejsr330
    . 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