Open In App

Spring c-namespace with Example

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: How to Create a Spring Bean in 3 Different Ways?

The Spring c-namespace will be discussed in this article. We are going to assume you’re familiar with how to create a Bean within an XML configuration file. The first question that comes to mind is what Spring c-namespace is and how it will help you create Bean. One of the most common methods for creating a spring bean is to define it in an XML configuration file. To understand the use of Spring c-namespace, we must first create a bean in an XML configuration file the traditional way via constructor dependency injection. Let’s make a bean for the Java class listed below:

Java




package com.gfg.scripter;
    
public class Author {
    private int id;
    private String name;
    private String dob;
    
    public Author(int id, String name, String dob) {
      this.id = id;
      this.name = name;
      this.dob = dob;
    }
     
    public void displayBasicInfo() {
        System.out.println("Author name is " + name
                           + " and Id is " + id);
    }
        
    // Getters & Setters
}


Now we must create an XML file called app-config.xml and add it to the project classpath as shown below:

XML




<?xml version="1.0" encoding="UTF-8"?>
       xsi:schemaLocation="http://www.springframework.org/schema/beans
     
    <bean id="author" class="com.gfg.scripter.Author">
      <constructor-arg name="id" value="101"/>
      <constructor-arg name="name" value="Bishal Dubey"/>
      <constructor-arg name="dob" value="11-11-1994"/>
    </bean>
     
</beans>


As you can see, we only need one xml tag, the <constructor-arg> tag, to inject bean dependency for each instance variable. So here’s how the Spring c-namespace can help us. In Spring XML, c-namespace is an XML shortcut for the <constructor-arg> tag, which is used to inject bean dependency via the constructor. In XML configuration, the c-namespace replaces the <constructor-arg> tag. Spring c-namespace reduces the length of xml code and improves the readability of XML configuration. Let’s look at how:

XML




<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
          
    <bean id="author" class="com.gfg.scripter.Author"
          c:id=101 c:name="Bishal Dubey" c:dob="11-11-1994"/>
    
</beans>


Note: To use the Spring c-namespace, it must be declared with xmlns:c somewhere within the XML element or a parent element where it is used. It is not necessary to use c. These are simply conventions at work. You could rewrite your XML to use whatever you want as long as they all map to the same XML namespaces. To enable the c-namespace feature, we must add the xmlns:c=”http://www.springframework.org/schema/c” and use c to inject any dependency. Let’s run a simple spring project with the above xml configuration to see what happens:

Java




package com.gfg.scripter;
    
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gfg.scripter.Author;
    
public class GeeksDemo {
    public static void main(String[] args) {
        AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-config.xml");
        Author author=(Author)context.getBean("author");
        System.out.println("Id :" +author.getId()+ "Name :"+author.getName());
        context.close();
    }
}


Output:

Id : 101 Name : Bishal Dubey

Note: We discussed passing the dependency through c-namespace, but suppose we want to pass the reference of another bean. Normally, we would use the ref attribute of the <constructor-arg> tag, but we can do it through c-namespace. c:[property-name] -ref=”value” attribute, as shown below

XML




<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
          
    <bean id="company" class="com.gfg.scripter.Company" c:name="Danske Bank" c:location="India"/>
    <bean id="employee" class="com.gfg.scripter.Employee" c:empName="Bishal Dubey" c:company-ref="company"/>
    
</beans>


We learned how to use constructor-based injection with c-namespace in this article.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads