zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    SpringBoot构建大数据开发框架
    阿里云 docker连接总报超时 registry.cn-hangzhou.aliyuncs.com (Client.Timeout exceeded while awaiting headers
    这些保护Spring Boot 应用的方法,你都用了吗?
    那些年让你迷惑的阻塞、非阻塞、异步、同步
    spring data jpa 分页查询
    如何在Windows 10上运行Docker和Kubernetes?
    Spring Mvc和Spring Boot配置Tomcat支持Https
    Why I don't want use JPA anymore
    Spring Data JPA Batch Insertion
    MySQL 到底能不能放到 Docker 里跑?
  • 原文地址:https://www.cnblogs.com/huey/p/4507122.html
Copyright © 2011-2022 走看看