Friday, May 7, 2021

Using c-namespace in Spring

While using Spring XML configuration for wiring beans you would have used <constructor-arg> element several times for providing, property values and/or providing reference for beans, as a constructor injection. If you are looking for any shorter alternative, just as p-namesapce is used in place of nested <property> element, you can use c-namespace in Spring.

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.

As example–

If you have an XML configuration for bean employee which has a constructor that refers to the bean of type address with constructor-arg you will write it like this -

<bean id="employee" class="org.netjs.model.Employee">
    <constructor-arg ref="address"/>
</bean>

You can use c-namespace to shorten it like this -

<bean id="employee" class="org.netjs.model.Employee" c:officeAddress-ref="address">
    <!-- <constructor-arg ref="address"/> -->
</bean>

You can see here that instead of using nested <constructor-arg> element you can use bean element’s attribute itself.

In this way of using c-namespace for injection of bean reference c:officeAddress-ref="address", c: denotes the use of c-namespace, officeAddress is the constructor argument name with in the employee class where value is injected, -ref indicates injection of bean reference.

Using c-namespace with literals

Same way c-namespace can be used for injecting literal values.

As example

If you have an XML configuration for bean employee which has a constructor argument empName then you can provide a literal value to empName property like this –

<bean id="employee" class="org.netjs.model.Employee">
    <constructor-arg value="Ram"/>
</bean>

Using c-namespace you can shorten it like this –

<bean id="employee" class="org.netjs.model.Employee" c:empName="Ram">
    <!-- <constructor-arg value="Ram"/> -->
</bean>

Spring c-namespace example

As we have already seen there are two classes Address and Employee and Address class bean is referred in Employee.

Employee class

public class Employee {
 private int empId;
 private String empName;
 private int age;
 private Address officeAddress;
 
 public Employee(int empId, String empName, int age, Address officeAddress){
  this.empId = empId;
  this.empName = empName;
  this.age = age;
  this.officeAddress = officeAddress;
 }
 
 public Address getOfficeAddress() {
  return officeAddress;
 }
 
 public int getEmpId() {
  return empId;
 }
 
 public String getEmpName() {
  return empName;
 }
 
 public int getAge() {
  return age;
 }
}

Here it can be seen that Employee class has constructor that has some literal values as argument and one argument of type Address.

Address Class

public class Address {
 private String number; 
 private String street; 
 private String city; 
 private String state; 
 private String pinCode; 
 public String getNumber() {
  return number;
 }
 public void setNumber(String number) {
  this.number = number;
 }
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 public String getPinCode() {
  return pinCode;
 }
 
 public void setPinCode(String pinCode) {
  this.pinCode = pinCode;
 }
 
}

XML Configuration

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  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-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">
 
  
  <bean id="address" class="org.netjs.model.Address" p:number="101" p:street="M I Road" 
          p:city="Jaipur" p:state="Rajasthan" p:pinCode="302001"> 
  </bean>
  
  <bean id="employee" class="org.netjs.model.Employee" c:empId="1001" c:age="25" 
   c:empName="Ram" c:officeAddress-ref="address" />
    
</beans>
Here notice the inclusion of xmlns:c="http://www.springframework.org/schema/c” and xmlns:p="http://www.springframework.org/schema/p” in XML configuration for c-namespace and p-namespace respectively.

In bean definition for address p-namespace is used for providing values. In bean definition for Employee c-namespace is used to provide values for constructor arguments.

Test Class

You can run this code using the following code -

import org.netjs.model.Address;
import org.netjs.model.Employee;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {        
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext
     ("appcontext.xml");
    Employee emp = (Employee)context.getBean("employee");  
    Address addr = emp.getOfficeAddress();
    System.out.println("name " + emp.getEmpName());
    System.out.println("City " + addr.getCity());
    System.out.println("PinCode " + addr.getPinCode());
    context.close();    
  }
}

Output

name Ram
City Jaipur
PinCode 302001 

That's all for this topic Using c-namespace in Spring. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page


Related Topics

  1. Using p-namespace For Shorter XML in Spring
  2. Bean Definition Inheritance in Spring
  3. ApplicationContextAware And BeanNameAware Interfaces in Spring Framework
  4. Lazy Initialization in Spring Using lazy-init And @Lazy Annotation
  5. @Conditional Annotation in Spring

You may also like-

  1. Spring Profiles With Examples
  2. Spring JdbcTemplate Select Query Example
  3. How to Read Properties File in Spring Framework
  4. Functional interface annotation in Java 8
  5. Java Stream API Examples
  6. Convert String to Byte Array Java Program
  7. Interface Default Methods in Java
  8. super Keyword in Java With Examples

No comments:

Post a Comment