p-namespace(命名空间) 让你使用 bean 元素的属性(而不是嵌套的 元素)来描述你的属性值;Spring支持具有 命名空间 的可扩展配置格式,这些命名空间是基于XML Schema定义的。p命名空间依赖于setter方法
基本使用示例:
| public class Dog { |
| private String name; |
| private int age; |
| private Date birth; |
| |
| @Override |
| public String toString() { |
| return "Dog{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| ", birth=" + birth + |
| '}'; |
| } |
| |
| 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; |
| } |
| |
| public Date getBirth() { |
| return birth; |
| } |
| |
| public void setBirth(Date birth) { |
| this.birth = birth; |
| } |
| } |
| <?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"> |
| |
| |
| <bean id="dogBean" class="com.powernode.spring6.bean.Dog" |
| p:name="小花" p:age="3" p:birth-ref="birthBean"/> |
| <bean id="birthBean" class="java.util.Date"/> |
| |
| |
| |
| |
| |
| |
| |
| </beans> |
| @Test |
| public void testP(){ |
| ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-p.xml"); |
| Dog dogBean = classPathXmlApplicationContext.getBean("dogBean", Dog.class); |
| System.out.println(dogBean); |
| } |
输出: Dog
Spring 3.1中引入的c命名空间允许配置构造器参数的内联属性,而不是嵌套的 constructor-arg 元素。
引用官网案例:
| <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 |
| https://www.springframework.org/schema/beans/spring-beans.xsd"> |
| |
| <bean id="beanTwo" class="x.y.ThingTwo"/> |
| <bean id="beanThree" class="x.y.ThingThree"/> |
| |
| |
| <bean id="beanOne" class="x.y.ThingOne"> |
| <constructor-arg name="thingTwo" ref="beanTwo"/> |
| <constructor-arg name="thingThree" ref="beanThree"/> |
| <constructor-arg name="email" value="something@somewhere.com"/> |
| </bean> |
| |
| |
| <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo" |
| c:thingThree-ref="beanThree" c:email="something@somewhere.com"/> |
| </beans> |
- 对于构造函数参数名称不可用的罕见情况(通常是字节码编译时没有debug信息),你可以使用回退到参数索引(下标),如下所示。
| |
| <bean id="beanOne" class="x.y.ThingOne" c:_0-ref="beanTwo" c:_1-ref="beanThree" |
| c:_2="something@somewhere.com"/> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!