spring 3配置文件中如何注入map list set等类型
2013-07-25 11:54
495 查看
首先写个 javabean类吧,如下
配置文件中配置
代码中调用
package com.bean; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class MessageBean { private String username; private String password; private int size; private List<String> citys; private Set<String> friends; private Map<Integer,String> books; private Properties props; public void setProps(Properties props) { this.props = props; } public void setFriends(Set<String> friends) { this.friends = friends; } public void setSize(int size) { this.size = size; } public void setPassword(String password) { this.password = password; } public void setUsername(String username) { this.username = username; } public void show(){ System.out.println(username); System.out.println(password); System.out.println(size); System.out.println("----------"); for(String str:citys){ System.out.println(str); } System.out.println("----------"); for(String str:friends){ System.out.println(str); } System.out.println("---------"); Set<Integer> keys = books.keySet(); for(Integer key:keys){ System.out.println(key+" "+books.get(key)); } System.out.println("---------"); Set params = props.keySet(); for(Object obj:params ){ System.out.println(obj+" : " +props.getProperty(obj.toString())); } } public void setCitys(List<String> citys) { this.citys = citys; } public void setBooks(Map<Integer, String> books) { this.books = books; } }
配置文件中配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="userdao" class="com.dao.impl.HibernateUserDAO"> </bean> <!-- setter方式注入 --> <bean id="useraction" class="com.action.UserAction"> <property name="userDao" ref="userdao"> </property> </bean> <!-- 构造器方式注入 --> <bean id="useraction1" class="com.action.UserAction1"> <constructor-arg index="0" ref="userdao"/> </bean> <!-- 各种类型值注入的写法 --> <bean id="messagebean" class="com.bean.MessageBean"> <property name="props"> <props> <prop key="url">http://www.tom.com</prop> <prop key="username">zhangsan</prop> <prop key="password">123456789</prop> </props> </property> <property name="books"> <map> <entry key="10" value="CoreJava"> </entry> <entry key="11" value="JavaWeb"> </entry> <entry key="12" value="SSH2"> </entry> </map> </property> <property name="friends"> <set> <value>张三</value> <value>李四</value> <value>王五</value> </set> </property> <property name="citys"> <list> <value>北京</value> <value>上海</value> <value>深圳</value> </list> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>1234</value> </property> <property name="size"> <value>15</value> </property> </bean> </beans>
代码中调用
String[] configs = {"applicationContext.xml"}; ApplicationContext ac = new ClassPathXmlApplicationContext(configs); MessageBean msgBean = (MessageBean)ac.getBean("messagebean"); msgBean.show();
相关文章推荐
- spring 3配置文件中如何注入map list set等类型
- spring 配置文件中如何注入map list set等类型
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中注入List Set Map Properties的配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring List,Set,Map,Properties,array的配置文件注入方式
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring基于xml配置文件对list、set、map集合的属性注入
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring 配置文件 list map set
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Spring中注入List,Set,Map,Properties的xml文件配置方法
- spring如何注入List,Set,Map对象