文章目录
- 前言
- 一、p命名空间
- 1.1、编写一个普通的Java类
- 1.2、spring配置文件
- 1.3、测试
- 1.4、运行结果
- 二、c命名空间
- 2.1、编写一个普通的Java类
- 2.2、spring配置文件
- 2.3、测试
- 2.4、运行结果
- 总结
前言
P命名空间注入:
目的:简化set方法注入
使用p命名空间注入的前提条件包括两个:
- 在XML头部信息中添加p命名空间的配置信息:xmlns:p=“http://www.springframeworl.org/schema/p”
- p命名空间注入是基于setter方法的,所以需要对应的属性提供set方法
c命名空间注入:
目的:简化构造方法
使用c命名空间注入的前提条件包括两个:
- 在XML头部信息中添加p命名空间的配置信息:xmlns:c=“http://www.springframeworl.org/schema/c”
- c命名空间注入是基于构造方法的,所以需要提供构造方法
一、p命名空间
1.1、编写一个普通的Java类
public class Dog {
//简单类型
private String name;
private int age;
//非简单类型
private Date birth;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", birth=" + birth +
'}';
}
}
1.2、spring配置文件
spring配置文件:spring-p.xml
第一步:在spring的配置文件头部添加p命名空间,xmlns:p=“http://www.springframework.org/schema/p”
第二步:使用 p:属性名
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
第一步:在spring的配置文件头部添加p命名空间,xmlns:p="http://www.springframework.org/schema/p"
第二步:使用 p:属性名
-->
<bean id="dogBean" class="com.powernode.spring6.bean.Dog" p:name="小陈" p:age="3" p:birth-ref="birthBean"></bean>
<!--这里获取的是当前系统时间-->
<bean id="birthBean" class="java.util.Date"></bean>
</beans>
1.3、测试
测试类:
@Test
public void testSpringP(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-p.xml");
Dog dog = applicationContext.getBean("dogBean", Dog.class);
System.out.println(dog);
}
1.4、运行结果
二、c命名空间
2.1、编写一个普通的Java类
public class People {
private String name;
private int age;
private boolean sex;
//c命名空间注入办法是基于构造方法的
public People(String name, int age, boolean sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
2.2、spring配置文件
spring配置文件:spring-c.xml
第一步:在spring的配置文件头部添加c命名空间,xmlns:c=“http://www.springframework.org/schema/c”
第二步:使用 c:参数名方式
也可以使用c:下标方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
第一步:在spring的配置文件头部添加c命名空间,xmlns:c="http://www.springframework.org/schema/c"
第二步:使用 c:下标方式 或者 c:参数名方式
-->
<bean id="peopleBean" class="com.powernode.spring6.bean.People" c:_0="小花" c:_1="20" c:_2="true"></bean>
</beans>
2.3、测试
测试类:
@Test
public void testSpringC(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-c.xml");
People people = applicationContext.getBean("peopleBean", People.class);
System.out.println(people);
}
2.4、运行结果
总结
p命名空间本质上还是set注入,只不过p命名空间注入可以让spring配置变得更简单
c命名空间是简化构造方法注入的