Spring 2.0的新标签----util实例
一个测试Bean:
public class TestBean {
return co;
}
public void setCo(Integer co) {
this.co = co;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean id="test" class="util.Test">
<property name="list" ref="list">
</property>
<property name="map" ref="map">
</property>
<property name="properties" ref="properties">
</property>
<property name="array" ref="array">
</property>
<property name="set" ref="set">
</property>
<property name="constantValue" ref="constantValue">
</property>
</bean>
<util:list id="list" list-class="java.util.ArrayList">
<value>listValue1</value>
<value>listValue2</value>
</util:list>
<util:map id="map">
<entry key="key1" value="mapValue1"></entry>
<entry key="key12" value="mapValue2"></entry>
</util:map>
<util:set id="set" set-class="java.util.TreeSet">
<value>setValue1</value>
<value>setValue2</value>
</util:set>
<util:properties id="properties" location="classpath:/util/config.properties">
</util:properties>
<util:list id="array">
<value>arrayValue1</value>
<value>arrayValue2</value>
</util:list>
<!-- 定义常量 -->
<util:constant id="constantValue" static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
<!-- 为bean属性成员定义id,供其他bean引用,但自身属性不能使用,只能用在其他的bean上 -->
<util:property-path id="source" path="test.constantValue"/>
<bean id="testBean" class="util.TestBean">
<property name="co">
<ref bean="source"/>
</property>
</bean>
</beans>
测试程序:
package util;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
private List list;
private Map map;
private String[] array;
private Set set;
private Properties properties;
private Integer constantValue;
public Integer getConstantValue() {
return constantValue;
}
public void setConstantValue(Integer constantValue) {
this.constantValue = constantValue;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("util/applicationContext.xml");
Test t=(Test)ctx.getBean("test");
t.printArray(t.getArray());
t.printList(t.getList());
t.printMap(t.getMap());
t.printSet(t.getSet());
t.printProperties(t.getProperties());
System.out.println(t.getConstantValue());
TestBean tb=(TestBean)ctx.getBean("testBean");
System.out.println("use util:property-path:"+tb.getCo());
}
public void printList(List result){
System.out.println("list value:");
for (Iterator iterator = result.iterator(); iterator.hasNext();) {
String element = (String) iterator.next();
System.out.println(element);
}
}
public void printMap(Map result){
System.out.println("map value:");
for (Iterator iterator = result.keySet().iterator(); iterator.hasNext();) {
String element = (String) iterator.next();
System.out.println(element);
}
}
public void printSet(Set result){
System.out.println("set value:");
for (Iterator iterator = result.iterator(); iterator.hasNext();) {
String element = (String) iterator.next();
System.out.println(element);
}
}
public void printArray(String[] result){
System.out.println("array value:");
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
public void printProperties(Properties result){
System.out.println("properties value:");
Enumeration enu2=result.propertyNames();
while(enu2.hasMoreElements()){
String key = (String)enu2.nextElement();
System.out.println(key);
}
}
}
测试结果:
array value:
arrayValue1
arrayValue2
list value:
listValue1
listValue2
map value:
key1
key12
set value:
setValue1
setValue2
properties value:
prop2
prop1
8
use util:property-path:8