Recap
As we already know, we can define bean properties and constructor arguments as references to other managed beans or as values defined inline.
Spring supports extensible configuration formats with namespaces, which are based on an XML Schema definition.
XML Shortcut with the p-namespace
Before using p-namespace, we register a class in configuration file with <property> label to define the value of parameter. So I personally think p means property here.
How to use
Add a new namespace in xmlns:p="http://www.springframework.org/schema/p"
The format of using p-namespace is:1
2<bean id="" class=""
p:attr="">
where id and class mean the same as before, p:attr="" means defining the value of a certain parameter
Example
User.java:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30package com.xliu.pojo;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
userbeans.xml:1
<bean id="user" class="com.xliu.pojo.User" p:age="18" p:name="xliu"/>
equals to:1
2
3
4<bean id="user" class="com.xliu.pojo.User">
<property name="name" value="xliu"/>
<property name="age" value="18"/>
</bean>
in this example, the value of user and age is defined in userbeans.xml
XML Shortcut with the c-namespace
Before using c-namespace, we register a class in configuration file with <constructor-arg> label to initialize the constructor method. So I personally think c means constructor here.
How to use
Add a new namespace in xmlns:c="http://www.springframework.org/schema/c"
The format of using p-namespace is:<bean id="" class="" c:age="" c:name=""/>
where id and class mean the same as before, c:attr="" means defining the constructor’s parameter
Example
User.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35package com.xliu.pojo;
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Different from previous section, I added one constructor with two parameter, age and name, here. Otherwise c-namespace will not be used
userbeans.xml:1
<bean id="user2" class="com.xliu.pojo.User" c:age="19" c:name="liux"/>
equals to:1
2
3
4<bean id="user2" class="com.xliu.pojo.User">
<constructor-arg name="age" value="19"/>
<constructor-arg name="name" value="xliu"/>
</bean>
Same, the constructor is defined.
Conclusion
Both p-namespace and c-namespace can be replaced by either<property>or<constructor-arg>, they are more elegant alternatives.