Home  >  Spring Core

Example of p-namespace in Spring

By Arvind Rai, April 11, 2013
In spring, p-namespace is the XML short cut to inject dependency in bean. p-namespace replaces <property> tag of XML. p-namespace has no XSD definition and it only exits in core of spring. We can directly assign the attribute name of the class with p-namespace within bean tag. We can use p-namespace in place of <property> tag in spring XML. It is easier and clear to use that will increase readability of spring XML application context. Suppose we have <bean> definition in XML as follows.
<bean id="comp" class="com.concretepage.bean.Company">
	<property name="name" value="ABCD Ltd"/>
	<property name="location" value="India"/>
</bean>  
We can change <property> tag using p-namespace as follows.
<bean id="comp" class="com.concretepage.bean.Company" p:name="ABCD Ltd" p:location="India"/>
 

Use p-namespace

Find the XML file with p-namespace.
app-conf.xml
<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
      
      <bean id="comp" class="com.concretepage.bean.Company" p:name="ABCD Ltd" p:location="India"/>
      <bean id="emp" class="com.concretepage.bean.Employee" p:empName="Ram Jethmalani" p:company-ref="comp"/>
</beans> 

Create Bean

Find the beans used in the example.
Company.java
package com.concretepage.bean;
public class Company {
	private String name;
	private String location;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
} 
Employee.java
package com.concretepage.bean;
public class Employee {
	private String empName;
	private Company company;
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
} 

Run Application

SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Employee;
public class SpringDemo {
	public static void main(String[] args) {
	    AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");
	    Employee employee=(Employee)context.getBean("emp");
	    System.out.println(employee.getEmpName());	     
	    System.out.println(employee.getCompany().getName());		
	    context.close();
	}
} 
Find the output.
Ram Jethmalani
ABCD Ltd 
In the same way, for constructor-arg tag, spring provides c-namespace. Find the link.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us