Spring util-namespace下标签相关操作

时间:2023-12-05 23:52:26

java代码

package com.stono.sprtest;

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBean3 {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans3.xml");
List<?> list = (List<?>) context.getBean("instruments");
for (Object object : list) {
System.out.println(object);
}
Elvis elvis = (Elvis) context.getBean("elvis");
System.out.println(elvis.getName()); // #{'abc'[0]}
System.out.println(elvis.getName2()); // #{map['b']}
@SuppressWarnings("unchecked")
Map<String, ?> map = (Map<String, ?>) context.getBean("map");
System.out.println(map.get("c"));
System.out.println(map.get("d")); // 从属性文件中读取内容;
System.out.println(map.get("e")); // 打出了全部
System.out.println(map.get("e1"));
Object x = map.get("f"); // x对象为properties对象;
System.out.println(x); // 打出了全部
System.out.println(map.get("f1"));
Set<?> set = (Set<?>) context.getBean("set");
System.out.println(set);
Object prop = context.getBean("prop"); // 获取属性文件对象
System.out.println(prop); // {stono.amber=Amber, stono.minstrel=Minstrel}
System.out.println(map.get("g"));// #{set.?[age eq 10]}表达式
System.out.println(map.get("h"));// #{set.^[age gt 11]}表达式
System.out.println(map.get("i"));// #{set.$[age gt 11]}表达式
Object x2 = map.get("j");// #{ .![]}返回ArrayList
System.out.println(x2);// #{set.![age]}表达式 result:[10, 15, 20, 25, 25];即便只有一个元素,也是返回ArrayList
System.out.println(map.get("k"));// #{set.?[age gt 10]}表达式
Object list2 = context.getBean("list2");
System.out.println(list2);
System.out.println(context.getBean("pi"));// util:constant标签
Object ppath = context.getBean("ppath"); // util:property-path标签
System.out.println(ppath);
System.out.println(map.get(Math.PI)); //map中key-ref的使用;
}
}

xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<util:list id="instruments">
<bean class="com.stono.sprtest.Cymbal"></bean>
<bean class="com.stono.sprtest.Harmonica"></bean>
<bean class="com.stono.sprtest.Saxophone"></bean>
</util:list>
<bean id="elvis" class="com.stono.sprtest.Elvis">
<property name="instrument" value="#{instruments[0]}"></property>
<property name="name" value="#{'abc'[0]}"></property>
<property name="name2" value="#{map['b']}"></property>
</bean>
<util:map id="map">
<entry key="a" value="a"></entry>
<entry>
<key>
<value>b</value>
</key>
<value>b</value>
</entry>
<entry key="c" value-ref="instruments"></entry>
<entry key="d" value="#{prop['stono.amber']}"></entry>
<entry key="e" value="#{systemEnvironment }"></entry>
<entry key="e1" value="#{systemEnvironment['USERPROFILE'] }"></entry>
<entry key="f" value="#{systemProperties}"></entry>
<entry key="f1" value="#{systemProperties['os.name']}"></entry>
<entry key="g" value="#{set.?[age eq 10]}"></entry>
<entry key="h" value="#{set.^[age gt 11]}"></entry>
<entry key="i" value="#{set.$[age gt 11]}"></entry>
<entry key="j" value="#{set.![age]}"></entry>
<entry key="k" value="#{set.?[age gt 10]}"></entry>
<entry key-ref="pi" value="PI"></entry>
</util:map>
<util:set id="set">
<bean class="com.stono.sprtest.Duke" p:age="10"></bean>
<bean class="com.stono.sprtest.Duke" p:age="15"></bean>
<bean class="com.stono.sprtest.Duke" p:age="20"></bean>
<bean class="com.stono.sprtest.Duke" p:age="25"></bean>
<bean class="com.stono.sprtest.Duke" p:age="25"></bean>
</util:set>
<util:properties id="prop" location="classpath:settings.properties">
</util:properties>
<util:list id="list2">
<bean class="java.lang.Integer">
<constructor-arg>
<value>10</value>
</constructor-arg>
</bean>
<bean class="java.lang.String">
<constructor-arg>
<value>typography</value>
</constructor-arg>
</bean>
<bean class="java.util.Calendar" factory-method="getInstance"></bean>
</util:list>
<util:constant id="pi" static-field="java.lang.Math.PI"/>
<bean id="duke" class="com.stono.sprtest.Duke" p:age="80"></bean>
<util:property-path id="ppath" path="duke.age"/>
</beans>