Spring c-namespace Example

By Arvind Rai, November 06, 2021
On this page we will learn Spring XML c-namespace example. The c-namespace is a shortcut to configure constructor arguments in bean definitions in XML configuration. The c-namespace is the replacement of constructor-arg element.
The c-namespace is used as c: namespace to perform constructor-based dependency injection.
Suppose we have a code using <constructor-arg> element.
<bean id="city" class="com.concretepage.bean.City">
	<constructor-arg name="cityName" value="Agra"/>
	<constructor-arg name="population" value="1000"/>
</bean> 
We can replace <constructor-arg> using c-namespace as follows.
<bean id="city" class="com.concretepage.bean.City" c:cityName="Agra" c:population="1000"/> 

Using c-namespace

Find the XML file with c-namespace.
app-conf.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
      
	<bean id="city" class="com.concretepage.bean.City" c:cityName="Agra" c:population="1000"/>
	<bean id="student" class="com.concretepage.bean.Student" c:city-ref="city"  c:name="Ram"/>
</beans> 
Find the beans used in our example.
City.java
package com.concretepage.bean;
public class City {
	private String cityName;
	private int population;
	public City(String cityName, int population){
		this.cityName = cityName;
		this.population = population;
	}
	public String getCityName() {
		return cityName;
	}
	public int getPopulation() {
		return population;
	}
} 
Student.java
package com.concretepage.bean;
public class Student {
	private City city;
	private String name;
	public Student(City city, String name){
		this.city=city;
		this.name=name;
	}
	public City getCity() {
		return city;
	}
	public String getName() {
		return name;
	}
} 
Run Application.
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Student;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");
                Student st=(Student) context.getBean("student");
                System.out.println("Student Name:"+ st.getName());
                System.out.println("City Nmae:"+st.getCity().getCityName());
                System.out.println("City Population:"+st.getCity().getPopulation());        
		context.close();
	}
} 
Find the output.
Student Name:Ram
City Nmae:Agra
City Population:1000 
The <property> tag can be replaced by p-namespace shortcut. Find the link.

Reference

XML Shortcut with the c-namespace

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us