码迷,mamicode.com
首页 > 编程语言 > 详细

spring ioc 容器概念

时间:2016-02-08 13:34:49      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:

spring ioc 容器

一个java应用程序是有很多类组成的,这些类相互协作、相互作用来提供应用程序的表现行为。那些被其它类组合提供某些行为的类,称之为其它类的依赖(dependencies)。利用软件工程中的组合模式(经常是继承模式的反模式)来说,我们经常利用某些类组合成其它类,不管这些类是通过构造函数还是setter方法或其它方法,那么组合成其它类的那些类就是这个组合类的依赖。当组合类要表现出的行为依赖这些类的时候,这些类必须被创建并注入给组合类。
在spring应用中,spring ioc 容器就负责上述功能:创建类并注入它们的依赖。那些被spring容器创建并管理的类称只为bean。当spring管理了这些类的时候,我们就没必要实现一些设计模式来组合我们的应用,比如Factory, Service Locator等等。

A Java application consists of objects that interact with each other to provide application behavior. The objects with which an object interacts are referred to as its dependencies. For instance, if an object X interacts with objects Y and Z, then Y and Z are dependencies of object X. DI is a design pattern in which the dependencies of an object are typically specified as arguments to its constructor and setter methods. And, these dependencies are injected into the object when it’s created.
In a Spring application, Spring IoC container (also referred to as Spring container) is responsible for creating application objects and injecting their dependencies. The application objects that the Spring container creates and manages are referred as beans. As the Spring container is responsible for putting together application objects, you don’t need to implement design patterns, like Factory, Service Locator, and so on, to compose your application. DI is also referred to as Inversion of Control (IoC) because the responsibility of creating and
injecting dependencies is not with the application object but with the Spring container.


  对于一个应用,其中的类和类之间的依赖关系是通过某些配置元数据表现。
For a given application, information about application objects and their dependencies is specified using configuration metadata. Spring IoC container reads application’s configuration metadata to instantiate application objects and inject their dependencies. 


  spring容器读取了配置元数据之后,通过java反射创建类并注入其依赖类。
Spring container reads the configuration metadata (like the one shown in example listing 1-3)of an application and creates the application objects defined by <bean> elements and injects their dependencies. Spring container makes use of Java Reflection API
(http://docs.oracle.com/javase/tutorial/reflect/index.html) to create application objects and 
inject their dependencies. The following figure summarizes how the Spring container works:


技术分享

 spring容器提供配置元数据的方式有xml和java注解两种方式。

The configuration metadata can be supplied to the Spring container via XML (as shown in example listing 1-3), Java annotations (refer chapter 6) and also through the Java code (refer chapter 6).
As the Spring container is responsible for creating and managing application objects, enterprise services (like transaction management, security, remote access, and so on) can be transparently applied to the objects by the Spring container. The ability of the Spring container to enhance the application objects with additional functionality makes it possible for you to model your application objects as simple Java objects (also referred to as POJOs or Plain Old Java Objects). Java classes corresponding to POJOs are referred to as POJO classes, which are nothing but Java classes that don’t implement or extend frameworkspecific interfaces or classes. The enterprise services, like transaction management, security,remote access, and so on, required by these POJOs are transparently provided by the Spring container.

spring ioc 容器概念

标签:

原文地址:http://blog.csdn.net/doctor_who2004/article/details/50644513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!