• Spring


    p 命名空间

    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>
  • 相关阅读:
    大话设计模式读书笔记--4.代理模式
    大话设计模式读书笔记--3.装饰模式
    大话设计模式读书笔记--2.策略模式
    大话设计模式读书笔记--1.简单工厂模式
    redis分片
    redis主从同步
    用Jedis连接Redis
    redis的数据类型和指令
    使用可视化工具redisclient连接redis
    《深入浅出通信原理》参考资料
  • 原文地址:https://www.cnblogs.com/huey/p/4507122.html
Copyright © 2020-2023  润新知