关于spring中 的配备
探索<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 http://www.springframework.org/schema/util/spring-util-2.0.xsd">
1. <util:constant/>元素
比如某类存在如下字段定义
这样就将java代码中的常量hwStatic(在test包下的HelloWorld类中)配置给spring进行管理,id为另起的名字;
又eg:
<util:constant id="maxValue" static-field="java.lang.Integer.MAX_VALUE"/>
2. <util:property-path/>元素
这里path="helloworld.hello"就是指bean为"helloworld"的属性hello。
3. <util:properties/>元素
"classpath:"表明,将从类路径上查找并装载xxx属性文件.
4. <util:list/>元素
它的作用就是在spring启动初始化bean时,给listUtil这个list赋值为这四个值。 下面的同理
5. <util:map/>元素
为了简化MapFactoryBean对象的使用,可使用如下代码 :
6. <util:set/>元素
同样的,为了简化SetFactoryBean对象,可使用如下代码 :
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>命名空间后,等价于
实例:http://blog.****.net/daryl715/archive/2007/09/26/1802292.aspx
原创地址:http://wutheringsea.iteye.com/blog/647924