后端——框架——容器框架——spring_core——《官网》阅读笔记——第九章节(内置schema)

  到第九章节已进入尾声,第九章节介绍Schema的内容,Schema分为两类,第一类为Spring内置的schema,第二类为自定义schema。

  Schema在日常开发中使用最频繁的只有较少的几个,而且随着注解方式的兴起,Xml的没落,Schema的使用频率也会越来越低。目前最常使用的Schema就是context,bean。

  我觉得本章掌握内置schema即可,无需自定义schema。本文省略自定义schema。

  Spring内置的schema有:

  1. Bean:必须引入的,配置bean。
  2. context:使用频率非常高,配置spring框架的上下文
  3. Aop:spring的切面功能,使用aop时,必须引入
  4. P:全称为property,为了方便bean属性的配置,在第一章节中已介绍
  5. C:全称为Constructor,为了方便bean构造器参数的配置,在第一章节中已介绍
  6. util:不是必须的,类似于工具类,方便配置集合,bean的属性等。

  下面详细介绍每个Schema。

1、Util

  使用util Schema之前,首先需要引入。XML的配置如下:

<?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>

1.1  constant

  constant标签的含义是获取常量的值。它可以赋给bean的属性,可以是集合中的某个元素。

  使用步骤如下:

  1. 第一步,在XXConstant类中定义任意常量XXConstant,在下述示例中我创建了UserConstant,定义了USER_NAME常量。
  2. 第二步,使用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" />
		<!-- 使用XXFactoryBean方式 -->
		<bean id="com.constant.UserConstant.USER_NAME" 
	class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
	</property>
</bean>

  示例中有两种方式,使用constant标签方式,将USER_NAME常量值赋值给User对象的name属性。

1.2  property-path

  property-path标签的含义是获取符合对象的属性值,例如Role对象中有User对象,User对象中有Address对象,获取Address对象中的country属性值,可以写为

  user.address.country。其中user,address必须是容器中User,Address对象的bean ID属性。

<!-- 配置Address -->
<bean id="address" class="com.bean.Address">
	<!-- 配置Address的country属性 -->
	<property name="country" value="CN">
</bean>

<!-- 配置 -->
<bean id="user" class="com.bean.User">
	<property name="address">
		<ref bean="address"/>
	</property>
</bean>

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

  它可以赋值给任何对象的属性。

1.3  Properties

  Properties标签,根据xxx.properties文件创建Properties对象。

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

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

  1. ignore-resource-not-found:当xxx.properties文件找不到时,是否抛出异常,true不会抛出,默认值为false。
  2. local-override:是否覆盖同名的属性
  3. scope:作用域,java.util.Properties也是一个bean。值为bean的作用域
  4. value-type:键值对中值的对象类型,默认为String,无需修改。

1.4  List

  List标签,根据标签创建List对象。

<!-- List集合 -->
<util:list id="userList" list-class="java.util.LinkedList" scope="singleton" value-type="com.bean.User">
		<ref bean="user"/>
</util:list>
  1. Id属性:唯一标识
  2. List-class:指明List的类型。
  3. Scope:作用域
  4. Value-type:容器中元素的类型。当是字符串或基本数据类型时无需指定。

1.5  Set

  Set标签,根据标签创建Set对象,唯一区别是将list-class属性修改为set-class。

1.6   Map

  Map标签,根据标签创建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属性。

2、Beans

  Beans Schema是必须的,用于往容器中注入bean。

2.1  bean

  在第一章节中已介绍过。

3、Context

  Context schema都是IOC容器相关的配置。在使用之前需要引入

<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>

  Context标签下的内容比较杂,配置至少需要了解标签的功能。

3.1  property-placeholder

  它是PropertyPlaceholderConfigurer的简洁形式。这个类的主要功能是往IOC容器中添加键值对,支持三种形式,xxx.properties文件形式,property子标签形式。

<!-- 配置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接口返回的整数。

3.2  annotation-config

  它的作用是激活注解,一般不使用,component-scan在此基础上提供了更强大的功能,

<!-- 激活注解 -->
<context:annotation-config/>

3.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

3.4  load-time-weaver

  它是AOP的一种类型,AOP有三种类型,inner type structure,load-time-weaver,advice。

3.5  spring-configured

  spring与AspectJ的集成。

3.6  mbean-export

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

4、AOP

  在第五,第六章节中介绍。

原文地址:https://www.cnblogs.com/rain144576/p/12272392.html