Spring IOC/DI- 3 different types
理论:
IOC(Inversion of Control控制反转)
DI(依赖注入) (Dependency Injection)
它不是一种技术而是一种思想。当初IOC理论的提出就是为了解决对象之间的“解耦”。在传统的创建对象的方式中我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;而现在IOC是有专门一个容器来创建这些对象,即由IOC容器来控制对象的创建,并将依赖对象注入给调用者,这样就产生为了“依赖注入”的概念,也就是"DI"。
- 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;
} - 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. 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的更多相关文章
- spring ioc DI 理解
下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...
- Spring+IOC(DI)+AOP概念及优缺点
Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...
- 黑马-Spring(IOC&DI) AOP
IOC(控制翻转) 概念 把对象的创建.初始化.销毁等工作交给spring容器来做 案例 环境 步骤 1. 写一个HelloWorld类 2. 写一个配置文件 把hello类放到spring容 ...
- Spring系列(二):Spring IoC/DI的理解
这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...
- spring Ioc/DI的理解
学习spring的童鞋都知道,spring中有两个非常重要的点,Ioc(控制反转)与DI(依赖注入),对于初级玩家来说,这两个概念可能有点模棱两可的感觉,今天就谈下自己的一点理解,不足请多多指教!!! ...
- spring IOC DI AOP MVC 事务, mybatis 源码解读
demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...
- 零基础学习java------37---------mybatis的高级映射(单表查询,多表(一对一,一对多)),逆向工程,Spring(IOC,DI,创建对象,AOP)
一. mybatis的高级映射 1 单表,字段不一致 resultType输出映射: 要求查询的字段名(数据库中表格的字段)和对应的java类型的属性名一致,数据可以完成封装映射 如果字段和jav ...
- Spring IOC/DI和AOP原理
一 IOC/DI 1. 概念机原理 IOC: Inversion of Control(控制反转)是一种设计思想,就是容器控制应用程序所需要外部资源的创建和管理,然后将其反转给应用程序.对象及其依赖对 ...
- Spring IOC/DI和AOP原理(转发:https://www.cnblogs.com/techroad4ca/p/5770073.html)
一 IOC/DI 1. 概念及原理 IOC: Inversion of Control(控制反转)是一种设计思想,就是容器控制应用程序所需要外部资源的创建和管理,然后将其反转给应用程序.对象及其依赖对 ...
- coding++:Spring IOC/DI 实现原理
什么是 SpringIOC: spring ioc 指的是控制反转,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.交由Spring容器统一进行管理,从而实现松耦合. “控制反 ...
随机推荐
- (三)SQL入门 数据库规格化简介
什么是数据库的规格化呢,说白了就是为了去除数据库冗余.为了数据库更加容易管理而将大表按照逻辑划分为小表的过程. 什么是数据库的去规格化呢,就是规格化的反面.那么你可能就会问,既然上面说了数据库的规格化 ...
- 删除txt文件每行第一(n)个空格前内容的方法
1. 把要处理的文本保存在a.txt文件中 2. 在相同文件夹中新建一个xx.txt文件,输入下面代码,再把文件名改为xx.bat. @echo offset fn=a.txt(for /f &quo ...
- C++11特性(模板类 initializer_list)
[1]initializer_list模板类 C++primer 原文如下: 通读原文相关篇幅,分析解读内容如下: 提供initializer_list类的初衷,为了便于将有限个同一类型(或可转换为同 ...
- iTunes
我们的电脑都要下载比较好的显卡那项 https://support.apple.com/zh_CN/downloads/itunes
- 压力测试工具JMeter入门教程
1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...
- 学习Linux系列--Python资源收集
本系列文章记录了个人学习过程的点点滴滴. 回到目录 玩Linux,不懂一门脚本语言是不行的,我这里选择的是Python. 10.入门资源 下面前三遍是比较系统的文档,后面是一些心得,我看了好几天,终于 ...
- 利用反射+AOP,封装Basehandler
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点, ...
- Http状态码(转)
什么是Http状态码?(转自http://bbs.tui18.com/thread-11597640-1-1.html) 百度百科上解释为:HTTP状态码(HTTP Status Code)是用以表示 ...
- spring功能总结
- 山东省第七届ACM省赛------Triple Nim
Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...