Spring p Namespaces

Concept Overview

Spring’s P namespace is an alternative to using the
property
tag. By using p namespace, we can perform dependency injection by directly using the attribute of
bean
tag instead of using the
property
tag.

    The benefits of using p namespace are:

  • p namespace is more compact than property tag
  • Using p namespace reduces the amount of XML required in Spring configuration. The size of spring config using p namespace is typically less than one with using property tag

Trail Tip: It is very important to discuss upfront which of the approaches (property tag or p namespace) would be used in a project so that inconsistency of declaration is avoided.

In practice, most projects use the
property
tag. P namespaces are typically used in reference books where compactness of spring declaration is more valuable due to space constraints.

The following sample program provides an overview of usage of Spring p namespace

Sample Program Overview

In the sample program, we will create an ATM class and Printer class.

We will inject Printer class into ATM class using p namespace via setter injection.

The ATM.printBalanceInformation() method delegates the call to Printer.printBalanceInformation() method.

Finally, we will test our setup using TestSpringPNamespace class which will load Spring context and call the ATM.printBalanceInformation() method to verify that setter injection using p namespace has happened successfully.

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 ATM class with a dependency on Printer class.The ATM class delegates the call to print the balance information to the Printer class (see line 15 below).

Create a Printer class with a method printBalanceInformation() (see line 5 below)

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

Declare the p namespace (see line 3 below). This enables us to use p namespace within the spring-config.

Declare the ATM class in spring-config.xml (see line 12 below).

Use p namespace to inject Printer into ATM bean (see line 13 below). Note that the
-ref
postfix tells Spring to use the reference of the printer bean.

Declare the Printer class in spring-config.xml (see line 15 below).


Finally, we need a java program to test the our setup.This is done by TestSpringPNamespace.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 ATM class through Spring using the bean name ‘atm’ (see line 12 below). In this step Spring will resolve the dependency on Printer class (using p namespace) by injecting it using setter injection.


We call the printBalanceInformation() on ATM class (see line 14 below) with some accountNumber (see line 13 below). We will verify that the call is delegated to Printer.printBalanceInformation() method, implying the injection using p namespace has happened 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 springpnamespace-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


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