要使用<util>标签,必须在XML中加入util名称空间(namespace):

xmlns:util="http://http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd"

分别使用<util:list>、<util:map>、<util:set>、<util:properties>等标签。

用它来取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean。

其中的<util:properties>标签可以通过location属性指定其properties属性文件的位置。

除了以上使用的标签外,还有<util:constant>可用来设置静态数据成员(staticfield),而免于设置org.springframework.beans.factory.config.FieldRetrievingFactoryBean,例如:

<bean id="circle" class="cn.itcast.spring._other.Circle">

<property name="pi">

<util:constant static-field="java.lang.Math.PI"/>

</property>

</bean>

  

 

还可以使用<util:property-path>标签为某个Bean的属性成员设置id属性,使之在容器管理中,免于设置org.springframework.beans.factory.config.PropertyPathFactoryBean,例如:

<!-- id值为PI的Bean,其值将会是circle.pi -->

<util:property-path  id="PI" path="circle.pi"/>

修改集合对象中示例的Bean定义文件

<bean id="some1" class="cn.itcast.spring._util.Some">

  <property name="val" value="some instance1" />

</bean>

<bean id="some2" class="cn.itcast.spring._util.Some">

  <property name="val" value="some instance2" />

</bean>

<bean id="some3" class="cn.itcast.spring._util.Some">

  <property name="val" value="some instance3" />

</bean>

<util:list id="strArray">

  <value>Hello</value>

  <value>Welcome</value>

</util:list>

<util:list id="objArray">

  <ref bean="some1" />

  <ref bean="some2" />

  <ref bean="some3" />

</util:list>

<util:list id="list" list-class="java.util.ArrayList">

  <ref bean="some1"/>

  <ref bean="some2" />

  <ref bean="some3" />

</util:list>

<util:map id="map" map-class="java.util.HashMap" >

  <entry key="key1" value-ref="some1"/>

  <entry key="key2" value-ref="some2"/>

  <entry key="key3" value-ref="some3"/>

</util:map>

写完util配置文件后,如何给某个类赋值呢?也可以直接用配置的方式:

<bean id="someBean" class="cn.itcast.spring._util.SomeBean">

  <property name="someStrArray" ref="strArray"/>

  <property name="someObjArray" ref="objArray"/>

  <property name="someList" ref="list"/>

  <property name="someMap" ref="map"/>

</bean>

或者在类中使用@Value注解

@Value("#{strArray}")
private List<String> somoeStrArray;
@Value("#{objArray}")
private List<Some> somoeObjArray;
@Value("#{list}")
private List<Some> somoeList;
@Value("#{map}")
private Map<String,Some> someMap;

Spring依赖注入 — util命名空间配置的更多相关文章

  1. Spring依赖注入的简化配置

    一, 很久很久以前, 当我们不用@Autowire注解时, 依赖注入要么通过setter方法, 要么通过构造方法; 需要在配置文件里配置一大堆property-ref.......... 二, 若使用 ...

  2. Spring依赖注入三种方式详解

    在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...

  3. Spring依赖注入 --- 简单使用说明

    Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...

  4. Spring、Spring依赖注入与编码剖析Spring依赖注入的原理

    Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...

  5. Spring学习笔记——Spring依赖注入原理分析

    我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...

  6. java后端开发三年!你还不了解Spring 依赖注入,凭什么给你涨薪

    前言 前两天和一个同学吃饭的时候同学跟我说了一件事,说他公司有个做了两年的人向他提出要涨薪资,他就顺口问了一个问题关于spring依赖注入的,那个要求涨薪的同学居然被问懵了...事后回家想了想这一块确 ...

  7. Spring依赖注入:注解注入总结

    更多11   spring   依赖注入   注解   java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...

  8. Spring 依赖注入,在Main方法中取得Spring控制的实例

    Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...

  9. Spring依赖注入 --- 模拟实现

    Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...

随机推荐

  1. opencv 3.3.0 如何旋转图像?

    函数介绍 1. void cv::flip(InputArray src,OutputArray dst,int flipCode) 2. void cv::transpose(InputArray ...

  2. 长整形的使用及cin加速

    _int64 和 long long 那么对ACMer来说,最为关心的就是在各个OJ上交题应分别使用哪种方式了.其实方式只有有限的几种: 如果服务器是linux系统,那么定义用long long,IO ...

  3. nyoj888 取石子(九) 反Nimm博弈

    这题就是反Nimm博弈--分析见反Nimm博弈 AC代码 #include <cstdio> #include <cmath> #include <algorithm&g ...

  4. 在SpringBoot中存放session到Redis

    前言 今天你们将再一次领略到SpringBoot的开发到底有多快,以及SpringBoot的思想(默认配置) 我们将使用redis存放用户的session,用户session存放策略有很多,有存放到内 ...

  5. 快了快了,你的 MacBook Pro 和 FineUICore!

    着玻璃窗,看到星巴克里那帮人拿着MacBook喝咖啡,你是不是要默念一遍:这帮傻叉,就爱装逼! 不过话说回来,你想不想尝试下这个傻叉的感觉? 是时候了,给自己一个理由,拥有自己的 MacBook Pr ...

  6. VUE PK REACT(1)

    一.浏览器兼容性 vue: ie9+ react: ie8+ 二.安装使用 vue:1.直接引入  <script src="https://cdn.jsdelivr.net/npm/ ...

  7. linux 搭建CA服务器 http+ssl mail+ssl 扫描与抓包

    搭建CA服务器 CA服务是给服务器发放数字证书,被通信双方信任,独立的第三方机构 国内常见的CA机构 中国金融认证中心(CFCA) 中国电信安全认证中心(CTCA) 北京数字证书认证中心(BJCA) ...

  8. cips2016+学习笔记︱简述常见的语言表示模型(词嵌入、句表示、篇章表示)

    在cips2016出来之前,笔者也总结过种类繁多,类似词向量的内容,自然语言处理︱简述四大类文本分析中的"词向量"(文本词特征提取)事实证明,笔者当时所写的基本跟CIPS2016一 ...

  9. directdraw显示rgb555

    // TODO: 在此添加控件通知处理程序代码  height=width=widthBytes=0;  m_screen.SetWindowPos(&CWnd::wndBottom,0,0, ...

  10. Unhandled event loop exception No more handles

    1.错误描述 2.错误原因 3.解决办法