p-Namespace
In general, in setter method dependency injection, to specify dependent values in spring configuration file we have to use <property> tags as per the no of properties in the bean class. In this context, to remove <property> tags and to provide dependent values as attributes in <bean> tag in spring configuration file we have to use “P-Namespace”.
Note
To use p-namespace in spring configration file we have to define “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 spring configuration file we have to use <copnstructor-arg> tags as per the no of parameters which we defined in the bean class constructor. In this context, to remove <constructor-arg> tags and to provide dependent values as attributes in <bean> tag in spring configuration file we have to use “C-Namespace”.
Note
To use c-namespace in spring configration file we have to define “c” namespace in XSD like below.
xmlns:c="http://www.springframework.org/schema/c"
To provide value as attribute by using “c” namespace in <bean> tag we have to use the following syntax.
<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 beans configuration file on the basis of index values then 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]