JAVA全系列 教程
3762个小节阅读:7088k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Spring 框架提供了p命名空间,属于<property>
Setter注入的简写方式。所以p就是property的缩写。
但是要注意,p命名空间是为了代替<property name="" ref=""/>
和<property name="" value="">
这两种写法。
使用时需要在xml文件中导入p命名空间。
xmlns: 代表Schema的命名空间的意思。
p是命名空间的名称。可以修改的。但是为了增加可读性,官方叫什么,我们就跟着叫什么。
xxxxxxxxxx
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
我们重新建立一个类,进行测试
xxxxxxxxxx
public class Animate {
private int id;
private String name;
// 省略getter、setter、构造方法
}
在没有使用p命名空间时
xxxxxxxxxx
<bean id="animate" class="com.bjsxt.pojo.Animate">
<property index="0" value="1"></property>
<property name="name" value="毛毛"></property>
</bean>
使用p命名空间后的简写方式。
语法:p:属性名="值" 或 p:_序号="值"
xxxxxxxxxx
<bean id="animate2" class="com.bjsxt.pojo.Animate" p:_0="2" p:name="旺财"></bean>
Spring 框架提供了c命名空间,属于<constructor-args>
构造注入的简写方式。所以c就是constructor的缩写。
但是要注意,c命名空间只提供了<constructor-args>
中name和index属性等效效果,而且还不能混合使用。也就是说当构造方法比较多,且无法通过一个属性就能明确确认构造方法时,不要使用c命名空间。
c命名空间需要在配置文件中添加新的Schema配置
xxxxxxxxxx
<?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">
</beans>
在没有使用c命名空间时
xxxxxxxxxx
<bean id="animate" class="com.bjsxt.pojo.Animate">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg index="1" value="毛毛"></constructor-arg>
</bean>
在使用c命名空间简写后的写法。
语法:c:属性名="值" 或 c:_序号="值"
xxxxxxxxxx
<bean id="animate2" class="com.bjsxt.pojo.Animate" c:id="2" c:_1="旺财" ></bean>