Spring 的 P 命名空间
什么是 P 命名空间?
P 命名空间 不是新的东西,它是对 IoC 和 DI 的简化。使用 p 命名空间 可以更加方便地完成 bean 的配置以及 bean 之间的依赖注入。
如何使用 p 命名空间?
首先必须确保 xml 文件中引入了 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-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>