Spring C/P 命名空间的使用和区别

名称空间p和c的使用

Spring2.0以后提供了xml命名空间。

P名称空间

C名称空间
首先它们不是真正的名称空间,是虚拟的。它是嵌入到spring内核中的。

使用p名称空间可以解决我们setter注入时简化

使用c名称空间可以解决我们构造器注入时简化

Demo:

Dog.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
30
31
32
33
34
35
36
package com.test.p;

public class Dog {

private String name;
private String color;

public Dog() {
super();
// TODO Auto-generated constructor stub
}

public Dog(String name, String color) {
super();
this.name = name;
this.color = color;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Dog [name=" + name + ", color=" + color + "]";
}

}

Person.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
30
31
32
33
34
35
36
37
38
package com.test.p;

public class Person {
private String name;
private Dog dog;

public Person() {
super();
// TODO Auto-generated constructor stub
}

public Person(String name, Dog dog) {
super();
this.name = name;
this.dog = dog;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

@Override
public String toString() {
return "Person [name=" + name + ", dog.name=" + dog.getName() + " dog.color" + dog.getColor() + "]";
}
}

以上是Demo使用的两个类

接下来我们来写applicationContext.xml的配置文件

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
<?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:p="http://www.springframework.org/schema/p"
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="dog1" class="com.test.p.Dog">
<property name="name" value="吉娃娃" />
<property name="color" value="黄色" />
</bean>

<!-- P命名空间方式的注入 简化上述操作-->
<bean id="dog2" class="com.test.p.Dog" p:name="金毛" p:color="黄色"></bean>

<!-- 传统构造器注入 -->
<bean id="dog3" class="com.test.p.Dog">
<constructor-arg index="0" value="藏獒" />
<constructor-arg index="1" value="黑色" />
</bean>

<!-- 使用C名称空间来简化构造器注入 -->
<bean id="dog4" class="com.test.p.Dog" c:name="哈士奇" c:color="彩色"></bean>
<!--
ref的使用
这个人叫张三,他有一只哈士奇,是彩色的
-->
<bean id="person" class="com.test.p.Person" p:name="张三" p:dog-ref="dog4"></bean>
</beans>

为了更加直观的看到他们之间的区别,我们将传统方式和c、p命名空间的方式都写了,并在接下来的test中进行调用,我们来观察它的执行结果

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.test.p;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DogTest {

@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Dog dog1 = (Dog)context.getBean("dog1");
System.out.println(dog1);
}

@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Dog dog2 = (Dog)context.getBean("dog2");
System.out.println(dog2);
}

@Test
public void test3() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Dog dog3 = (Dog)context.getBean("dog3");
System.out.println(dog3);
}

@Test
public void test4() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Dog dog4 = (Dog)context.getBean("dog4");
System.out.println(dog4);
}

@Test
public void test5() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person p1 = (Person)context.getBean("person");
System.out.println(p1);
}
}

我们来观察它们最后的执行结果:

Test1

Test2

Test3

Test4

Test5

文章目录
,