c namespace was introduced in Spring 3.1. Like p namespace is a shortcut notation for <property> tags, c namespace is a shortcut notation for <constructor-arg> tags.
Consider following example:
Student.java
Course.java
MainApp.java
ApplicationContext.xml
In the above program arguments of Student and Course class constructors have been injected using <constructor-arg> tags.
Now the same program using c namespace is (you only need to change ApplicationContext.xml, other classes remain same):
ApplicationContext.xml
In the above program we are using c namespace to inject the argument values of Student and Course class constructors. For example,
c:age="30" is substitute of:
<constructor-arg value="30"/>
Note how we are injecting reference to other bean as a value for the 'c' argument of Student class constructor, using c namespace:
c:c-ref="course1"
is a substitute of
<constructor-arg ref="course1"/>
Note the -ref at the end of c:c-ref="course1". -ref means this property would accept a reference to other bean as its value.
Unlike <constructor-arg> tags, c namespace are not separate tags but they are the part of <bean> tag
Note that name, age and c in c:name, c:age, c:c-ref respectively are the name of arguments of Student class constructor. They are not the names of Student class member fields/properties which are name, age and course.
For c namespace to work you have to add its schema URI https://www.springframework.org/schema/c in above xml file.
In case when we do not know the names of constructor arguments we can use following syntax in ApplicationContext.xml:
Here c:_0 means first argument of constructor, c:_1 means second argument and c:_2-ref means third argument which is accepting a reference to other bean as its value.
Consider following example:
Student.java
package di.cnamespace;
public class Student {
private String name;
private int age;
private Course course;
//constructor
Student(String name, int age, Course c){
this.name = name;
this.age = age;
this.course = c;
}
// setters and getters...
public void displayInfo(){
System.out.println("Student Name : "+name);
System.out.println("Student Age : "+ age);
System.out.println("Course name : "+course.getCourseName());
}
}
public class Student {
private String name;
private int age;
private Course course;
//constructor
Student(String name, int age, Course c){
this.name = name;
this.age = age;
this.course = c;
}
// setters and getters...
public void displayInfo(){
System.out.println("Student Name : "+name);
System.out.println("Student Age : "+ age);
System.out.println("Course name : "+course.getCourseName());
}
}
Course.java
package di.cnamespace;
public class Course {
String courseName;
Course(String course){
this.courseName = course;
}
public void setCourseName(String name){
this.courseName = name;
}
public String getCourseName(){
return courseName;
}
}
public class Course {
String courseName;
Course(String course){
this.courseName = course;
}
public void setCourseName(String name){
this.courseName = name;
}
public String getCourseName(){
return courseName;
}
}
MainApp.java
package di.cnamespace;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Student stud = (Student)appContext.getBean("student");
stud.displayInfo();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Student stud = (Student)appContext.getBean("student");
stud.displayInfo();
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="di.cnamespace.Student">
<constructor-arg value="abc"/>
<constructor-arg value="30"/>
<constructor-arg ref="course1"/>
</bean>
<bean id="course1" class="di.cnamespace.Course">
<constructor-arg value="Computers"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="di.cnamespace.Student">
<constructor-arg value="abc"/>
<constructor-arg value="30"/>
<constructor-arg ref="course1"/>
</bean>
<bean id="course1" class="di.cnamespace.Course">
<constructor-arg value="Computers"/>
</bean>
</beans>
In the above program arguments of Student and Course class constructors have been injected using <constructor-arg> tags.
Now the same program using c namespace is (you only need to change ApplicationContext.xml, other classes remain same):
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:c="https://www.springframework.org/schema/c"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="di.cnamespace.Student" c:name="abc" c:age="30" c:c-ref="course1"/>
<bean id="course1" class="di.cnamespace.Course" c:course="computers"/>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:c="https://www.springframework.org/schema/c"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="di.cnamespace.Student" c:name="abc" c:age="30" c:c-ref="course1"/>
<bean id="course1" class="di.cnamespace.Course" c:course="computers"/>
</beans>
In the above program we are using c namespace to inject the argument values of Student and Course class constructors. For example,
c:age="30" is substitute of:
<constructor-arg value="30"/>
Note how we are injecting reference to other bean as a value for the 'c' argument of Student class constructor, using c namespace:
c:c-ref="course1"
is a substitute of
<constructor-arg ref="course1"/>
Note the -ref at the end of c:c-ref="course1". -ref means this property would accept a reference to other bean as its value.
Unlike <constructor-arg> tags, c namespace are not separate tags but they are the part of <bean> tag
Note that name, age and c in c:name, c:age, c:c-ref respectively are the name of arguments of Student class constructor. They are not the names of Student class member fields/properties which are name, age and course.
For c namespace to work you have to add its schema URI https://www.springframework.org/schema/c in above xml file.
In case when we do not know the names of constructor arguments we can use following syntax in ApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:c="https://www.springframework.org/schema/c"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="di.cnamespace.Student" c:_0="abc" c:_1="30" c:_2-ref="course1"/>
<bean id="course1" class="di.cnamespace.Course" c:_0="computers"/>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:c="https://www.springframework.org/schema/c"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="di.cnamespace.Student" c:_0="abc" c:_1="30" c:_2-ref="course1"/>
<bean id="course1" class="di.cnamespace.Course" c:_0="computers"/>
</beans>
Here c:_0 means first argument of constructor, c:_1 means second argument and c:_2-ref means third argument which is accepting a reference to other bean as its value.
1 (p namespace) | 2 (c namespace)
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment