现在的位置: 首页 > 综合 > 正文

spring Ioc 的学习

2017年11月09日 ⁄ 综合 ⁄ 共 831字 ⁄ 字号 评论关闭

IoC Inverse of Control 反转控制的概念,

就是将原本在程序中手动创建Service对象的控制权,交由Spring框架管理,简单说,就是创建Service对象控制权被反转到了Spring框架

为了使接口和实现类之间的解耦合

小例子的讲解

导入基本的架包后,可以在src下配置applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 配置哪个类对IHelloService实例化 -->
	<bean id="helloService" class="cn.spring.service.impl.HelloService"></bean>

</beans>

scheme可以在spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html中找到

测试代码

	public void test2() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		//配置文件中的id属性
		IHelloService service = (IHelloService) context.getBean("helloService");
		service.sayHello();
		
	}

抱歉!评论已关闭.