在IOC中有一个DI的概念。

IOC是控制反转,DI是依赖注入。现在编写的类里面是没有其他的属性的。如果你学过像UML设计的话,


电视没有遥控器,按按钮也可以,但是紧密的那种,像人和四肢,人如果没有了四肢人就废了。部门和领导,部门没有领导,照样可以存在,这种属于松散的。但是人和脑袋,这种关系就是紧密的了。你不能离开它而单独的存在。所以说聚合里面也是有这两种的,你简单了解一下就行了。

依赖:一个对象里面需要用到另外一个对象。

public class A{//A依赖了b

private B b;//b也是另外一个单独的对象  属性b

}

这种依赖通常是指方法的参数。你需要在方法A里面写一个setB()把b传进来。

DI需要IOC的,你得在IOC的基础上。将对象的创建权,由Spring来管理。也就是说那个HelloService你不需要自己去创建了,Spring可以帮你创建。DI依赖注入在Spring创建对象的过程中,把对象依赖的属性注入到类中.

A这个类得在Spring中配置。你配置一下之后Spring就帮我把这个A创建好了。那么A依赖的这个b呢,它会不会一并创建并且把它的值也给它设置上呢?这个过程叫做依赖注入。它是里面的这些个值想被赋值也想给它一并带过来的话那么就需要使用依赖注入。


IOC是创建对象,把创建对象的控制权呢交给Spring了,DI是我们创建这个对象的时候这个对象依赖的其他属性Spring也会帮你注入进来。

package cn.itcast.spring3.demo1;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest1 {
@Test
//传统方式
public void demo1(){
//造成程序紧密耦合.
//应该采用工厂+配置文件+反射的机制
HelloService helloService = new HelloServiceImpl();
helloService.sayHello();
}
@Test
//Spring开发
public void demo2(){
//创建一个Spring的工厂类.
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//ApplicationContext就是Spring的工厂类
//不写applicationContextx.xml的全路径,默认会去WEB-INF下面找applicationContextx.xml
//但是现在applicationContextx.xml写好之后已经发布到WEB-INF的classes里面
HelloService helloService = (HelloService) applicationContext.getBean("helloService");
helloService.sayHello();
}
}
package cn.itcast.spring3.demo1;
/**
* 入门案例的实现类
* @author ZhongZhenhua
*
*/
public class HelloServiceImpl implements HelloService{
private String info; public void setInfo(String info) {
this.info = info;
} public void sayHello(){
System.out.println("Hello Spring...."+info);
}
}
package cn.itcast.spring3.demo1;
/**
* 入门案例:
* @author ZhongZhenhua
*
*/
public interface HelloService {
public void sayHello();
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 别去schema,schema是文件,本地的文件,你得引那个头 --> <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"> <!-- 把接口和实现类在这个配置文件中配置,有了实现类的全路径之后到时候才能用工厂反射 --> <!-- 通过一个<bean>标签来设置类的信息,通过id属性为类起个标识. -->
<!-- 接口,实现类,配置文件也都有了 -->
<!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
<!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
也是这么做的,只不过工厂由Spring提供好了
-->
<bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
<!-- 使用<property>标签注入属性
value指的是普通值
ref指的是对象
-->
<property name="info" value="传智播客"></property>
</bean>
</beans>

day38 03-Spring的IOC和DI的区别的更多相关文章

  1. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  2. Spring框架中的IOC和DI的区别

    上次面试被问到IOC和DI的区别时,没怎么在意,昨天又被问到,感觉有点可惜.今晚总算抽点时间,查看了spring官方文档.发现,IoC更像是一种思想,DI是一种行为.为了降低程序的耦合度,利用spri ...

  3. spring的IOC,DI及案例详解

    一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...

  4. 对Spring中IOC和DI的理解

    前几篇讲了Spring中IOC和DI的用法,本篇应该放到三篇之前,但一直没有想到好的讲解方式,后参考https://blog.csdn.net/luoyepiaoxue2014/article/det ...

  5. spring之IOC和DI实现

    Spring核心思想 : IOC控制反转 DI依赖注入 AOP切面编程 IOC思想: 场景描述: 在没有加入spring框架之前我们调取业务层时需要创建对象,例如:  接口名   变量名=new  接 ...

  6. IoC和DI的区别

    ------------------siwuxie095                                     IoC 和 DI 的区别         1.区别     (1)Io ...

  7. 总结一下 Spring的IOC、DI

    国庆节刚过,应一些朋友的提问,总结一下Spring中IOC也即DI的通俗理解. 网友wm5920解释: IOC控制反转:说的是创建对象实例的控制权从代码控制剥离到IOC容器控制,实际就是你在xml文件 ...

  8. 关于Spring的IOC和DI

    原始调用模型 Spring的演化过程 Spring的调用过程 ======================================= IoC[理解][应用][重点] 1.IoC(Inversi ...

  9. 转载百度百科上的强回复,关于spring的IOC和DI

    IoC与DI   首先想说说IoC(Inversion of Control,控制倒转).这是spring的核心,贯穿始终.所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命 ...

  10. Java 反射和内省实现spring的IOC和DI

    1.构造两个JavaBean package com.spring.model; public class People { private Car car; public Car getCar() ...

随机推荐

  1. ThreadLocal简析

    简介 ThreadLocal在Java多线程开发中常见的一个类,在面试中也经见的问题,比如ThreadLocal的作用是什么,ThreadLocal的实现原理是什么等等.ThreadLocal是jav ...

  2. Java-MyBatis-MyBatis3-XML映射文件:结果映射

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:结果映射 1.返回顶部 1. 结果映射 resultMap 元素是 MyBatis 中最重要最强大的元素.它可以让你从 90 ...

  3. iOS之UIBezierPath贝塞尔曲线属性简介

    #import <Foundation/Foundation.h> #import <CoreGraphics/CoreGraphics.h> #import <UIKi ...

  4. Ubuntu 安装gnome桌面及vnc远程连接

    安装gnome桌面 sudo apt-get install gnome-core 安装vnc sudo apt-get install vnc4server 启动vnc vncserver 设置一下 ...

  5. PAT甲级——A1105 Spiral Matrix【25】

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  6. Oracle的UTL_FILE.FOPEN学习笔记

    Oracle提供的文件操作包UTL_FILE包中的UTL_FILE.FOPEN负责打开一个文件. UTL_FILE.FOPEN(location in varchar2, filename in va ...

  7. Activiti 部分实用功能

    helloworld中已经写了关于部署流程图,查询个人任务,完成个人任务部分.现在添加几个新的实用功能 1.判断流程是否完成,代码如下 public void isProcessEnd() { Str ...

  8. STL与泛型编程-第一周笔记-Geekband

    1, 模板观念与函数模板 简单模板: template< typename T > T Function( T a, T b) {- } 类模板: template struct Obje ...

  9. OSG实现正八面体剖分成球

    #include<Windows.h> #include<osg/Node> #include<osg/Geode> #include<osg/Group&g ...

  10. MyEclipse设置 web访问根路径

    使用鼠标右键点击项目(点击属性properties)进入如下图: