Thursday, June 17, 2021

Spring p-namespace For Shorter XML Configuration in Spring

While using Spring XML configuration for wiring beans you would have used <property> element several times for providing property values and/or providing reference for beans. If you are looking for any shorter alternative to nested <property> element you can use p-namespace in Spring.

The p-namespace enables you to use the bean element’s attributes, instead of nested <property/> elements.

As example–

If you have a XML configuration for bean employee with property officeAddress of type Address which refers to another bean address like this–

<bean id="employee" class="org.netjs.model.Employee">
    <property name="officeAddress" ref="address"/>
</bean>
You can use p-namespace in Spring to shorten it like this–
<bean id="employee" class="org.netjs.model.Employee" p:officeAddress-ref="address">
    <!-- <property name="officeAddress" ref="address"/> -->
</bean>

You can see here that instead of using nested <property> element you can use bean element’s attribute itself.

In this way of using p-namespace for injection of bean reference p:officeAddress-ref="address", p: denotes the use of p-namespace, officeAddress is the property name with in the employee class where value is injected, -ref indicates injection of bean reference.

Using p-namespace with literals

Same way p-namespace can be used for injecting literal values.

As example

If you have a XML configuration for bean employee which has a property empName then you can provide a literal value to empName property like this-

<bean id="employee" class="org.netjs.model.Employee">
    <property name="empName" value="Ram"/>
</bean>

Using p-namespace you can shorten it like this–

<bean id="employee" class="org.netjs.model.Employee" p:empName="Ram">
    <!-- <property name="empName" value="Ram"/> -->
</bean>

Spring p-namespace example

As we have already seen there are two classes Address and Employee where Address class bean is referred in Employee.

Address class

public class Address {
 private String number; 
 private String street; 
 private String city; 
 private String state; 
 private String pinCode; 
 public String getNumber() {
  return number;
 }
 public void setNumber(String number) {
  this.number = number;
 }
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 public String getPinCode() {
  return pinCode;
 }
 
 public void setPinCode(String pinCode) {
  this.pinCode = pinCode;
 }
}

Employee class

public class Employee {
 private int empId;
 private String empName;
 private int age;
 private Address officeAddress;
 
 public Address getOfficeAddress() {
  return officeAddress;
 }
 public void setOfficeAddress(Address officeAddress) {
  this.officeAddress = officeAddress;
 }
 public int getEmpId() {
  return empId;
 }
 public void setEmpId(int empId) {
  this.empId = empId;
 }
 public String getEmpName() {
  return empName;
 }
 public void setEmpName(String empName) {
  this.empName = empName;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}

XML Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">
   
  <bean id="address" class="org.netjs.model.Address" p:number="101" p:street="M I Road" 
    p:city="Jaipur" p:state="Rajasthan" p:pinCode="302001"> 
  </bean>
  <bean id="employee" class="org.netjs.model.Employee" p:empId="1001" p:age="25" 
    p:empName="Ram" p:officeAddress-ref="address">
    <!-- <property name="empName" value="Ram"/> -->
  </bean>   
</beans>

Here notice the inclusion of xmlns:p="http://www.springframework.org/schema/p in XML configuration for p-namespace.

Test Class

You can run this code using the following code-
public class App {
 public static void main(String[] args) {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext
    ("appcontext.xml");
  Employee emp = (Employee)context.getBean("employee");  
  Address addr = emp.getOfficeAddress();
  System.out.println("Name " + emp.getEmpName());
  System.out.println("Age " + emp.getAge());
  System.out.println("city " + addr.getCity());
  context.close(); 
 }
}

Output

Name Ram
Age 25
city Jaipur

Points to remember

  1. The p-namespace is not as flexible as the standard XML format. For example, the format for declaring property references clashes with properties that end in Ref, whereas the standard XML format does not. If you want to reference bean named address and you also have the property named as address-ref then it will result in a clash if you use p-namespace.
  2. You can’t use p-namespace while wiring a collection.

That's all for this topic Spring p-namespace For Shorter XML Configuration in Spring. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page


Related Topics

  1. Using c-namespace in Spring
  2. Spring Profiles With Examples
  3. registerShutdownHook() Method in Spring Framework
  4. How to Inject Prototype Scoped Bean into a Singleton Bean in Spring
  5. Lazy Initialization in Spring Using lazy-init And @Lazy Annotation

You may also like-

  1. Wiring Collections in Spring
  2. How to Read Properties File in Spring Framework
  3. Difference Between component-scan And annotation-config in Spring
  4. Configuring DataSource in Spring Framework
  5. How to Sort ArrayList of Custom Objects in Java
  6. How HashMap Works Internally in Java
  7. Callable and Future in Java With Examples
  8. Is String Thread Safe in Java