1 (p namespace) | 2 (c namespace)
The main purpose of p namespace is to remove the long <property> tags from xml configuration file. You can think p namespace as a kind of shortcut notation of <property> tags.
Consider following example:
Student.java
package di.pnamespace;
public class Student {
private String name;
private int age;
private Course course;
// setters and getters
public void displayInfo(){
System.out.println("Student Name : "+name);
System.out.println("Student Age : "+ age);
System.out.println("Course name : "+course.getCourseName());
}
}
public class Student {
private String name;
private int age;
private Course course;
// setters and getters
public void displayInfo(){
System.out.println("Student Name : "+name);
System.out.println("Student Age : "+ age);
System.out.println("Course name : "+course.getCourseName());
}
}
Course.java
package di.pnamespace;
public class Course {
String courseName;
public void setCourseName(String name){
this.courseName = name;
}
public String getCourseName(){
return courseName;
}
}
public class Course {
String courseName;
public void setCourseName(String name){
this.courseName = name;
}
public String getCourseName(){
return courseName;
}
}
MainApp.java
package di.pnamespace;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Student stud = (Student)appContext.getBean("student");
stud.displayInfo();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Student stud = (Student)appContext.getBean("student");
stud.displayInfo();
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="di.pnamespace.Student">
<property name="name" value="abc"/>
<property name="age" value="30"/>
<property name="course" ref="course1"/>
</bean>
<bean id="course1" class="di.pnamespace.Course">
<property name="courseName" value="computers"/>
</bean>
</beans>
In the above program dependencies of Student and Course class have been injected using <property> tags.
Now the same program using p namespace is (you only need to change ApplicationContext.xml, other classes remain same):
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="di.pnamespace.Student" p:name="abc" p:age="31" p:course-ref="course1"/>
<bean id="course1" class="di.pnamespace.Course" p:courseName="computers"/>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="di.pnamespace.Student" p:name="abc" p:age="31" p:course-ref="course1"/>
<bean id="course1" class="di.pnamespace.Course" p:courseName="computers"/>
</beans>
In the above program we are using p namespace to inject the dependencies of Student and Course classes.
For example,
p:age="31" is substitute of:
<property name="age" value="31">
Note how we are injecting reference to other bean as a value of property -course of Student class, using p namespace:
p:course-ref="course1"
is a substitute of
<property name="course" ref="course1">
Note the -ref at the end of p:course-ref="course1". -ref means this property would accept a reference to other bean as its value.
Also note that unlike <property> tags, p namespace are not separate tags but they are the part of <bean> tag
For p namepsace to work you have to add its schema URI https://www.springframework.org/schema/p in above xml file.
1 (p namespace) | 2 (c namespace)
I would like to know your comments and if you liked the article then please share it on social networking buttons.
Any way to pass null using the c: or p: namespaces? Normal syntax allows as a value, but that doesn't work with these shortcuts.
ReplyDelete