Spring p-namespace with Example
Last Updated :
26 Apr, 2025
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 GeeksDemo {
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.
Similar Reads
Spring c-namespace with Example
Prerequisite: How to Create a Spring Bean in 3 Different Ways? The Spring c-namespace will be discussed in this article. We are going to assume you're familiar with how to create a Bean within an XML configuration file. The first question that comes to mind is what Spring c-namespace is and how it w
3 min read
Spring MVC CRUD with Example
In this article, we will explore how to build a Spring MVC CRUD application from scratch. CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic operations to create any type of project. Spring MVC is a popular framework for building web applications. Spring MVC follows
7 min read
Spring Boot JpaRepository with Example
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
9 min read
Spring - ResourceLoaderAware with Example
Prerequisite: Introduction to Spring Framework In this article, we will discuss the various ways through which we can load resources or files (e.g. text files, XML files, properties files, etc.) into the Spring application context. These resources or files may be present at different locations like
4 min read
Spring with Castor Example
With the use of CastorMarshaller class, we can achieve marshal a java object into XML code and vice-versa of it with the help of using castor. The castor is the implemented class for Marshaller and Unmarshaller interfaces within it, thus it does not require other further configurations by its defaul
3 min read
Spring Boot - RestTemplateBuilder with Example
Before jumping into RestTemplateBuilder let's have a quick overview of RestTemplate. RestTemplate is a synchronous REST client which performs HTTP requests using a simple template-style API. We can also state that RestTemplate class is a synchronous client and is designed to call REST services. Rest
12 min read
Data Binding in Spring MVC with Example
Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
8 min read
Spring @ResponseBody Annotation with Example
Spring Annotations allow us to configure dependencies and implement dependency injection through java programs. Those are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compil
4 min read
Spring @RequestMapping Annotation with Example
The @RequestMapping annotation in Spring MVC is one of the most important annotations used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to the handler method
4 min read
Spring @Required Annotation with Example
Spring Annotations provide a powerful way to configure dependencies and implement dependency injection in Java applications. These annotations act as metadata, offering additional information about the program. The @Required annotation in Spring is a method-level annotation used in the setter method
5 min read