博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring ioc 注入List/Map等
阅读量:7125 次
发布时间:2019-06-28

本文共 4723 字,大约阅读时间需要 15 分钟。

from: http://hi.baidu.com/winters1224/item/d5fe20bd873513afeaba9322



exactList是action里的一个List<String>类型的变量。
   <property name="exactList">
    <list>
     <value>01</value>
     <value>02</value>
     <value>03</value>
     <value>04</value>
     <value>05</value>
     <value>06</value>
     <value>07</value>
     <value>08</value>
     <value>09</value>
     <value>34</value>
    </list>
   </property>
再附一个别人的例子
form:
1.--User类
package org.stelcomtech;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class User {
private String username;
private int age;
private Home myHome;
private Home[] homes;
private List homesList;
private Set homesSet;
private Map homesMap;
public Home[] getHomes() {
return homes;
}
public void setHomes(Home[] homes) {
this.homes = homes;
}
public List getHomesList() {
return homesList;
}
public void setHomesList(List homesList) {
this.homesList = homesList;
}
public Map getHomesMap() {
return homesMap;
}
public void setHomesMap(Map homesMap) {
this.homesMap = homesMap;
}
public Set getHomesSet() {
return homesSet;
}
public void setHomesSet(Set homesSet) {
this.homesSet = homesSet;
}
public Home getMyHome() {
return myHome;
}
public void setMyHome(Home myHome) {
this.myHome = myHome;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2.---Home类
package org.stelcomtech;
public class Home {
private String homeAddr;
public String getHomeAddr() {
return homeAddr;
}
public void setHomeAddr(String homeAddr) {
this.homeAddr = homeAddr;
}
}
3.--测试类 MainTest
package org.stelcomtech;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MainTest {
public static void main(String[]args){
ApplicationContext ac=new FileSystemXmlApplicationContext("org/stelcomtech/applicationContext.xml");
User user=(User)ac.getBean("userid");
Home[] homes=user.getHomes();
System.out.println("************数组注入***************");
for(int i=0;i<homes.length;i++){
System.out.println("家庭地址:"+homes[i].getHomeAddr());
}
System.out.println("************List注入********************");
List list=user.getHomesList();
Iterator ir=list.iterator();
while(ir.hasNext()){
//Home home1=(Home)ir.next();
System.out.println("家庭地址:"+ir.next().toString());
}
System.out.println("***********Set注入******************");
Set set=user.getHomesSet();
Iterator ir1=set.iterator();
while(ir1.hasNext()){
System.out.println("家庭地址:"+ir1.next().toString());
}
System.out.println("************Map注入**************");
Map map=user.getHomesMap();
Home home11=(Home)map.get("1");
System.out.println("家庭地址:"+home11.getHomeAddr().toString());
Home home111=(Home)map.get("2");
System.out.println("家庭地址:"+home111.getHomeAddr().toString()); 
}
}
4.配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" "
xmlns:xsi=" "
xsi:schemaLocation="   ">
<bean id="myHome1" class="org.stelcomtech.Home" abstract="false"
lazy-init="default" dependency-check="default">
<property name="homeAddr">
<value>wuhan</value>
</property>
</bean>
<bean id="myHome2" class="org.stelcomtech.Home" abstract="false"
lazy-init="default" dependency-check="default">
<property name="homeAddr">
<value>shanghai</value>
</property>
</bean>
<!-- autowire 选择 byName时,按照Bean id 是否和类中的属性名来查找 -->
<!-- autowire 选择 byType时,按照 类的类型 来查找 -->
<bean id="userid" class="org.stelcomtech.User" abstract="false"
lazy-init="default" dependency-check="default">
<property name="username">
<value>haiyang</value>
</property>
<property name="age">
<value>22</value>
</property>
<!-- 数组注入方式 -->
<property name="homes">
<list>
<ref bean="myHome1" />
<ref bean="myHome2" />
</list>
</property>
<!-- List注入方式 -->
<property name="homesList">
<list>
<ref bean="myHome1" />
<ref bean="myHome2" />
<value>haha</value>
</list>
</property>
<!-- Set注入方式 -->
<property name="homesSet">
<set>
<value>hello</value>
<ref bean="myHome2" />
<ref bean="myHome1" />
</set>
</property>
<!-- Map注入方式 -->
<property name="homesMap">
<map>
<entry key="1">
<ref bean="myHome1" />
</entry>
<entry key="2">
<ref bean="myHome2" />
</entry>
</map>
</property>
</bean> 
</beans>
5.运行结果
log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
************数组注入***************
家庭地址:wuhan
家庭地址:shanghai
************List注入********************
家庭地址:
家庭地址:
家庭地址:haha
***********Set注入******************
家庭地址:hello
家庭地址:
家庭地址:
************Map注入**************
家庭地址:wuhan
家庭地址:shanghai

转载地址:http://lgeel.baihongyu.com/

你可能感兴趣的文章
RHCE 学习笔记(31) - 防火墙 (中)
查看>>
puppet自动化运维
查看>>
linux系统配置基础--此篇会不断的完善
查看>>
git命令记录
查看>>
jenkins 配置
查看>>
关于dubbo服务的xml配置文件报错的问题
查看>>
Linux 环境变量和alias设置
查看>>
Spring MVC +Hibernate集成
查看>>
搭建LAMP环境,并实践基于DNS做基于域名的虚拟主机
查看>>
neutron-metadata-proxy无响应,导致windos2003密码透传失败
查看>>
用Python实现用户登录接口
查看>>
毕业前1个月的想法
查看>>
例程详析动态链接库
查看>>
geopy使用详解
查看>>
Notebook Workflows: The Easiest Way to Implement Apache Spark Pipelines
查看>>
mysql必知必会表样例
查看>>
我的友情链接
查看>>
python logging 模块在windows报错ConfigParser.NoSectionError: No section
查看>>
windows下python3虚拟环境搭建
查看>>
error at ::0 formal unbound in pointcut
查看>>