Spring中注入List,Set,Map,Properties
2012-12-27 09:44
381 查看
下面的例子展示了如何注入
List – <list/>
Set – <set/>
Map – <map/>
Properties – <props/>
配置文件:
运行:
我们也可以使用ListFactoryBean。The ‘[b]ListFactoryBean‘ class provides developer a way to create a concrete List collection class (ArrayList and LinkedList) in Spring’s bean configuration file.[/b]
或者:加入:xmlns:util="http://www.springframework.org/schema/util"然后就可以:
[b][b]SetFactoryBean[/b] The ‘SetFactoryBean‘ class provides developer a way to create a concrete Set collection (HashSet and TreeSet) in Spring’s bean configuration file.[/b]
set和上面的一样:
[b][b]MapFactoryBean[/b] The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.[/b]
map也一样:
List – <list/>
Set – <set/>
Map – <map/>
Properties – <props/>
Spring beans
import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Customer { private List<Object> lists; private Set<Object> sets; private Map<Object, Object> maps; private Properties pros; //... }
配置文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> <!-- java.util.Map --> <property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </property> <!-- java.util.Properties --> <property name="pros"> <props> <prop key="admin">admin@nospam.com</prop> <prop key="support">support@nospam.com</prop> </props> </property> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> </beans>
运行:
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
我们也可以使用ListFactoryBean。The ‘[b]ListFactoryBean‘ class provides developer a way to create a concrete List collection class (ArrayList and LinkedList) in Spring’s bean configuration file.[/b]
<bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="lists"> <bean class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="targetListClass"> <value>java.util.ArrayList</value> </property> <property name="sourceList"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> </bean> </property> </bean>
或者:加入:xmlns:util="http://www.springframework.org/schema/util"然后就可以:
<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.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="lists"> <util:list list-class="java.util.ArrayList"> <value>1</value> <value>2</value> <value>3</value> </util:list> </property> </bean> </beans>
[b][b]SetFactoryBean[/b] The ‘SetFactoryBean‘ class provides developer a way to create a concrete Set collection (HashSet and TreeSet) in Spring’s bean configuration file.[/b]
set和上面的一样:
<bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="sets"> <util:set set-class="java.util.HashSet"> <value>1</value> <value>2</value> <value>3</value> </util:set> </property> </bean>
[b][b]MapFactoryBean[/b] The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.[/b]
map也一样:
<bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="maps"> <util:map map-class="java.util.HashMap"> <entry key="Key1" value="1" /> <entry key="Key2" value="2" /> <entry key="Key3" value="3" /> </util:map> </property> </bean>
相关文章推荐
- Spring中注入List,Set,Map,Properties的xml文件配置方法
- List,Set,Map,Properties的Spring注入实例
- Spring中注入List Set Map Properties的配置文件
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- Spring注入集合的值List,Set,Map,Properties
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- spring List,Set,Map,Properties,array的配置文件注入方式
- Spring注入容器类型List、Map、Properties、Set
- spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装(引用)
- java_spring_List,Map,Properties,Set注入与遍历
- List,Set,Map,Properties的Spring注入实例
- Spring注入Properties、List、Map、Set
- Spring中注入List,Set,Map,Properties
- spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装
- spring注入Set,List,Properties和Map
- (3) spring注入Properties,List,Set,Map
- spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装
- List,Set,Map,Properties的Spring注入实例
- spring的list ,set,map,properties注入(set,get注入)
- Spring集合 (List,Set,Map,Properties) 实例