Spring p-namespace with Example
Prerequisite: How to Create a Spring Bean in 3 Different Ways?
In this article, we are going to discuss the Spring p-namespace. Here I am going to assume that you know about How we can create Bean inside an XML configuration file. The first question that will come to your mind is what is Spring p-namespace and how it will be useful in creating Bean?
One of the most popular ways to create a spring bean is to define a bean in an XML configuration file. To understand the use of Spring p-namespace, we have to go through the traditional way of creating a bean in an XML configuration file. Let’s create a bean for the java class below:
Java
package com.gfg.scripter; public class Coder { private int id; private String name; private String qualification; private String dob; public void displayBasicInfo() { System.out.println( "Coder name is " + name + " and Id is " + id); } // Getters & Setters } |
Now we have to create an XML file named app-config.xml file and add it to the project classpath like below :
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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> < bean id = "coderTanu" class = "com.gfg.scripter.Coder" > < property name = "id" value = 100 /> < property name = "name" value = "Tanu Jain" /> < property name = "qualification" value = "B.Tech - CSE" /> < property name = "dob" value = "27-07-1996" /> </ bean > </ beans > |
As you see, for each instance variable we have to use one xml tag i.e. <property> tag to inject bean dependency. So here is how Spring p-namespace will help us. In Spring XML, p-namespace is the XML shortcut for <property> tag to inject bean dependency. The p-namespace replaces <property> tag in XML configuration. Because of Spring p-namespace, the length of xml code will be reduced and it increases the readability of XML configuration. Let’s see how :
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 = "coderTanu" class = "com.gfg.scripter.Coder" p:id = 100 p:name = "Tanu Jain" p:qualification = "B.Tech - CSE" p:dob = "27-07-1996" /> </ beans > |
Note: To use Spring p-namespace, It needs to be declared with xmlns:p somewhere within the XML element or a parent element of which they are being used. It is not mandatory to use p. These are simply being used by convention. You could rewrite your XML to use anything – as long as they are defined to map to the same XML namespaces. To enable the p-namespace feature, we need to add the xmlns:p=”http://www.springframework.org/schema/p” and refer to p-namespace using p to inject any dependency. Let’s run a simple spring project to see the result using the above xml configuration :
Java
package com.gfg.scripter; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gfg.scripter.Coder; public class BeginnerDemo { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext( "app-config.xml" ); Coder coder=(Coder)context.getBean( "coderTanu" ); System.out.println( "Id :" +coder.getId()+ "Name :" +coder.getName()); context.close(); } } |
Output:
Id : 100 Name : Tanu Jain
Note: We discussed passing the dependency through p-namespace but suppose we want to pass the reference of another bean, typically we use the ref attribute of <property/> tag but through p-namespace, we can do it via. p:[property-name]-ref=”value” attribute just like below
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 = "company" class = "com.gfg.scripter.Company" p:name = "Accenture" p:location = "India" /> < bean id = "employee" class = "com.gfg.scripter.Employee" p:empName = "Tanu Jain" p:company-ref = "company" /> </ beans > |
In this tutorial, we have shown how to use property-based injection with p-namespace.
Contact Us