zoukankan      html  css  js  c++  java
  • 后端——框架——容器框架——spring_core——Schema

      Schema分为两类,内置,自定义(略)。

    内置schema有以下六个:

    bean:配置bean。已介绍过,略。

    context:配置上下文。

    aop:配置切面。已介绍过,略。

    P || C:配置属性,构造器参数的简便写法。 已介绍过,略。

    util:配置集合的简便写法。

    1、context

    引入context的schema,如下:

    <beans xmlns="http://www.springframework.org/schema/beans" 
    	   xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
           	http://www.springframework.org/schema/context
    	https://www.springframework.org/schema/context/spring-context.xsd">
    		
    		<!-- 只引入了beans和context -->
    </beans>
    

      上下文配置较杂,下面详细介绍各个标签。

    1.1   property-placeholder

    添加自定义键值对,三种形式,properties文件形式,property标签形式, property-ref形式.

    示例如下:

    <!-- 配置property-placeholder -->
    <!-- 引入xxx.properties文件形式 -->
    <context:property-placeholder location="properties/jdbc.properties" 
    ignore-resource-not-found="false"/>
    <!-- 直接定义property子标签 -->
    <context:property-placeholder>
    	<property name="someKey" value="someValue"/>
    </context:property-placeholder>
    <!-- 引入IOC容器中的Properties对象 -->
    <context:property-placeholder properties-ref="jdbcProp"/>
    1. location:xxx.properties文件的位置
    2. file-encoding:文件的字符集
    3. ignore-resource-not-found:xxx.properties文件未找到时是否抛出异常,true表示不抛出异常。
    4. ignore-unresolvable:如果找不到对应的key值时,是否抛出异常。True表示不抛出异常。
    5. local-override:若存在同名key值,是否使用local properties覆盖。True表示覆盖。
    6. null-value:等价于null的value值,例如将”null”字符串等价于null
    7. trim-values:该值为true时,获取value值时会调用String的trim函数
    8. value-separator:key-value之间的分隔符
    9. order:指定顺序,等价于实现Order接口返回的整数。

    1.2   annotation-config

    激活注解。

    <context:annotation-config/>
    

    1.3    component-scan

    等价于@ComponentScan,扫描路径,添加过滤条件

    <context:component-scan base-package="com.rain"/>
    1. base-package:root package,以及它的所有子包
    2. annotation-config:激活注解,默认值为true,使用默认值即可
    3. name-generator:bean名称的生成策略,是NameGenerator接口的实现类
    4. resource-pattern:扫描的资源格式,默认为**/*.class,只扫描字节码文件。
    5. scope-resolver:内置的解析器,根据BeanDefinition对象获取ScopeMetadata,即获取bean的作用域。
    6. scoped-proxy:生成代理类的策略,no是不需要,targetClass是使用CGLIB,Interfaces是使用Java的动态代理。

    子标签:

    1. include-filter:添加包含的过滤条件,有五种类型。参考@ComponentScan
    2. exclude-filter:添加排除的过滤条件,有五种类型。参考@ComponentScan

    1.4  load-time-weaver

    开启spring提供的load time weaver。它是切面weave机制中的其中一种。

    1.5   spring-configured

    spring与AspectJ的集成。

    1.6   mbean-export

    集成MBean,使用JMX等工具监控。 

    2、util

    引入util的schema,如下:

    <?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:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
        <!-- 只引入了beans和util Schema -->
    </beans>
    

    2.1     constant

    引用类中的public static修饰的常量。

    步骤如下:

    第一步,创建常量。示例中创建了UseConstant类,包含USER_NAME常量。

    第二步,使用util:constant获取常量值。

    <!-- 设置user的属性 -->
    <bean id="user" class="com.bean.User">
    	<!-- 设置user的name属性 -->
    	<property name="name">
    		<!-- 获取UserConstant的USER_NAME常量 -->
    		<util:constant 
    static-field="com.constant.UserConstant.USER_NAME" />
    	</property>
    </bean>
    

    2.2     property-path

    对象属性引用链,例如User类存在Address类属性,Address类存在Country类属性。可以使用user.address.country引用country属性值。

    <!-- 获取user.address.country -->
    <util:property-path path="user.address.country"/>
    

    2.3   properties

    注入properties对象,属性通过properties文件获取。

    <util:properties id="jdbc" location="classpath:properties/jdbc.properties"/>
    

      示例中ID为唯一标识符,location为xxx.properties文件的位置。其他属性如下:

    ignore-resource-not-found:当xxx.properties文件找不到时,是否抛出异常,true不会抛出,默认值为false。

    local-override:是否覆盖同名的属性

    scope:作用域,java.util.Properties也是一个bean。值为bean的作用域

    value-type:键值对中值的对象类型,默认为String,无需修改。

    2.4  list

    配置list的简便方式

    <util:list  id="userList" list-class="java.util.LinkedList" scope="singleton" value-type="com.bean.User">
         <ref bean="user"/>
    </util:list>
    

      Id:唯一标识

    List-class:指明List的类型。

    Scope:作用域

    Value-type:容器中元素的类型。当是字符串或基本数据类型时无需指定。

    2.5   set

    与list基本相同,唯一区别是将list-class属性修改为set-class。

    2.6   map

    配置map的简便方式

    <util:map id="map" map-class="java.util.HashMap" scope="singleton">
    	<entry key="someKey" value="someValue">
    </util:map>
    

      示例中key和value的类型都是String,所以未配置key-type和value-type属性。

  • 相关阅读:
    计算机网络面试小知识总结(转载)
    williamisnotme@gmail.com
    jdk1,8 HashMap
    Mybatis 为什么不要用二级缓存
    CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
    一级缓存,二级缓存
    mock单测
    java8函数式编程(转载)
    volatile解析
    JVM 技术分享(初级)
  • 原文地址:https://www.cnblogs.com/rain144576/p/14758593.html
Copyright © 2011-2022 走看看