1.28 注入map集合

戴着假发的程序员出品

[查看视频教程]

map集合和list、set的不同是,map是双列集合。所以注入时稍有不同,但是大致一样。

要使用标签 map 和 entry。 我们看案例:

准备Account类。拥有属性account,有参和无参构造方法。

 1 /**
 2  * @author 戴着假发的程序员
 3  *  
 4  * @description 账户
 5  */
 6 public class Account {
 7     private String account;
 8     public Account(){
 9     }
10     public Account(String account) {
11         this.account = account;
12     }
13     public void setAccount(String account) {
14         this.account = account;
15     }
16     @Override
17     public String toString(){
18         return "account:"+account;
19     }
20 }

准备group类,拥有两个map集合,其中一个存储账户名字(String)类型,以账号(Integer)作为key。另一个存储Account对象,以账号(Integer)作为key。并且提供有参数构造和无参数构造。

 1 /**
 2  * @author 戴着假发的程序员
 3  *  
 4  * @description
 5  */
 6 public class Group {
 7     //账户名字集合
 8     private Map<Integer,String> accNames;
 9     //账户对象集合
10     private Map<Integer,Account> accounts;
11 
12     public void setAccNames(Map<Integer,String> accNames) {
13         this.accNames = accNames;
14     }
15     public void setAccounts(Map<Integer,Account> accounts) {
16         this.accounts = accounts;
17     }
18     //无参数构造
19     public Group(){
20     }
21     //有参数构造
22     public Group(Map<Integer,String> accNames, Map<Integer,Account> accounts) {
23         this.accNames = accNames;
24         this.accounts = accounts;
25     }
26     public void showNames(){
27         System.out.println(accNames);
28     }
29     public void showAccount(){
30         System.out.println(accounts);
31     }
32 }

在配置文件中进行注入:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans  default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6     <!-- 准备三个accountBean -->
 7     <bean id="account1" class="com.boxuewa.dk.demo2.beans.Account">
 8         <constructor-arg name="account" value="卡卡西"/>
 9     </bean>
10     <bean id="account2" class="com.boxuewa.dk.demo2.beans.Account">
11         <constructor-arg name="account" value="佐助"/>
12     </bean>
13     <bean id="account3" class="com.boxuewa.dk.demo2.beans.Account">
14         <constructor-arg name="account" value="鸣人"/>
15     </bean>
16     <!-- 注册一个group -->
17     <bean id="group" class="com.boxuewa.dk.demo2.beans.Group">
18         <property name="accNames">
19             <!-- 使用set标签注入简单set集合属性 -->
20             <map>
21                 <entry key="9527" value="路飞"/>
22                 <entry key="9528" value="佐助"/>
23                 <entry key="9529"><value>鸣人 </value></entry>
24             </map>
25         </property>
26         <property name="accounts">
27             <!-- set标签注入引用类型属性 -->
28             <map>
29                 <entry key="110"> <ref bean="account1"/></entry>
30                 <entry key="111"> <ref bean="account2"/></entry>
31                 <entry key="112" value-ref="account3"/>
32             </map>
33         </property>
34     </bean>
35 </beans>

测试:

1     @Test
2     public void testMap(){
3         ApplicationContext ac =
4                 new ClassPathXmlApplicationContext("applicationContext.xml");
5         Group bean1 = ac.getBean("group1",Group.class);
6         bean1.showNames();
7         bean1.showAccount();
8     }

注意几个问题:

1、上面的注入方式同样适合构造方法的注入:

 1 <!-- 注册一个group -->
 2 <bean id="group" class="com.boxuewa.dk.demo2.beans.Group">
 3     <constructor-arg name="accNames">
 4         <!-- 使用set标签注入简单set集合属性 -->
 5         <map>
 6             <entry key="9527" value="路飞"/>
 7             <entry key="9528" value="佐助"/>
 8             <entry key="9529"><value>鸣人 </value></entry>
 9         </map>
10     </constructor-arg>
11     <constructor-arg name="accounts">
12         <!-- set标签注入引用类型属性 -->
13         <map>
14             <entry key="110"> <ref bean="account1"/></entry>
15             <entry key="111"> <ref bean="account2"/></entry>
16             <entry key="112" value-ref="account3"/>
17         </map>
18     </constructor-arg>
19 </bean>

2、同样我们可以在注入bean的同时实例化:

 1 <constructor-arg name="accounts">
 2     <!-- set标签注入引用类型属性 -->
 3     <map>
 4         <entry key="110"> <ref bean="account1"/></entry>
 5         <entry key="111"> <ref bean="account2"/></entry>
 6         <entry key="112" value-ref="account3"/>
 7         <entry key="112">
 8             <bean class="com.boxuewa.dk.demo2.beans.Account">
 9                 <constructor-arg name="account" value="翠花"/>
10             </bean>
11         </entry>
12     </map>
13 </constructor-arg>

3、map的key可以是任何类型,所以其实map的可以也可使用 key-ref引用其他类型,当然就需要你自己制定一个专门的类型作为key。 这种方式我们一般都会使用。

 

posted @ 2020-10-05 09:56  戴着假发的程序员0-1  阅读(294)  评论(0编辑  收藏  举报