美文网首页我爱编程
八、基于XML Schema的简化配置方式

八、基于XML Schema的简化配置方式

作者: 数独题 | 来源:发表于2017-03-05 15:22 被阅读57次

使用p:命名空间简化配置:

Chinese.java

package entity;

import inter.Axe;
import inter.Persion;



public class Chinese implements Persion{

    private Axe axe;
    private int age;
    
    public void setAxe(Axe axe) {
        this.axe = axe;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public void useAxe() {
        // TODO Auto-generated method stub
        System.out.println(axe.chop());
        System.out.println("age成员变量的值:"+age);
    }

    
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的根元素和Schema并导入p:命名空间 -->
<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"
    
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
        <!-- 使用p:命名空间配置Bean -->
    <bean id="chinese" class="entity.Chinese" p:age="29" p:axe-ref="steelAxe"/>
      
    <bean id="steelAxe" class="entity.SteelAxe" />
</beans>

使用c:命名空间简化配置:

p:命名空间主要用于简化设值注入,而c:命名空间主要用于简化构造注入。

使用c:指定构造参数的格式为:c:构造器参数名="值"或c:构造器参数名-ref="其他Bean的id"

Chinese.java

package entity;

import inter.Axe;
import inter.Persion;



public class Chinese implements Persion{

    private Axe axe;
    private int age;
    
    public Chinese(Axe axe,int age)
    {
        this.axe=axe;
        this.age=age;
    }

    @Override
    public void useAxe() {
        // TODO Auto-generated method stub
        System.out.println(axe.chop());
        System.out.println("age成员变量的值:"+age);
    }

    
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的根元素和Schema并导入c:命名空间 -->
<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"
    
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
        <!-- 使用p:命名空间配置Bean -->
    <bean id="chinese" class="entity.Chinese" c:axe-ref="steelAxe" c:age="29"/>
      
    <bean id="steelAxe" class="entity.SteelAxe" />
</beans>

Spring还支持一种通过索引来配置构造参数的方式。

<bean id="chinese" class="entity.Chinese" c:_0-ref="steelAxe" c:_1="29"/>

使用util:命名空间简化配置:

为了使用util:命名空间的元素,必须先再Spring配置文件中导入最新的spring-util-4.0.xsd
beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的根元素和Schema并导入p:命名空间和util:命名空间元素 -->
<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"
    xmlns:util="http://www.springframework.org/schema/util"
    
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
       
        
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-4.0.xsd >
    
</beans>

util Schema下提供了如下几个元素:

  • constant:该元素用于获取指定类的静态Field的值。
  • property-path:该元素用于获取指定对象的getter方法的返回值。
  • list:该元素用于定义一个List Bean,支持使用<value.../>,<ref.../>,<bean.../>等子元素来定义LIst集合元素。
  • set:该元素用于定义一个Set Bean,支持使用<value.../>,<ref.../>,<bean.../>等子元素来定义Set集合元素。
  • map:该元素用于定义一个Map Bean,支持使用<value.../>,<ref.../>,<bean.../>等子元素来定义Map集合元素。
  • properties:该元素用于加载一份资源文件,并根据加载的资源文件创建一个properties Bean实例。

相关文章

网友评论

    本文标题:八、基于XML Schema的简化配置方式

    本文链接:https://www.haomeiwen.com/subject/ubfegttx.html