IOC:inverse of Control: 控制反转。

  意思是程序中的之间的关系,不用代码控制,而完全是由容器来控制。在运行阶段,容器会根据配置信息直接把他们的关系注入到组件中。同样,这也是 依赖注入的含义。依赖注入和控制反转其实是一个概念。只不过强调的不同而已,依赖注入强调关系的注入是由容器在运行时完成,而控制反转强调关系是由容器控制。其实本质是一样的。

贴一段代码

    /**
* 学校类
*/
public class School {
private String name;
public School(String name) {
this.name=name;
}
public void printInfo() {
System.out.println("该学校的名称是:"+name);
}
}
/**
* 学生类
*/
public class Student {
public int id;
public String name;
private School school;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
}
    <?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="school" class="ioc.iocsample.School">
<constructor-arg index="0">
<value>廊坊师院</value>
</constructor-arg>
</bean>
<bean id="student" class="ioc.iocsample.Student">
<property name="id" value="001"/>
<property name="name" value="张三"/>
<property name="school" ref ="school"/>
</bean>
</beans>
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student=(Student)factory.getBean("student");
student.getSchool().printInfo();
}
}

spring中的依赖注入DI(dependence injection)共有三种方式:第一种是接口注入(Interface Injection)第二种是get set注入(set/get Injection)第三种是构造器注入(Constructor Injection)

三种注入方式的区别:

1.接口注入:组件需要依赖特定接口的实现,其中的加载接口实现和接口实现的具体对象都是由容器来完成。这样,接口必须依赖容器,这样的组件具有侵入性, 降低了重用性。其中如J2EE开发中常用的Context.lookup(ServletContext.getXXX),都是接口注入的表现形式。(这 种注入方式不是常用的)

2.getter/setter方式注入:对于需要注入的东西比较明确。符合java的设计规则。更适合java开发人员,使用起来更加自然,更加方便。

3.构造器方式注入:在类加载的时候,就已经注入依赖的组件。但是若是参数多的话,使用起来不方便。

但是后两种注入方式是spring常用的,而第一种接口注入方式不常用。

最新文章

  1. MFC&amp;Halcon之图片显示
  2. CSS3基础03(3D②) 求粉丝
  3. python之简单POST模拟登录
  4. 2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟
  5. grails中报Cannot create a session after the response has been committed异常的解决办法
  6. 如何实现数字lcd显示效果(原创)
  7. 高放的c++学习笔记之重载运算与类型转换
  8. php 7.3 新特性
  9. 合并两个 Lambda 表达式
  10. python虚拟环境搭建
  11. postgresql 函数获取多个字段的数字大小值
  12. OC屏幕手势解锁
  13. ssh Socket error Event: 32 Error: 10053.
  14. bzoj 2406: 矩阵 ——solution
  15. abp 修改abp.zero的实体映射类,使生成的表和字段为大写状态
  16. 【算法】Huffman编码(数据结构+算法)
  17. GIT(7)----强制用远程代码覆盖本地修改
  18. jQuery ajax 添加头部参数跨域
  19. [UWP]爱恋动漫BT开发小记
  20. Windows网络通信(一):socket同步编程

热门文章

  1. 小峰mybatis(5)mybatis使用注解配置sql映射器--动态sql
  2. 杂项-TOOL:NPIO
  3. 廖雪峰Java1-2程序基础-7布尔运算符
  4. js原生态函数中使用jQuery中的 $(this)无效的解决方法
  5. mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级-&gt;将使用Mysql字符集复选框去掉,下拉框选择GBK-&gt;导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
  6. select count(*) as total from(select count(*) from tab_cb_casim group by `card_no`) as cai;
  7. 信息学奥赛(NOIP)初赛学习方法推荐
  8. 网络基础知识 tcp
  9. 并发工具类(一)等待多线程的CountDownLatch
  10. Python之函数——基础篇