摘要:关于集合对象的注入(Array,List,Map)
●
SomeBean.java
import java.util.List;
import java.util.Map;
public class SomeBean {
private String[] someStrArray;
private Some[] someObjArray;
private List someList;
private Map someMap;
public List getSomeList() {
return someList;
}
public void setSomeList(List someList) {
this.someList = someList;
}
public Map getSomeMap() {
return someMap;
}
public void setSomeMap(Map someMap) {
this.someMap = someMap;
}
public Some[] getSomeObjArray() {
return someObjArray;
}
public void setSomeObjArray(Some[] someObjArray) {
this.someObjArray = someObjArray;
}
public String[] getSomeStrArray() {
return someStrArray;
}
public void setSomeStrArray(String[] someStrArray) {
this.someStrArray = someStrArray;
}
}
●
Some.java
package com.kela.spring.ioc;
public class Some {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
●
Beans-config_3.xml
<?xml
version=
"1.0"
encoding=
"GB2312"
?>
<!DOCTYPE
beans
PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>
<beans>
<bean
id=
"some_1"
class=
"com.kela.spring.ioc.Some"
>
<property
name=
"name"
>
<value>
KangFeng
</value>
</property>
</bean>
<bean
id=
"some_2"
class=
"com.kela.spring.ioc.Some"
>
<property
name=
"name"
>
<value>
Kela
</value>
</property>
</bean>
<bean
id=
"someBean"
class=
"com.kela.spring.ioc.SomeBean"
>
<!--
数组
-->
<property
name=
"someStrArray"
>
<list>
<value>
Hello
</value>
<value>
你好
</value>
</list>
</property>
<!--
对象数组
-->
<property
name=
"someObjArray"
>
<list>
<ref
bean=
"some_1"
></ref>
<ref
bean=
"some_2"
></ref>
</list>
</property>
<!--
List
-->
<property
name=
"someList"
>
<list>
<value>
ListTest
</value>
<ref
bean=
"some_1"
></ref>
<ref
bean=
"some_2"
></ref>
</list>
</property>
<!--
Map
-->
<property
name=
"someMap"
>
<map>
<entry
key=
"MapTest"
>
<value>
你好,
KangFeng
</value>
</entry>
<entry
key=
"someKey_1"
>
<ref
bean=
"some_1"
></ref>
</entry>
</map>
</property>
</bean>
</beans>
●
TestClass.java
package com.kela.spring.ioc;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestClass {
public void method_3() {
try {
ApplicationContext context = new FileSystemXmlApplicationContext("bin\\com\\kela\\spring\\ioc\\beans-config_3.xml");
SomeBean someBean = (SomeBean)context.getBean("someBean");
//
取得StringArray注入的内容
System.out.println("
打印StringArray的内容~~~");
for(int i = 0; i < someBean.getSomeStrArray().length; i ++) {
System.out.println(someBean.getSomeStrArray()[i]);
}
//
取得SomeObject注入的内容
System.out.println("
打印SomeObject的内容~~~");
Some[] some = (Some[])someBean.getSomeObjArray();
for(int i = 0; i < some.length; i++) {
System.out.println(some[i].getName());
}
//
取得List注入的内容
System.out.println("
打印List的内容~~~");
List someList = (List)someBean.getSomeList();
for(int i = 0; i < someList.size(); i++) {
System.out.println(someList.get(i));
}
//
取得Map注入的内容
System.out.println("
打印Map的内容~~~");
Map someMap = (Map)someBean.getSomeMap();
System.out.println(someMap.get("MapTest"));
System.out.println(someMap.get("someKey_1"));
} catch (Exception e) {
System.out.println("[ERROR]" + e.getMessage());
}
}
public static void main(String[] args) {
TestClass testClass = new TestClass();
testClass.method_3();
}
}