如何在Spring中注入Java集合类
展开全部
//普通java bean
public class NotificationBean {
public Map hobby;
public Map getHobby() {
return hobby;
}
public void setHobby(Map hobby) {
this.hobby = hobby;
}
}
<!--配置文件application-spring.xml-->
<bean id="notificationBean" class="com.straw.spring.beans.NotificationBean">
<property name="hobby">
<map>
<entry key="a" value="a"/>
<entry key="b" value="b"/>
</map>
</property>
</bean>
//java 测试类
public class MainTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ap = new ClassPathXmlApplicationContext("application-spring.xml");
NotificationBean notificationBean = (NotificationBean) ap.getBean("notificationBean");
Map hobby = notificationBean.getHobby();
Set<String> set = hobby.keySet();
for (String s:set){
System.out.println(s+":"+hobby.get(s));
}
}
}
//运行结果
a:a
b:b
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询