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

关于spring中<util:/>的配置的更多相关文章

  1. Spring - IoC(8): 基于 Annotation 的配置

    除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普 ...

  2. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  3. 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法

    Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法    获取Class类实例的方法:   类名.class   实例名.getClass()   Class.forNam ...

  4. 关于Spring中的<context:annotation-config/>配置

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  5. 关于Spring中的<context:annotation-config/>配置(开启注解)

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  6. 关于Spring中的<context:annotation-config/>配置作用

    转自:https://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html 当我们需要使用BeanPostProcessor时,直接在Spri ...

  7. Spring中的<context:annotation-config/>配置

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  8. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

  9. spring中关于<context:component-scan>的使用说明(转)

    https://blog.csdn.net/liuxingsiye/article/details/52171508 通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个表情,配置 ...

  10. spring中关于<context:component-scan>的使用说明

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

随机推荐

  1. 第六章 Odoo 12开发之模型 - 结构化应用数据

    在本系列文章第三篇Odoo 12 开发之创建第一个 Odoo 应用中,我们概览了创建 Odoo 应用所需的所有组件.本文及接下来的一篇我们将深入到组成应用的每一层:模型层.视图层和业务逻辑层. 本文中 ...

  2. TP5隐藏index.php

    一,找到/public/.htaccess文件,如果你的入口文件已经移动到根目录下,那么你的.htaccess文件也要剪切到根目录下,总之要确保.htaccess跟入口的index.php保持同级. ...

  3. RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景?

    RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? RxJS/Cycle.js 与 React/Vue 相比更适用于什么样的应用场景? - 知乎 https://www ...

  4. PAT甲级——A1035 Password

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  5. 微服务注册发现集群搭建—单机版(Registrator+Consul+Consul-template+nginx)

    1.创建模板文件 docker-compose.yml #backend web application, scale this with docker-compose scale web=3 web ...

  6. 评价目标检测(object detection)模型的参数:IOU,AP,mAP

    首先我们为什么要使用这些呢? 举个简单的例子,假设我们图像里面只有1个目标,但是定位出来10个框,1个正确的,9个错误的,那么你要按(识别出来的正确的目标/总的正确目标)来算,正确率100%,但是其实 ...

  7. Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  8. 有趣的纯CSS实现动态晴阴雨雪

    我们先来看看实现的效果吧 非常的美腻,对吧.这个是纯css,且单标签实现的哦~ 先贴完整代码,我们再来看看这个里面究竟有什么可以借鉴的知识点 <!DOCTYPE html> <htm ...

  9. Django项目:CRM(客户关系管理系统)--27--19PerfectCRM实现King_admin数据修改

    登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html {#table_data_list.html#} {## ————————08PerfectCR ...

  10. Django项目:CRM(客户关系管理系统)--23--15PerfectCRM实现King_admin多条件过滤

    登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html list_filter = ('source','consultant','consult_co ...