P-Namespace and C-Namespace
In this tutorial, we are going to discuss P-Namespace and C-Namespace in Spring framework.
P-Namespace
In general, in the setter method dependency injection, to specify dependent values in the spring configuration file, we have to use <property> tags as per the number of properties in the bean class. In this context, to remove <property> tags and to provide dependent values as attributes in tag in the spring configuration file, we have to use “P-Namespace”.
In simple words, while using Spring XML configuration for wiring beans, you would have used <property> element several times to provide property values and/or reference for beans. If you are looking for any shorter alternative to nested <property> element, you can use p-namespace in Spring.
Note
To use p-namespace in the spring configuration file, we must define the “p” namespace in XSD like below.
xmlns:p="http://www.springframework.org/schema/p"
To provide value as attribute by using “p” namespace in <bean> tag we have to use the following syntax.
<bean id="--" class="--" p:prop_Name="value" p:prop_Name="value".../>
If we want to specify object reference variable as dependent value the we have to use “-ref” along with property.
<bean id="--" class="--" p:prop_Name-ref="ref"/>
C-Namespace
In general, in constructor dependency injection, to specify dependent values in the spring configuration file, we have to use <copnstructor-arg> tags as per the no of parameters that we defined in the bean class constructor. In this context, to remove <copnstructor-arg> tags and to provide dependent values as attributes in tag in the spring configuration file, we have to use “C-Namespace”.
The c-namespace in Spring enables you to use the bean element’s attributes for configuring the constructor arguments rather than nested constructor-arg elements.
Note
To use c-namespace in the spring configuration file, we must define the “c” namespace in XSD like below.
xmlns:c="http://www.springframework.org/schema/c"
We must use the following syntax to provide value as an attribute by using the “c” namespace in the <bean> tag.
<bean id="--" class="--" c:arg_Name="value" c:arg_Name="value".../>
If we want to specify object reference variable as dependent value then we have to use “-ref” along with argument_Name.
<bean id="--" class="--" c:arg_Name-ref="ref"/>
If we want to specify dependent values in the beans configuration file based on index values, we have to use XML code like below.
<bean id="--" class="--" c:_0="val1" c:_1="val2"...c:_4-ref="ref"/>
package com.ashok.spring.core.bean.dependencyinjection.namespace.p.beans;
/**
*
* @author Ashok Kumar
*
*/
public class Employee {
private String empName;
private String empId;
private String empAddress;
private double salary;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
+ "]";
}
}
<?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-3.0.xsd">
<bean id="emp"
class="com.ashok.spring.core.bean.dependencyinjection.namespace.p.beans.Employee"
p:empName="Ashok Kumar" p:empId="Emp0087" p:empAddress="Bhimavaram"
p:salary="45000" />
</beans>
package com.ashok.spring.core.bean.dependencyinjection.namespace.p.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ashok.spring.core.bean.dependencyinjection.namespace.p.beans.Employee;
/**
*
* @author Ashok Kumar
*
*/
public class TestSpringApplication {
@SuppressWarnings("resource")
public static void main(String[] args) {
String configFile = "/com/ashok/spring/core/bean/dependencyinjection/namespace/p/config/applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
Employee emp = (Employee) context.getBean("emp");
System.out.println(emp);
}
}
Output
Employee [empName=Ashok Kumar, empId=Emp0087, empAddress=Bhimavaram, salary=45000.0]
E.g 2
package com.ashok.spring.core.bean.dependencyinjection.namespace.c.beans;
/**
*
* @author Ashok Kumar
*
*/
public class Employee {
private String empName;
private String empId;
private String empAddress;
private double salary;
public Employee(String empName, String empId, String empAddress, double salary) {
super();
this.empName = empName;
this.empId = empId;
this.empAddress = empAddress;
this.salary = salary;
}
@Override
public String toString() {
return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
+ "]";
}
}
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="emp"
class="com.ashok.spring.core.bean.dependencyinjection.namespace.c.beans.Employee"
c:empName="Ashok Kumar" c:empId="Emp0087" c:empAddress="Bhimavaram"
c:salary="45000" />
</beans>
package com.ashok.spring.core.bean.dependencyinjection.namespace.c.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ashok.spring.core.bean.dependencyinjection.namespace.c.beans.Employee;
/**
*
* @author Ashok Kumar
*
*/
public class TestSpringApplication {
@SuppressWarnings("resource")
public static void main(String[] args) {
String configFile = "/com/ashok/spring/core/bean/dependencyinjection/namespace/c/config/applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
Employee emp = (Employee) context.getBean("emp");
System.out.println(emp);
}
}
Output
Employee [empName=Ashok Kumar, empId=Emp0087, empAddress=Bhimavaram, salary=45000.0]