• 使用Spring的命名空间p装配属性-摘自《Spring实战(第3版)》


    使用<property>元素为Bean的属性装配值和引用并不太复杂。尽管如此,Spring的命名空间p提供了另一种Bean属性的装配方式,该方式不需要配置如此多的尖括号。

    命名空间p的schema URL为http://www.springframework.org/schema/p。如果你想使用命名空间p,只需要在Spring的XML配置中增加如下一段声明:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:p="http://www.springframework.org/schema/p"
                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"> 

    (以上链接为手打可能有误,拷贝时请检查)

    通过此声明,我们现在可以使用p:作为<bean>元素所有属性的前缀来装配Bean的属性。

    为了示范,我使用property声明了一个bean和使用命名空间p声明一个bean来比较一下。

    property:
    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells"/>
        <property name="instrument" ref="piano"/>
    </bean>
    p命名空间:
    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
        p:song="Jingle Bells"
        p:instrument-ref="piano"/>

     p:song属性的值被设置为“Jingle Bells”,将使用该值装配song属性。同样,p:instrument-ref属性的值被设置为“piano”,将使用一个id为piano的bean引用来装配instrument属性,-ref后缀作为一个标识来告诉spring应该装配一个引用而不是字面值。

    选择<property>还是命名空间p取决于你,它们是等价的。命名空间p的最主要优点是更简洁。

  • 相关阅读:
    Navicat工具破解
    ios7开发者必知
    浏览器工作原理
    Lettcode_104_Maximum Depth of Binary Tree
    LeetCode:Balanced Binary Tree
    error: openssl/md5.h: No such file or directory
    欢迎来到Swift天地(Welcome to Swift)
    最大子阵列和
    【Cocos2d-x Lua】数据库封装类型的操作
    python于lxml应用
  • 原文地址:https://www.cnblogs.com/hihtml5/p/5823007.html
Copyright © 2020-2023  润新知