ioc与di(ioc与di).doc

  1. 1、本文档共20页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
ioc与di(ioc与di)

ioc与di(ioc与di) ioc与di 首先想说说ioc (inversion of control 控制倒转).这是spring的核心, 贯穿始终.所谓ioc, 对于spring框架来说, 就是由spring来负责控制对象的生命周期和对象间的关系.这是什么意思呢, 举个简单的例子, 我们是如何找女朋友的? 常见的情况是, 我们到处去看哪里有长得漂亮身材又好的mm, 然后打听她们的兴趣爱好、qq号、电话号、ip号、iq号........, 想办法认识她们, 投其所好送其所要, 然后嘿嘿...... 这个过程是复杂深奥的, 我们必须自己设计和面对每个环节.传统的程序开发也是如此, 在一个对象中, 如果要使用另外的对象, 就必须得到它 (自己new一个, 或者从jndi 中查询一个), 使用完之后还要将对象销毁 (比如connection等), 对象始终会和其他的接口或类藕合起来. 那么ioc是如何做的呢? 有点像通过婚介找女朋友, 在我和女朋友之间引入了一个第三者: 婚姻介绍所.婚介管理了很多男男女女的资料, 我可以向婚介提出一个列表, 告诉它我想找个什么样的女朋友, 比如长得像李嘉欣, 身材像林熙雷, 唱歌像周杰伦, 速度像卡洛斯, 技术像齐达内之类的, 然后婚介就会按照我们的要求, 提供一个mm, 我们只需要去和她谈恋爱、结婚就行了.简单明了. If the marriage broker does not meet our requirements, we will throw an exception. The whole process is no longer controlled by me, but controlled by a container like mechanism such as matchmaking. Springs development is so, all classes will be registered in the spring container, tell spring what you are, what you need, then spring will be running in the system at proper time, to take the initiative to give you what you want, but also give you to the other things you need. The creation and destruction of all classes are controlled by spring, that is to say, the object life cycle is no longer the object that references it, but spring. For a specific object, it used to control other objects, and now all objects are controlled by spring, so this is called control inversion. If you still dont understand, I decide to give up. One important aspect of IoC is to dynamically provide an object with other objects that it needs when the system is running. This is achieved through DI (Dependency, Injection, dependency injection). For example, A objects need to operate the database, we always A to write your own code to get a Connection object with spring, we just need to tell spring, Connection needs a A, as for the Connection structure, when A does not need to know the structure. When the system is running, spring will make a Connection at the right time and inject it into the A like an injection, thus c

文档评论(0)

jgx3536 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:6111134150000003

1亿VIP精品文档

相关文档