探索<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/>元素
比如某类存在如下字段定义
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
原创地址:http://wutheringsea.iteye.com/blog/647924
相关推荐
<artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>...
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 字符集 过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class...
util.js 功能 54<br/>7.1 $() 54<br/>7.2 addOptions and removeAllOptions 54<br/>7.3 addRows and removeAllRows 55<br/>7.4 getText 56<br/>7.5 getValue 56<br/>7.6 getValues 57<br/>7.7 onReturn 57<br/>7.8 ...
<filter-name>CAS Filter</filter-name> <filter-class> edu.yale.its.tp.cas.client.filter.CASFilter </filter-class> <!-- server login url --> <init-param> <param-name> edu.yale.its.tp...
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> ...
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport...
09. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10. </listener> 11. 12. <servlet> 13. <servlet-name>spring</servlet-name> 14. <servlet-class>org.spring...
<groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--mapper--> <dependency> <groupId>tk.mybatis</groupId...
-- <version>3.6.1</version> --> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 将源码打包成JAR包 -...
本文实例讲述了Spring使用util:命名空间简化配置操作。分享给大家供大家参考,具体如下: 一 配置 <?xml version=1.0 encoding=GBK?> <!-- 指定Spring配置文件的根元素和Schema 导入p:命名空间和util:...
其它的运行符可以作用于数字和日期,但不能作用于字符串,大部分的时候,使用gt等字母运算符代替>会有更好的效果,因为 FreeMarker会把>解释成FTL标签的结束字符,当然,也可以使用括号来避免这种情况,如:<#if (x>y)> ...
1.2添加memcached如下依赖的jar包到tomcat8.x/lib/: asm-5.1.jar couchbase-client-1.4.12.jar javolution-5.5.1.jar kryo-4.0.0.jar kryo-serializers-0.38.jar memcached-session-manager-2.0.0.jar memcached...
<artifactId>spring-boot-util</artifactId> <<version>1.0.0-SNAPSHOT</version> </dependency> 2.配置IP安全性属性 启用IP安全性 ipSecurity.enabled=true 注意:默认为ipSecurity.enabled=false 。 通过CI
由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中: 代码 4 Spring中有关Hibernate的配置信息 1. <beans> 2. <!-- 数据源的配置 ...
查询出列表,也就是返回list, 在我们这个例子中也就是 List<User> , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...
<interceptor name="myInterceptor" class="util.MyInterceptor"></interceptor> 定义拦截器的值栈空间 <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> ...
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script> 客户端调用JAVA基本类: function getJavaDate(){ JDate.getYear(load); } function load(data){ alert(data+...
微服务架构集大成者—Spring Cloud https://www.jianshu.com/p/3899d7f47303 api首页路径:http://192.168.0.107:9090/swagger-ui.html //端口可以在配置文件修改, F:\test\java\java_learn\idea_work\...
9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用 ...
<bean id="redisSessionDAO" class="hxk.util.redis.RedisSessionDAO"> <property name="redisManager" ref="redisManager"/> <property name="timeToLiveSeconds" value="180"/> </bean> <!-- 配置shiro提供...