Thursday, November 22, 2018
Home > Spring > P namespace and C namespace in spring

P namespace and C namespace in spring

p namespace and c namespace in spring

p namespace and c namespace help us to reduce the configuration size of XML bean in both of the injection setter as well as Constructor Injection. These are configured as below:

p:propertyName=”  ” and  c:constructorName=”  ” .

Example:

p:department-ref=”department” p:name=”John” p:age=”26″

deparment,name and age are propertyName

c:deptName=”LA”

deptName is constructorName


Simple XML configuration  for class beans Employee and Department for respected property and constructor injection.

<bean id="department" class="beans.Department">
<constructor-arg value="LA"/>
</bean>

<bean id="employee" class="beans.Employee">
<property name="department" ref="department"/>
<property name="name" value="John"/>
<property name="age" value="26"/>
</bean>

XML configuration With p namespace and c namespace for class beans Employee and Department for respected property and constructor injection, we need to add some schema in <beans> tag as below.

Property injection:
xmlns:p=”http://www.springframework.org/schema/p”

Constructor injection:
xmlns:c=”http://www.springframework.org/schema/c”

As we can see in below configuration file our xml code is decreased as :

<bean id="department" class="beans.Department" c:deptName="LA"/> 
<bean id="employee" class="beans.Employee" p:department-ref="department" p:name="John" p:age="26"/>

Here is the complete configuration with p namespace & c namespace in configuration file.

<?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"
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="department" class="beans.Department" c:deptName="LA"/>
<bean id="employee" class="beans.Employee" p:department-ref="department" p:name="John" p:age="26"/>
</beans>

Example of p namespace and c namespace in Spring:

We are using Ecplise IDE and some required jars to develop below application.

Ecplise IDE : Ecplise Luna

Jars: Placed below jar in “Referenced Libraries” Folder

  1. commons-logging-1.2
  2. spring-beans-5.0.8.RELEASE
  3. spring-context-5.0.8.RELEASE
  4. spring-core-5.0.8.RELEASE
  5. spring-expression-5.0.8.RELEASE

To create this example, we have created 5 files inside src directory.

  1. Department.java
  2. Employee .java
  3. config.xml
  4. Test.java

Department.java

package beans;

public class Department {
	public String deptName;
	Department(String deptName)
	{
		this.deptName=deptName;
	}
}

Employee.java

package beans;

public class Employee {
	private Department department;
	private String name;
	private int age;
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

config.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:c="http://www.springframework.org/schema/c"
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="department" class="beans.Department" c:deptName="LA"/>
<bean id="employee" class="beans.Employee" p:department-ref="department" p:name="John" p:age="26"/>
</beans> 

Test.java

package test;

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

import beans.Employee;

final class Test {

	public static void main(String[] args) {
		ApplicationContext ap=new ClassPathXmlApplicationContext("resources/spring.xml");
		Employee e=(Employee)ap.getBean("employee");
		System.out.println(e.getName()+" "+e.getAge()+" "+e.getDepartment().deptName);
	}
}

OUTPUT
John 26 LA


Related Topics:-

You may also like :-
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *