Spring p-namespace example


In the Spring framework, p-namespace is used to inject setter-based dependency. The p-namespace is XML shortcut and reduce the numbers of line in the configuration file. However, the p-namespace is not defined in an XSD file and exists only in the core of Spring.

Similar Post: Spring c-namespace example

The following example shows two XML snippets that resolve to the same result: The first uses standard XML format and the second uses the p-namespace.

<!—traditional style -->
<bean id="oldTeacher" class="beans.Teacher">
    <property name="name" value="Vijay Pnadey" />
    <property name="qualification" value="PhD" />
</bean>

<!—new style -->
<bean id="newTeacher" class="beans.Teacher" p:name="Vijay Pandey" p:qualification="PhD"  />

And to pass the reference of another bean, typically we use the ref attribute of <property/> tag but by using p-namespace, use the p:[property-name]-ref="value" attribute. In the following snippets first uses standard XML format and the second uses the p-namespace.

<!-- old concept -->
<bean id="oldTeacher" class="beans.Teacher">
    <property name="name" value="Vijay Pnadey" />
    <property name="qualification" value="PhD" />
    <property name="student" ref="oldStudent" />
</bean>

<bean id="oldStudent" class="beans.Student">
    <property name="name" value="Atul" />
    <property name="course" value="B.Sc" />
</bean>

<!-- new concept -->
<bean id="newTeacher" class="beans.Teacher" p:name="Vijay Pandey"
      p:qualification="PhD" p:student-ref="newStudent" />

<bean id="newStudent" class="beans.Student" p:name="Atul" p:course="B.Sc" />

Before proceeding next, you have to check the important note from Spring documentation.

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. We recommend that you choose your approach carefully and communicate this to your team members, to avoid producing XML documents that use all three approaches at the same time.

Let’s check the complete example.

Dependency Required

Add the following dependency into your pom.xml file.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
</dependencies>

Spring Beans

Create the bean’s classes and generate the setter method of all declared parameter and the business logic that actually uses the injected values.

Student.java
package beans;

public class Student {

	// Setters and Getters....
	private String name;
	private String course;
}
Teacher.java
package beans;

public class Teacher {

	// Generate setters....
	private String name;
	private String qualification;
	private Student student;

	public void setName(String name) {
		this.name = name;
	}

	public void setQualification(String qualification) {
		this.qualification = qualification;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	// business logic
	public void display() {
		System.out.println("Teacher and Student details...........\n");
		System.out.println("Teacher name: " + name);
		System.out.println("Teacher qualification: " + qualification);
		System.out.println("Student name: " + student.getName());
		System.out.println("Student course: " + student.getCourse());
	}
}

Spring Beans Configuration

Add p-namespace schema into root tag beans.

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

	<!-- setter-based dependency injection using p-namespace -->

	<bean id="newTeacher" class="beans.Teacher" p:name="Vijay Pandey" p:qualification="PhD" p:student-ref="newStudent" />

	<bean id="newStudent" class="beans.Student" p:name="Atul" p:course="B.Sc" />

</beans>

Run it

Load the configuration file and run it.

Principal.xml
package clients;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.Teacher;

public class Principal {
	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		Teacher tchr = (Teacher) context.getBean("newTeacher");
		tchr.display();
	}
}
Output:

You will get the following result in your console log.

Teacher and Student details...........

Teacher name: Vijay Pandey
Teacher qualification: PhD
Student name: Atul
Student course: B.Sc

Download Source Code: spring-p-namespace-example


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.