首页 > 学院 > 开发设计 > 正文

Spring(3.2.3)

2019-11-15 00:11:53
字体:
来源:转载
供稿:网友
SPRing(3.2.3) - Beans(4): p-namespace & c-namespacep 命名空间

p 命名空间允许你使用 bean 元素的属性而不是 <property/>子元素来描述 Bean 实例的属性值。从 Spring2.0 开始,Spring 支持基于 xml Schema 的方式来简化配置文件。beans 元素的结构定义在一个 XML Schema 文档中。但是,p 命名空间没有定义在 XSD 文件中,而是直接存在与 Spring 内核中。使用p 命名空间时,只需要在配置文件根元素beans 引入 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-3.0.xsd">        <bean id="magicWand" class="com.huey.dream.bean.Weapon"        p:type="Magic Wand"        p:weightKg="9.5" />        <bean id="magician" class="com.huey.dream.bean.GameCharacter"        p:type="Magician"        p:level="10"        p:weapon-ref="magicWand" />    </beans>

上述的配置与如下的配置等价:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    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-3.0.xsd">    <bean id="magicWand" class="com.huey.dream.bean.Weapon">        <property name="type" value="Magic Wand" />        <property name="weightKg" value="9.5" />    </bean>        <bean id="magician" class="com.huey.dream.bean.GameCharacter">        <property name="type" value="Magician" />        <property name="level" value="10" />        <property name="weapon" ref="magicWand" />    </bean></beans>

c 命名空间

c 命名空间 与 p 命名空间用法类似,不同是 p 命名空间作用于属性注入,而 c 命名空间作用于构造注入。

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:c="http://www.springframework.org/schema/c"    xsi:schemaLocation="     http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <bean id="magicWand" class="com.huey.dream.bean.Weapon"        c:type="Magic Wand"        c:weightKg="9.5" />        <bean id="magician" class="com.huey.dream.bean.GameCharacter"        c:type="Magician"        c:level="10"        c:weapon-ref="magicWand" /></beans>

上述的配置与如下的配置等价:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    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-3.0.xsd">    <bean id="magicWand" class="com.huey.dream.bean.Weapon">        <constructor-arg name="type" value="Magic Wand" />        <constructor-arg name="weightKg" value="9.5" />    </bean>        <bean id="magician" class="com.huey.dream.bean.GameCharacter">        <constructor-arg name="type" value="Magician" />        <constructor-arg name="level" value="10" />        <constructor-arg name="weapon" ref="magicWand" />    </bean></beans>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表