戴着假发的程序员 出品
注入map集合
spring应用手册(第一部分)
map集合和list、set的不同是,map是双列集合。所以注入时稍有不同,但是大致一样。
要使用标签 map 和 entry。 我们看案例:
准备Account类。拥有属性account,有参和无参构造方法。
/**
* @author 戴着假发的程序员
*
* @description 账户
*/
public class Account {
private String account;
public Account(){
}
public Account(String account) {
this.account = account;
}
public void setAccount(String account) {
this.account = account;
}
@Override
public String toString(){
return "account:"+account;
}
}
准备group类,拥有两个map集合,其中一个存储账户名字(String)类型,以账号(Integer)作为key。另一个存储Account对象,以账号(Integer)作为key。并且提供有参数构造和无参数构造。
/**
* @author 戴着假发的程序员
*
* @description
*/
public class Group {
//账户名字集合
private Map<Integer,String> accNames;
//账户对象集合
private Map<Integer,Account> accounts;
public void setAccNames(Map<Integer,String> accNames) {
this.accNames = accNames;
}
public void setAccounts(Map<Integer,Account> accounts) {
this.accounts = accounts;
}
//无参数构造
public Group(){
}
//有参数构造
public Group(Map<Integer,String> accNames, Map<Integer,Account> accounts) {
this.accNames = accNames;
this.accounts = accounts;
}
public void showNames(){
System.out.println(accNames);
}
public void showAccount(){
System.out.println(accounts);
}
}
在配置文件中进行注入:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName" 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.xsd">
<!-- 准备三个accountBean -->
<bean id="account1" class="com.boxuewa.dk.demo2.beans.Account">
<constructor-arg name="account" value="卡卡西"/>
</bean>
<bean id="account2" class="com.boxuewa.dk.demo2.beans.Account">
<constructor-arg name="account" value="佐助"/>
</bean>
<bean id="account3" class="com.boxuewa.dk.demo2.beans.Account">
<constructor-arg name="account" value="鸣人"/>
</bean>
<!-- 注册一个group -->
<bean id="group" class="com.boxuewa.dk.demo2.beans.Group">
<property name="accNames">
<!-- 使用set标签注入简单set集合属性 -->
<map>
<entry key="9527" value="路飞"/>
<entry key="9528" value="佐助"/>
<entry key="9529"><value>鸣人 </value></entry>
</map>
</property>
<property name="accounts">
<!-- set标签注入引用类型属性 -->
<map>
<entry key="110"> <ref bean="account1"/></entry>
<entry key="111"> <ref bean="account2"/></entry>
<entry key="112" value-ref="account3"/>
</map>
</property>
</bean>
</beans>
注意几个问题:
1、上面的注入方式同样适合构造方法的注入:
<!-- 注册一个group -->
<bean id="group" class="com.boxuewa.dk.demo2.beans.Group">
<constructor-arg name="accNames">
<!-- 使用set标签注入简单set集合属性 -->
<map>
<entry key="9527" value="路飞"/>
<entry key="9528" value="佐助"/>
<entry key="9529"><value>鸣人 </value></entry>
</map>
</constructor-arg>
<constructor-arg name="accounts">
<!-- set标签注入引用类型属性 -->
<map>
<entry key="110"> <ref bean="account1"/></entry>
<entry key="111"> <ref bean="account2"/></entry>
<entry key="112" value-ref="account3"/>
</map>
</constructor-arg>
</bean>
2、同样我们可以在注入bean的同时实例化:
<constructor-arg name="accounts">
<!-- set标签注入引用类型属性 -->
<map>
<entry key="110"> <ref bean="account1"/></entry>
<entry key="111"> <ref bean="account2"/></entry>
<entry key="112" value-ref="account3"/>
<entry key="112">
<bean class="com.boxuewa.dk.demo2.beans.Account">
<constructor-arg name="account" value="翠花"/>
</bean>
</entry>
</map>
</constructor-arg>
3、map的key可以是任何类型,所以其实map的可以也可使用 key-ref引用其他类型,当然就需要你自己制定一个专门的类型作为key。 这种方式我们一般都会使用。