Spring 配置标签——util标签
一、配置applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
<bean id="blankDisc" class="com.hk.BlankDisc" c:_0="aaaa" c:_1="111"/>
<bean id="person" class="com.hk.Person">
<property name="list" ref="list"/>
<property name="map" ref="map"/>
<property name="set" ref="set"/>
</bean>
<util:list id="list">
<value>listValue1111</value>
<value>listValue2222</value>
<value>listValue3333</value>
</util:list>
<util:map id="map">
<entry key="map1" value="11111"/>
<entry key="map2" value="22222"/>
<entry key="map3" value="33333"/>
</util:map>
<util:set id="set">
<value>setValue1</value>
<value>setValue2</value>
<value>setValue3</value>
</util:set>
</beans>
二、配置javabean
package com.hk; import java.util.List; import java.util.Map; import java.util.Set; /** * User: muyunlong * Date: 2017/8/6 上午7:53 * version: 1.0 */ public class Person { private List list; private Set set; private Map map; public void setList(List list) { this.list = list; } public void setSet(Set set) { this.set = set; } public void setMap(Map map) { this.map = map; } public List getList() { return list; } public Set getSet() { return set; } public Map getMap() { return map; } }
三、新建测试类
package com.hk; import javafx.application.Application; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * Hello world! * */ public class App { @Autowired private static CompactDisc compactDisc; public static void main( String[] args ) { ApplicationContext application = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); CompactDisc c = (CompactDisc) application.getBean("blankDisc"); c.play(); Person p = (Person) application.getBean("person"); List list = p.getList(); for (int i = 0; i < list.size(); i++) { String aa = (String) list.get(i); System.out.println(aa); } } private void play(){ compactDisc.play(); } }
四、总结
spring util-命名空间中的元素
元素 | 描述 |
<util:constant> | 引用某个类型public static 域,并将其暴露为bean |
<util:list> | 创建一个java.util.list类型的bean,其中包含值或者引用 |
<util:map> | 创建一个java.util.map类型的bean,其中包含值或者引用 |
<util:properties> | 创建一个java.util.properties类型的bean |
<util:property-path> | 引用一个属性(或内嵌属性),并将其暴露为bean |
<util:set> | 创建一个java.util.set类型的bean,其中包含值或者引用 |