Spring c-namespace example
Spring c-namespace
is used to inject the constructor-based dependency injection. It is similar to p-namespace as described previous tutorial. The c-namespace
is XML shortcut and replacement of <constructor-arg/>
subelement of <bean/>
tag. It also reduces the numbers of line in the configuration file and introduced in Spring 3.1.
The following example shows two XML snippets that resolve to the same result: The first uses standard XML format and the second uses the c-namespace
.
<!-- old style -->
<bean id="oldboss" class="beans.Boss">
<constructor-arg name="name" value="Atul Rai" />
<constructor-arg name="employee" ref="oldemp" />
</bean>
<bean id="oldemp" class="beans.Employee">
<constructor-arg name="empName" value="Mukul" />
<constructor-arg name="empSalary" value="1000000" />
</bean>
<!-- new style -->
<bean id="newboss" class="beans.Boss" c:name="Atul Rai" c:employee-ref="newemp" />
<bean id="newemp" class="beans.Employee" c:empName="Mukul" c:empSalary="1000000" />
Note: In case of an overloaded constructor, just pass the parameter name and Spring will find out its type.
Dependency Required
To resolve the dependency problem, add the following to your pom.xml file.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
Or directly add these JARs to your project build path.
- spring-context-5.0.2.RELEASE.jar
- spring-aop-5.0.2.RELEASE.jar
- spring-beans-5.0.2.RELEASE.jar
- spring-core-5.0.2.RELEASE.jar
- spring-expression-5.0.2.RELEASE.jar
Spring Beans
Create the bean classes and its parameterized constructor.
package org.websparrow.beans;
public class Employee {
// Generate getters....
private String empName;
private int empSalary;
public Employee(String empName, int empSalary) {
this.empName = empName;
this.empSalary = empSalary;
}
public String getEmpName() {
return empName;
}
public int getEmpSalary() {
return empSalary;
}
}
package org.websparrow.beans;
public class Boss {
private String name;
private String officeLocation;
private Employee employee;
// Parameterized constructor
public Boss(String name, String officeLocation, Employee employee) {
this.name = name;
this.officeLocation = officeLocation;
this.employee = employee;
}
// business logic
public void display() {
System.out.println("Boss and Employee details....\n");
System.out.println("Boss name: " + name);
System.out.println("Office Location: " + officeLocation);
System.out.println("Employee name: " + employee.getEmpName());
System.out.println("Employee salary: " + employee.getEmpSalary());
}
}
Spring Beans Configuration
Add c-namespace
schema into root tag beans.
<?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.xsd">
<bean id="boss" class="org.websparrow.beans.Boss" c:name="Atul Rai" c:officeLocation="India"
c:employee-ref="emp" />
<bean id="emp" class="org.websparrow.beans.Employee" c:empName="Mukul" c:empSalary="1000000" />
</beans>
Run it
Load the configuration file and run it.
package org.websparrow.clients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.Boss;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Boss boss = (Boss) context.getBean("boss");
boss.display();
}
}
You will get the following result in your console log.
Boss and Employee details....
Boss name: Atul Rai
Office Location: India
Employee name: Mukul
Employee salary: 1000000
Download Source Code: spring-c-namespace-example
Similar Posts
- Spring Collection (List, Set and Map) Dependency Injection Example
- Spring Boot Security- Change default username and password parameter
- How to read properties file in Spring
- How to build executable JAR with Maven in Spring Boot
- Spring Boot- The Tomcat connector configured to listen on port 8080 failed to start
- Spring autowiring using @Resource and @Inject annotation example
- Difference Between @Component and @ComponentScan Annotations in Spring Boot
- Spring Boot + Spring Security with JPA authentication and MySQL
- Spring Boot- Pagination and Sorting using Spring Data JPA
- Spring Boot Microservices + Netflix Eureka Service Registry Example