关于spring中util:/的配置
解决redis设置缓存时间找到的帖子,我这个初学者需要学习的还是很多的。
原文地址:http://www.doc100.net/bugs/t/216322/index.html
探索<util/>命名空间 事情的发展总是一段曲折前进的过程。当Spring刚出现时,开发者可以使用<list/>、<map/>、<set/>等元素定义集合,然而这些集合不能够在不同的受管Bean间进行复用。尽管开发者可以采用抽象Bean机制实现复用,但实在不怎么优雅。与此同时,开发者借助ListFactoryBean、MapFactoryBean和SetFactoryBean等对象能够定义出可供复用的集合。然而,这也不是很友好的做法。再后来,<util/>命名空间被Spring 2.x引入,这才使得集合的定义变得简单。
首先在spring的配置文件中添加
<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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util <a href="http://www.springframework.org/schema/util/spring-util-2.0.xsd">http://www.springframework.org/schema/util/spring-util-2.0.xsd">
1. <util:constant/>元素 比如某类存在如下字段定义
public static final String hwStatic = "hello static constant";
如果希望以上属性取值作为受管Bean,可以如下配置:
<util:constant id="hwConstant" static-field="test.HelloWorld.hwStatic"/>
这样就将java代码中的常量hwStatic(在test包下的HelloWorld类中)配置给spring进行管理,id为另起的名字; 又eg:
<util:constant id="maxValue" static-field="java.lang.Integer.MAX_VALUE"/>
2. <util:property-path/>元素
<bean id="property-path" path="helloWorld.hello"/> <bean id="helloWorld" class="test.HelloWorld"> <property name="hello" value="hi"/> </bean>
这里path="helloworld.hello"就是指bean为"helloworld"的属性hello。
3. <util:properties/>元素 "classpath:"表明,将从类路径上查找并装载xxx属性文件.
<util:properties id="xxx" location="classpath:xxxxx.properties">
4. <util:list/>元素
<util:list id="listUtil" list-class="java.util.ArrayList"> <value>first</valuse> <value>two</valuse> <value>three</valuse> <value>ten</valuse> </util:list>
它的作用就是在spring启动初始化bean时,给listUtil这个list赋值为这四个值。 下面的同理
5. <util:map/>元素
<bean id="abstractCollectionBean" abstract="true"> <property name="map"> <map> <entry key="mapKey1" value="mapValue1"> <entry key="mapKey2" value="mapValue2"> </map> </property> </bean>
继承了abstractCollectionBean的子bean
<bean id="CollectionBean" class="test.CollectionBean" parent="abstractCollectionBean"> <property name="map"> <map merge="true" key-type="java.lang.String" value-type="java.lang.String"> <entry key="mapKey1" value="mapValue1Override"/> <entry> <key><value>mapKey2</value></key> <value>mapValue2</value> </entry> <entry key="testBean" value-ref="testBean"> </map> </property> </bean> <bean id="testBean" class="test.TestBean" />
为了简化MapFactoryBean对象的使用,可使用如下代码 :
<util:map id="mapUtil" map-class="java.util.HashMap"> <entry key="1" value="first"> <entry key="2" value="two"> <entry key="3" value="three"> </util:map>
6. <util:set/>元素 同样的,为了简化SetFactoryBean对象,可使用如下代码 :
<util:set id="setUtil" set-class="java.util.HashSet"> <value>first</value> <value>two</value> <value>three</value> </util:set>
7. 使用<p/>命名空间 在xml头加入 xmlns:p=http://www.springframework.org/schema/p;这里的p就是property的意思。 例如如下代码:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" ref="locations"/> <property name="order" value="1"/> </bean> <util:list id="locations"> <value>userinfo.properties</value> </util:list>
在导入了</p>命名空间后,等价于
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:locations-ref="locations" p:order="1" /> <util:list id="locations"> <value>userinfo.properties</value> </util:list>
实例:http://blog.csdn.net/daryl715/archive/2007/09/26/1802292.aspx
转载于:https://www.cnblogs.com/zypu/p/6593827.html
关于spring中util:/的配置相关推荐
- Quartz 在 Spring 中如何动态配置时间
在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间 ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开 ...
- Spring中的Bean配置、属性配置、装配内容详细叙述
文章目录 1.Bean的配置 1.1.配置方式 2.Bean的实例化 2.1.构造器实例化 2.2.静态工厂方式实例化 2.3.实例工厂方式实例化 3.Bean的作用域 3.1.作用域的种类 4.Be ...
- Spring中的Bean配置
IOC&DI概述 OPC(Inversion of Control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源.作为回应,容器适时的返回资源.而应用了IOC ...
- spring中的事务配置
为什么80%的码农都做不了架构师?>>> 一 简介 spring中提供了三种事务管理的方法. 编程式事务管理 :亦即自己编写事务管理的代码,通过注入获取到spring中的事务管 ...
- 在Spring中使用JDBCJobStore配置Quartz
我将开始一些有关Quartz Scheduler内部,提示和技巧的系列文章,这是第0章-如何配置持久性作业存储. 在Quartz中,您基本上可以在将作业和触发器存储在内存中以及在关系数据库中进行选择( ...
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能(i18n或者国际化).与HierarchicalMessageSource一起使用,它还能够处理 ...
- Spring中的scope配置和@scope注解
Scope,也称作用域,在 Spring IoC 容器是指其创建的 Bean 对象相对于其他 Bean 对象的请求可见范围.在 Spring IoC 容器中具有以下几种作用域:基本作用域(single ...
- Spring讲解二:Spring中的Bean配置0
一.IOC &DI 概述 IOC(Inversion of Control):思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源.而应用I ...
最新文章
- 编译-链接-运行-环境配置各种error汇总
- linux mq查看端口_通过rabbitmq的web监控mcollective的状态
- Android 实现 调用 WPS Office手机版接口
- 【ARM】Tiny4412裸板编程之MMU(段1M)
- matlab概率论实验 分别掷硬币1,基于Matlab的概率论仿真实验
- nyoj 寻找最大数
- XBRL 可扩展商业报告语言
- 啊哈C语言 第三章 【代码】【习题答案】
- linux 下librtmp源码,linux下基于libRTMP的接收流媒体的程序
- 关于html5毕业论文设计任务书,毕业论文设计任务书(精选多篇)
- esp32实现万能红外遥控器 基于开源红外码库IREXT
- CME上位机控制Copley驱动MAXON电机
- 随身Win8系统DIY
- wintogo取消屏蔽本地磁盘
- 记录一直以来看过的电视剧、电影及书籍
- 华为认证hcia含金量_华为认证那个证书有什么用?
- docker查看mysql镜像版本_Docker 查看镜像信息
- 第一性原理(DFT)基础知识
- 基于深度学习的多任务人脸属性分析(基于飞桨PaddlePaddle)
- kuwo.php,酷我音乐公开api