spring 的 util 命名空间主要是用来在 xml 配置里直接定义独立的集合 bean(list、set、map、properties),还能引用常量、属性路径,方便多个 bean 共享同一份集合配置,不用每个地方都重复写一遍。
一、util 命名空间是什么?
util 命名空间是 spring 提供的一个 xml 扩展命名空间,专门用于定义集合类型的 bean。它解决了标准 xml 配置中集合无法独立定义、无法被多个 bean 复用的问题。
在没有 util 命名空间时,集合只能内联在某个 bean 的 <property> 中:
<bean id="userservice" class="com.example.userservice">
<property name="servers">
<list>
<value>server1</value>
<value>server2</value>
</list>
</property>
</bean>这个 list 只属于 userservice,其他 bean 无法引用它。如果多个 bean 需要同一个集合,只能重复配置。
util 命名空间允许把集合定义成独立的 bean,赋予 id,然后通过 ref 注入到任意 bean 中。
<util:list id="serverlist">
<value>server1</value>
<value>server2</value>
</util:list>
<bean id="userservice" class="com.example.userservice">
<property name="servers" ref="serverlist"/>
</bean>
<bean id="orderservice" class="com.example.orderservice">
<property name="servers" ref="serverlist"/>
</bean>serverlist 成为一个独立的 bean,可以被多个 bean 共享。
二、引入 util 命名空间
在 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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- util 标签在这里使用 -->
</beans>关键是 xmlns:util 和 spring-util.xsd 两行。没有它们,<util:list> 等标签不会被解析。
三、常用标签详解
3.1<util:list>定义 list bean
<util:list id="serverlist" list-class="java.util.arraylist">
<value>server1</value>
<value>server2</value>
<value>server3</value>
</util:list>list-class 可选,默认是 arraylist。如果需要 linkedlist,可以指定:
<util:list id="serverlist" list-class="java.util.linkedlist">
list 中可以放简单值、bean 引用、嵌套集合。
<util:list id="mixedlist">
<value>text</value>
<ref bean="userdao"/>
<bean class="com.example.user"/>
</util:list>3.2<util:set>定义 set bean
<util:set id="serverset" set-class="java.util.linkedhashset">
<value>server1</value>
<value>server2</value>
<value>server1</value> <!-- 自动去重 -->
</util:set>set-class 可选,默认是 linkedhashset。set 会自动去重,元素顺序取决于实现类。
3.3<util:map>定义 map bean
<util:map id="configmap" map-class="java.util.linkedhashmap">
<entry key="timeout" value="30"/>
<entry key="retry" value="3"/>
<entry key="debug" value="true"/>
</util:map>map-class 可选,默认是 linkedhashmap。entry 的 key 和 value 可以是简单值、bean 引用、嵌套对象。
<util:map id="beanmap">
<entry key="primary" value-ref="primarydatasource"/>
<entry key="secondary" value-ref="secondarydatasource"/>
</util:map>3.4<util:properties>定义 properties bean
方式一:从外部文件加载
<util:properties id="dbprops" location="classpath:db.properties"/>
方式二:内联定义
<util:properties id="appprops">
<prop key="app.name">myapp</prop>
<prop key="app.timeout">30</prop>
</util:properties><util:properties> 返回的是 java.util.properties 对象,键和值都是 string。
3.5<util:constant>引用静态常量
<util:constant id="maxvalue" static-field="java.lang.integer.max_value"/> <util:constant id="charset" static-field="java.nio.charset.standardcharsets.utf_8"/>
static-field 写全限定类名加字段名。常用于把某个静态常量注册为 bean。
3.6<util:property-path>引用其他 bean 的属性
<bean id="user" class="com.example.user">
<property name="name" value="张三"/>
</bean>
<util:property-path id="username" path="user.name"/>username 成为一个 bean,值是 user 的 name 属性。常用于把某个 bean 的属性暴露为独立的 bean。
四、与标准集合标签的区别
| 对比维度 | util 命名空间 | 标准 <list>、<map> |
|---|---|---|
| 是否独立 bean | 是,有 id | 否,内联在属性中 |
| 是否可复用 | 可被多个 bean 引用 | 只属于当前 bean |
| 是否可单独获取 | 可以,getbean("id") | 不可以 |
| 定义位置 | 直接放在 <beans> 下 | 嵌套在 <property> 或 <constructor-arg> 中 |
| 适用场景 | 多个 bean 共享集合 | 单个 bean 的私有集合 |
五、与 p 命名空间配合
p 命名空间不能直接定义集合,但可以引用 util 定义的集合 bean。
<util:list id="serverlist">
<value>server1</value>
<value>server2</value>
</util:list>
<bean id="userservice" class="com.example.userservice"
p:servers-ref="serverlist"
p:appname="myapp"/>这种组合在 xml 配置时代很常见:util 负责定义集合,p 负责注入。
六、底层原理
util 命名空间的解析由 utilnamespacehandler 完成,它注册了以下 beandefinitionparser:
| 标签 | 解析器 |
|---|---|
<util:list> | listbeandefinitionparser |
<util:set> | setbeandefinitionparser |
<util:map> | mapbeandefinitionparser |
<util:properties> | propertiesbeandefinitionparser |
<util:constant> | constantbeandefinitionparser |
<util:property-path> | propertypathbeandefinitionparser |
以 <util:list> 为例,解析器会创建一个 listfactorybean 的 beandefinition,把 list-class、value、ref 等配置转换为 listfactorybean 的属性。容器实例化时,listfactorybean 的 getobject() 返回实际的 list 对象。
所以 util 定义的集合本质上是工厂 bean 生产的产品,而不是直接注册的集合对象。
七、完整示例
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemalocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="serverlist" list-class="java.util.arraylist">
<value>server1.example.com</value>
<value>server2.example.com</value>
<value>server3.example.com</value>
</util:list>
<util:set id="roleset">
<value>admin</value>
<value>user</value>
<value>admin</value>
</util:set>
<util:map id="configmap">
<entry key="timeout" value="30"/>
<entry key="retry" value="3"/>
</util:map>
<util:properties id="appprops">
<prop key="app.name">myapp</prop>
<prop key="app.version">1.0</prop>
</util:properties>
<bean id="userservice" class="com.example.userservice"
p:servers-ref="serverlist"
p:roles-ref="roleset"
p:configs-ref="configmap"
p:props-ref="appprops"/>
</beans>java 类:
public class userservice {
private list<string> servers;
private set<string> roles;
private map<string, string> configs;
private properties props;
// setter 方法
public void setservers(list<string> servers) { this.servers = servers; }
public void setroles(set<string> roles) { this.roles = roles; }
public void setconfigs(map<string, string> configs) { this.configs = configs; }
public void setprops(properties props) { this.props = props; }
}八、注意事项
1. 需要声明 util 命名空间和 schemalocation
缺少 xmlns:util 或 spring-util.xsd,标签不会被解析。
2. id 必须唯一
util 定义的集合是独立 bean,id 不能与其他 bean 重复。
3. 集合元素类型
<value> 默认是 string,spring 会尝试类型转换。如果集合是 list<integer>,spring 会自动把 "30" 转成 30。
4. <util:properties> 的 location
加载外部文件时,路径要写对:classpath:db.properties 或 file:/opt/config/db.properties。
5. 不支持数组
util 命名空间没有 <util:array>。需要数组时,用 <util:list> 定义 list,注入到数组类型的属性时 spring 会自动转换。
6. 现代项目中使用减少
spring boot 以 java 配置和注解为主,util 命名空间主要出现在老项目和传统 xml 配置中。理解它有助于维护旧代码。
九、总结
| 维度 | 核心要点 |
|---|---|
| 作用 | 定义独立、可复用的集合 bean |
| 引入 | xmlns:util + spring-util.xsd |
| 常用标签 | <util:list>、<util:set>、<util:map>、<util:properties>、<util:constant>、<util:property-path> |
| 与标准集合区别 | util 定义的集合是独立 bean,可被多个 bean 引用 |
| 与 p 命名空间配合 | util 定义集合,p 负责注入 |
| 底层 | utilnamespacehandler + 各标签的 beandefinitionparser,本质是工厂 bean |
| 适用场景 | 多个 bean 共享集合、需要单独获取集合 bean、加载 properties 文件 |
| 注意事项 | 需要命名空间声明、id 唯一、不支持数组、现代项目使用减少 |
util 命名空间是 spring xml 配置时代管理集合 bean 的标准工具。它让集合从“某个 bean 的私有属性”变成“容器中可共享的资源”,在需要多个 bean 共用同一组配置或集合时非常有用。虽然在新项目中已经很少直接使用,但阅读和维护老项目时,掌握 util 命名空间能帮你快速理解配置结构。
到此这篇关于spring util 命名空间详解的文章就介绍到这了,更多相关spring util 命名空间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论