定义接口:
package
Bean.collections;


public
interface
Person
...
{
publicvoiduseAxe();
}
package Bean.collections;
定义实现类:
package
Bean.collections;

import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.HashSet;
import
java.util.List;
import
java.util.Map;
import
java.util.Properties;
import
java.util.Set;

import
Bean.collections.Person;


public
class
Chinese
implements
Person
...
{
privateListschools=newArrayList();
privateMapscores=newHashMap();
privatePropertieshealth=newProperties();
privateSetaxes=newHashSet();

publicSetgetAxes()...{
returnaxes;
}

publicvoidsetAxes(Setaxes)...{
this.axes=axes;
}

publicPropertiesgetHealth()...{
returnhealth;
}

publicvoidsetHealth(Propertieshealth)...{
this.health=health;
}

publicListgetSchools()...{
returnschools;
}

publicvoidsetSchools(Listschools)...{
this.schools=schools;
}

publicMapgetScores()...{
returnscores;
}

publicvoidsetScores(Mapscores)...{
this.scores=scores;
}

publicvoiduseAxe()...{
System.out.println(schools);
System.out.println(scores);
System.out.println(axes);
System.out.println(health);
}

}
package
Bean.collections;


public
class
SteelAxe
implements
Axe
...
{


publicStringchop()...{
return"这是一把铁斧子";

}

}


package
Bean.collections;


public
class
WoodAxe
implements
Axe
...
{


publicStringchop()...{
return"这是一把木头斧子";

}

}
<?
xmlversion="1.0"encoding="UTF-8"
?>
<!
DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"
>

<
beans
>

<
bean
id
="chinese"
class
="Bean.collections.Chinese"
>
<
property
name
="schools"
>
<
list
>
<
value
>
小学
</
value
>
<
value
>
中学
</
value
>
<
value
>
大学
</
value
>
</
list
>
</
property
>
<
property
name
="health"
>
<
props
>
<
prop
key
="血压"
>
正常
</
prop
>
<
prop
key
="身高"
>
178
</
prop
>
</
props
>
</
property
>
<
property
name
="scores"
>
<
map
>
<
entry
key
="数学"
>
<
value
>
88
</
value
>
</
entry
>
<
entry
key
="语文"
>
<
value
>
99
</
value
>
</
entry
>
</
map
>
</
property
>
<
property
name
="axes"
>
<
set
>
<
value
>
字符串斧子
</
value
>
<!--
用嵌套bean定义属性
-->
<
bean
class
="Bean.collections.WoodAxe"
/>
<!--
引用bean作为属性
-->
<
ref
local
="steelaxe"
/>
</
set
>
</
property
>
</
bean
>
<
bean
id
="steelaxe"
class
="Bean.collections.SteelAxe"
></
bean
>
</
beans
>
public
static
void
main(String[]args)
throws
Exception
...
{

Stringpath=newTest().getClass().getResource("/").getPath();
Stringrealpath=path.substring(1,path.length());
ApplicationContextcontext=newFileSystemXmlApplicationContext(realpath+"/collection.xml");


Personperson1=(Person)context.getBean("chinese");
person1.useAxe();


}
package
Bean.collections;

public
interface
Person
...
{
publicvoiduseAxe();
}package Bean.collections;
public interface Axe {
public String chop();
}
public String chop();
}
定义实现类:
package
Bean.collections;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.HashSet;
import
java.util.List;
import
java.util.Map;
import
java.util.Properties;
import
java.util.Set;
import
Bean.collections.Person;

public
class
Chinese
implements
Person
...
{
privateListschools=newArrayList();
privateMapscores=newHashMap();
privatePropertieshealth=newProperties();
privateSetaxes=newHashSet();
publicSetgetAxes()...{
returnaxes;
}
publicvoidsetAxes(Setaxes)...{
this.axes=axes;
}
publicPropertiesgetHealth()...{
returnhealth;
}
publicvoidsetHealth(Propertieshealth)...{
this.health=health;
}
publicListgetSchools()...{
returnschools;
}
publicvoidsetSchools(Listschools)...{
this.schools=schools;
}
publicMapgetScores()...{
returnscores;
}
publicvoidsetScores(Mapscores)...{
this.scores=scores;
}
publicvoiduseAxe()...{
System.out.println(schools);
System.out.println(scores);
System.out.println(axes);
System.out.println(health);
}
}
package
Bean.collections;

public
class
SteelAxe
implements
Axe
...
{

publicStringchop()...{
return"这是一把铁斧子";
}
}


package
Bean.collections;

public
class
WoodAxe
implements
Axe
...
{

publicStringchop()...{
return"这是一把木头斧子";
}
}
spring配置文件:
<?
xmlversion="1.0"encoding="UTF-8"
?>
<!
DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"
>

<
beans
>

<
bean
id
="chinese"
class
="Bean.collections.Chinese"
>
<
property
name
="schools"
>
<
list
>
<
value
>
小学
</
value
>
<
value
>
中学
</
value
>
<
value
>
大学
</
value
>
</
list
>
</
property
>
<
property
name
="health"
>
<
props
>
<
prop
key
="血压"
>
正常
</
prop
>
<
prop
key
="身高"
>
178
</
prop
>
</
props
>
</
property
>
<
property
name
="scores"
>
<
map
>
<
entry
key
="数学"
>
<
value
>
88
</
value
>
</
entry
>
<
entry
key
="语文"
>
<
value
>
99
</
value
>
</
entry
>
</
map
>
</
property
>
<
property
name
="axes"
>
<
set
>
<
value
>
字符串斧子
</
value
>
<!--
用嵌套bean定义属性
-->
<
bean
class
="Bean.collections.WoodAxe"
/>
<!--
引用bean作为属性
-->
<
ref
local
="steelaxe"
/>
</
set
>
</
property
>
</
bean
>
<
bean
id
="steelaxe"
class
="Bean.collections.SteelAxe"
></
bean
>
</
beans
>
测试代码:
public
static
void
main(String[]args)
throws
Exception
...
{
Stringpath=newTest().getClass().getResource("/").getPath();
Stringrealpath=path.substring(1,path.length());
ApplicationContextcontext=newFileSystemXmlApplicationContext(realpath+"/collection.xml");

Personperson1=(Person)context.getBean("chinese");
person1.useAxe();

}
运行结果“
[小学, 中学, 大学]
{数学=88, 语文=99}
[字符串斧子, Bean.collections.WoodAxe@1506dc4, Bean.collections.SteelAxe@1126b07]
{血压=正常, 身高=178}
本文通过具体示例展示了如何在Spring框架中进行集合类型的注入,包括List、Set、Map及Properties等不同类型的注入方式,并提供了完整的Java代码实现及Spring配置文件。
9123

被折叠的 条评论
为什么被折叠?



