Spring 的 P 命名空间
什么是 P 命名空间?
P 命名空间 不是新的东西,它是对 IoC 和 DI 的简化。使用 p 命名空间 可以更加方便地完成 bean 的配置以及 bean 之间的依赖注入。
如何使用 p 命名空间?
首先必须确保 xml 文件中引入了 p 命名空间。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-49fV2hfI-1592836598054)(Spring的 P 命名空间.assets/image-20200614013904650.png)]](https://i-blog.csdnimg.cn/blog_migrate/85b23b34c254588c6ffcc62415b14bbd.png)
<?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-3.2.xsd">
<bean id="b" class="com.theSunAndSnow.entity.Student" p:id="1" p:name="JayChou" p:age="40" p:address-ref="address"></bean>
<bean id="address" class="com.theSunAndSnow.entity.Address" p:name="科技路" p:id="1"></bean>
</beans>
简化了原本配置 bean 标签时繁多的 property 标签。
p:address-ref 相当于
<property name="address" ref="address"></property>

本文介绍Spring框架中的P命名空间,它简化了bean的配置和依赖注入过程,通过使用p命名空间,开发者可以在bean定义中直接指定属性值,无需额外的property标签,使XML配置文件更简洁。
2135

被折叠的 条评论
为什么被折叠?



