集合对象注入&&通过构造函数注入
2009-09-22 00:00:00 来源:WEB开发网测试类:
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml");
// ItcastClassPathXMLApplicationContext ctx=new
// ItcastClassPathXMLApplicationContext("beans.xml");
//
IPersonService ipersonService = (IPersonService) ctx
.getBean("personService");
//集合对象的遍历
System.out.println("===========set==================");
for (String value : ipersonService.getSets()) {
System.out.println(value);
}
// ipersonService.Save();
// ctx.close();
// ctx.registerShutdownHook();
System.out.println("===========List=================");
for(String value:ipersonService.getLists()){
System.out.println(value);
}
System.out.println("=========properties===============");
for(Object value:ipersonService.getProperties().keySet()){
System.out.println(value);
}
System.out.println("================maps==================");
for(Object value:ipersonService.getMaps().keySet()){
System.out.println(value);
}
//调用PersonServiceBean的sava方法,输出结果
ipersonService.Save();
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="personService"
class="cn.itcast.service.impl.PersonServiceBean">
<property name="IPersonDao" ref="personDaoBean"></property>
<constructor-arg index="0" ref="personDaoBean"
type="cn.itcast.dao.IPersonDao" />
<constructor-arg index="1" type="java.lang.String"
value="传智博客">
</constructor-arg>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="properties">
<props>
<prop key="properties1">property1</prop>
<prop key="properties2">property2</prop>
<prop key="properties3">property3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key1" value="keyFirst"></entry>
<entry key="key2" value="keySecond"></entry>
<entry key="key3" value="keyThird"></entry>
</map>
</property>
</bean>
<bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<!--
<bean id="anotherPersonServiceBean"
class="cn.itcast.service.impl.AnotherPersonServiceBean" >
</bean>
-->
</beans>
public class PersonDaoBean implements IPersonDao {
public void add(){
System.out.println("这是personDaoBean的Add()方法");
}
}
输出:
===========set==================
set1
set2
set3
===========List=================
list1
list2
list3
=========properties===============
properties3
properties2
properties1
================maps==================
key1
key2
key3
传智博客
这是personDaoBean的Add()方法
更多精彩
赞助商链接