读书人

流入依赖对象

发布时间: 2012-08-09 15:59:21 作者: rapoo

注入依赖对象
知识点:


基本类型对象注入:
<bean id="orderService" ref="orderDao"/>
</bean>
方式二(使用内部bean,但该bean不能被其他bean使用)
<bean id="orderService" 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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!--外部引用
<bean id="personDaoBean" >
<property name="personDao" ref="personDaoBean"/>
</bean>
-->
<!--使用内部bean,但该bean不能被其他bean使用
<bean id="personServer" >
<property name="personDao">
<bean ref="personDaoBean"/>
<constructor-arg index="1" value="liyong"/>
</bean>
</beans>

第五步:编写单元测试

public class JUnitTest {

@Test
public void TestSave()
{
//得到Spring容器实例
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//ApplicationContext ctx2 = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

//这里面向接口
IPersonServer server =(IPersonServer)ctx.getBean("personServer");
server.save();
}
}

第六步:部署

.....

读书人网 >Web前端

热点推荐