Tuesday, 20 December 2016

Using p-namespace for shorter XML in Spring

If you have worked on Spring and used 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.

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

For 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>

Full 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 Using p-namespace for shorter XML in Spring. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. What is Dependency Injection in Spring
  2. Dependency Injection using factory-method in Spring
  3. Using Spring profiles to switch environment
  4. How to read properties file in Spring Framework
  5. Autodiscovery of bean using componenent-scan in Spring
  6. Difference between component-scan and annotation-config in Spring

You may also like -

>>>Go to Spring tutorial page

2 comments:

  1. Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks.

    web design company in chennai

    ReplyDelete