Home  >  Spring Core

Example of c-namespace in Spring

By Arvind Rai, March 24, 2013
On this page we will provide Example of c-namespace in spring. c-namespace has been introduced in spring 3.1. It replaces the old style of constructor-arg. The bean which needs to be configured with c-namespace, must have constructor to accept those arguments. Find the old style for constructor dependency injection.
<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"/>
 



XML file with 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> 

Create 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 in XML. Find the link for the example.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us