理论:

IOC(Inversion of Control控制反转)

DI(依赖注入) Dependency Injection

 

它不是一种技术而是一种思想。当初IOC理论的提出就是为了解决对象之间的“解耦”。在传统的创建对象的方式中我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;而现在IOC是有专门一个容器来创建这些对象,即由IOC容器来控制对象的创建,并将依赖对象注入给调用者,这样就产生为了“依赖注入”的概念,也就是"DI"。

 

  1. Injection based on Annotation

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    <!-- 以上是全部Spring都一样的配置文件 、beans是根元素-->
    <bean id="stoneAxe" class="com.StoneAxe"></bean>
    <!-- 下面是根据注解方式进行注入 -->
    <context:annotation-config/>
    <bean id="chinese" class="com.Chinese"/>
    </beans>
    public class Chinese implements Person {
    @Autowired
    private Axe axe;
    }
  2. Injection base on set method
    <?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 以上是全部Spring都一样的配置文件 、beans是根元素-->
    <bean id="stoneAxe" class="com.StoneAxe"></bean> <!-- 下面是根据属性注入,也就是set方法注入 -->
    <bean id="chinese" class="com.Chinese">
    <property name="axe" ref="stoneAxe"/>
    </bean> </beans>
  3. 3. Injection base on constructor.

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 以上是全部Spring都一样的配置文件 、beans是根元素-->
<bean id="stoneAxe" class="com.StoneAxe"></bean> <!-- 下面是根据构造器注入 --> <bean id="chinese" class="com.Chinese">
<constructor-arg ref="stoneAxe"/>
</bean> </beans>

Spring IOC/DI- 3 different types的更多相关文章

  1. spring ioc DI 理解

    下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...

  2. Spring+IOC(DI)+AOP概念及优缺点

    Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...

  3. 黑马-Spring(IOC&DI) AOP

    IOC(控制翻转) 概念 把对象的创建.初始化.销毁等工作交给spring容器来做 案例 环境 步骤 1.  写一个HelloWorld类 2.  写一个配置文件   把hello类放到spring容 ...

  4. Spring系列(二):Spring IoC/DI的理解

    这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...

  5. spring Ioc/DI的理解

    学习spring的童鞋都知道,spring中有两个非常重要的点,Ioc(控制反转)与DI(依赖注入),对于初级玩家来说,这两个概念可能有点模棱两可的感觉,今天就谈下自己的一点理解,不足请多多指教!!! ...

  6. spring IOC DI AOP MVC 事务, mybatis 源码解读

    demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...

  7. 零基础学习java------37---------mybatis的高级映射(单表查询,多表(一对一,一对多)),逆向工程,Spring(IOC,DI,创建对象,AOP)

    一.  mybatis的高级映射 1  单表,字段不一致 resultType输出映射: 要求查询的字段名(数据库中表格的字段)和对应的java类型的属性名一致,数据可以完成封装映射 如果字段和jav ...

  8. Spring IOC/DI和AOP原理

    一 IOC/DI 1. 概念机原理 IOC: Inversion of Control(控制反转)是一种设计思想,就是容器控制应用程序所需要外部资源的创建和管理,然后将其反转给应用程序.对象及其依赖对 ...

  9. Spring IOC/DI和AOP原理(转发:https://www.cnblogs.com/techroad4ca/p/5770073.html)

    一 IOC/DI 1. 概念及原理 IOC: Inversion of Control(控制反转)是一种设计思想,就是容器控制应用程序所需要外部资源的创建和管理,然后将其反转给应用程序.对象及其依赖对 ...

  10. coding++:Spring IOC/DI 实现原理

    什么是 SpringIOC: spring ioc 指的是控制反转,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.交由Spring容器统一进行管理,从而实现松耦合. “控制反 ...

随机推荐

  1. C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0

    C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0

  2. Core Data 使用映射模型

    Core Data 使用映射模型 如果新版本的模型存在较复杂的更改,可以创建一个映射模型,通过该模型指定源模型如何映射到目标模型. 创建映射模型,新建File,  Core Data 选择Mappin ...

  3. Swift_UI_UILabel

    Swift_UI_UILabel import UIKit class ViewController: UIViewController { override func viewDidLoad() { ...

  4. centos 7 系统服务auditd kdump tuned irqbalance

    tuned是红帽推出的一个动态调优方案,用户可以在不同的时间段内采用不同的调优方案.由于以服务进程形式存在,就可以很方便的和crontab结合!Tuned 是监控并收集各个系统组件用量数据的守护进程, ...

  5. npm 安装远程包(github的)

    npm install git+ssh://git@github.com:xxx/xxx.git#master --save-dev npm install git+ssh://git@github. ...

  6. [已解决] git 重命名文件夹

    git mv oldfolder newfolder 原文地址:http://www.cnblogs.com/gifisan/p/5980608.html

  7. webpack react基础配置二 热加载

    用到 webpack-dev-server  先安装,注意 装到全局 还是本项目我也没注意  因为之前一直报错,有很小可能是安装到本地解决了,或者是我网络问题: 装到全局:$ npm install ...

  8. flag+文件操作

    flag标志位,标识位,在其他语言中可能叫开关,个人觉得当作开关更容易理解.下面我们来利用这个开关来控制文件操作的流程,从而优雅的修改配置文件. global log 127.0.0.1 local2 ...

  9. Flesch Reading Ease -POJ3371模拟

    Flesch Reading Ease Time Limit: 1000MS Memory Limit: 65536K Description Flesch Reading Ease, a reada ...

  10. error: could not read CFBundleIdentifier from Info.plist (null)解决方法之一

    出现这种错误的原因可能很多,以下是我遇到的一种情况: 项目移植到新的环境 编译报错:  error: could not read CFBundleIdentifier from Info.plist ...