Spring、基本类型属性和集合类型属性的注入
2015-11-10 11:34
459 查看
Spring 还可以对基本属性和集合类型属性进行注入:
public interface PersonIService {
public String getBaseProperty();
public Set<String> getSets();
public List<Integer> getList();
public Properties getProperties();
public Map<String, String> getMaps();
}
beans.xml:
测试代码:
public interface PersonIService {
public String getBaseProperty();
public Set<String> getSets();
public List<Integer> getList();
public Properties getProperties();
public Map<String, String> getMaps();
}
public class PersonServiceImpl implements PersonIService { private String baseProperty; private Set<String> sets; private List<Integer> list; private Properties properties; private Map<String,String> maps; public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public List<Integer> getList() { return list; } public void setList(List<Integer> list) { this.list = list; } public Set<String> getSets() { return sets; } public String getBaseProperty() { return baseProperty; } public void setBaseProperty(String baseProperty) { this.baseProperty = baseProperty; } public void setSets(Set<String> sets) { this.sets = sets; } }
beans.xml:
<bean id="personIService" class="cn.server.impl.PersonServiceImpl"> <property name="baseProperty" value="value:基本属性1" /> <property name="sets"> <set> <value>set装配-value1</value> <value>set装配-value2</value> <value>set装配-value3</value> </set> </property> <property name="list"> <list> <value>11</value> <value>12</value> <value>13</value> </list> </property> <property name="properties"> <props> <prop key="property1">value1</prop> <prop key="property2">value2</prop> <prop key="property3">value3</prop> </props> </property> <property name="maps"> <map> <entry key="map1" value="value1" /> <entry key="map2" value="value2" /> <entry key="map3" value="value3" /> </map> </property> </bean>
测试代码:
@Test public void otherTest(){ ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); PersonIService personIService=(PersonIService)ac.getBean("personIService"); System.out.println("========基本属性注入============"); System.out.println(personIService.getBaseProperty()); System.out.println("========Set集合类型注入============"); for(String str : personIService.getSets()){ System.out.println(str); } System.out.println("========list集合类型注入============"); for(Integer i : personIService.getList()){ System.out.println(i); } System.out.println("========properties集合类型注入============"); for(Object key : personIService.getProperties().keySet()){ System.out.println(key+"="+personIService.getProperties().getProperty(key.toString())); } System.out.println("========map集合类型注入============"); for(Object key : personIService.getMaps().keySet()){ System.out.println(key+"="+personIService.getMaps().get(key)); } }
相关文章推荐
- Spring mvc 中log4j的配置和使用
- Java6开发WebService进阶
- Spring、基本类型属性和集合类型属性的注入
- Java6开发WebService入门
- WebSphereMQ发送消息和读取消息
- spring mvc @PathVariable绑定URI模板变量值
- 使用Eclipse构建Maven的SpringMVC项目
- Android-08 如何更改eclipse中Android程序的默认包名
- java开发中基于JDBC连接数据库实例总结
- spring 整合hibernate项目配置文件
- java.lang.IllegalStateException: Fragment already added ListFragment异常的处理
- JavaEE 学习整理笔记: Servlet 介绍
- Java中唯一数的生成
- MyEclipse中xml的区别xml(advanced Templates)、xml(Basic Templates)、xml schema
- HashTable HashMap TreeSet TreeMap性能测试,比较。
- java——泛型
- HBase简单代码实例(Java)
- 【Java并发编程】:线程间通信中notifyAll造成的早期通知问题(含代码)
- 【Java并发编程】:线程间通信中notify通知的遗漏(含代码)
- spring mvc实现查询