Spring Inner bean,List,map,set的xml配置
java程序(接口)
package com.java.test;
/**
* 定义输出输出接口
*/
public interface ICollectionTest {
/**
* 输出
*/
public void printCollection();
}
java程序(实现类)
package com.java.test.property;
public class CollectionTest02 {
public String printCollection(){
return "this is a inner bean";
}
}
XML配置信息(bean.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="collectionTest" class="com.java.test.property.CollectionTest">
<!-- inner bean name:对象名称 bean下定义只有Class属性,没有name属性-->
<property name="collectionTest02">
<bean class="com.java.test.property.CollectionTest02"></bean>
</property>
<!-- list-->
<property name="demoList">
<list>
<value>list1</value>
<value>list2</value>
<value>list2</value>
</list>
</property>
<!-- map -->
<property name="maps">
<map>
<entry key="entryKey1">
<value>entryValue1</value>
</entry>
<entry key="entryKey2">
<value>entryValue2</value>
</entry>
<entry key="entryKey3">
<value>entryValue3</value>
</entry>
</map>
</property>
<!-- set -->
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
</bean>
</beans>
测试类
import com.java.test.ICollectionTest;
import com.java.test.property.CollectionTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
ICollectionTest ICollectionTest = context.getBean("collectionTest", CollectionTest.class);
ICollectionTest.printCollection();
}
}
测试结果
this is a inner bean
list1
list2
list2
entryKey1:entryValue1
entryKey2:entryValue2
entryKey3:entryValue3
set1
set2
set3