7.4.1 Dependency resolution process

The container performs bean dependency resolution as follows:

如下是容器执行bean依赖解析的过程:

  • The ApplicationContext is created and initialized with configuration metadata that describes all the beans. Configuration metadata can be specified via XML, Java code, or annotations.
  •  ApplicationContext 被描述所有beans的配置元数据创建和初始化.配置元数据可以通过XML,Java代码或注解指定.
  • For each bean, its dependencies are expressed in the form of properties, constructor arguments, or arguments to the static-factory method if you are using that instead of a normal constructor. These dependencies are provided to the bean, when the bean is actually created.
  • 每一个bean的依赖被表达成属性,构造器参数或静态工厂方法参数的形式.如果你使用这些而不是通常的构造器.当这个bean被实际创建的时候,这些依赖被提供给这个bean.
  • Each property or constructor argument is an actual definition of the value to set, or a reference to another bean in the container.
  • 在容器中,每个属性或构造器参数都是一个设置的值或另一个bean的引用的实际定义.
  • Each property or constructor argument which is a value is converted from its specified format to the actual type of that property or constructor argument. By default Spring can convert a value supplied in string format to all built-in types, such as intlongStringboolean, etc.
  • 每个属性或构造器参数都从一个指定的格式的值转换成它们的实际类型.Spring默认能够将一个被提供的字符串格式的值转换为任意的内置类型,例如 intlongStringboolean等.

The Spring container validates the configuration of each bean as the container is created. However, the bean properties themselves are not set until the bean is actually created. Beans that are singleton-scoped and set to be pre-instantiated (the default) are created when the container is created. Scopes are defined in Section 7.5, “Bean scopes”. Otherwise, the bean is created only when it is requested. Creation of a bean potentially causes a graph of beans to be created, as the bean’s dependencies and its dependencies' dependencies (and so on) are created and assigned. Note that resolution mismatches among those dependencies may show up late, i.e. on first creation of the affected bean.

Spring容器被创建时会验证每个bean的配置.然而,这个bean被实际创建前都不会设置这个bean的属性.当这个容器被创建的时候,被创建出来的beans都是单例域和设置成预先实例化(默认).域被定义在Section 7.5, “Bean scopes”.否则,当这个bean被请求时才会创建它.当bean的依赖和它的依赖的依赖(等)被创建和分配的时,一个bean的创建可能会导致一堆beans被创建.注意与这些依赖解析不匹配时可能会延迟,也就是,首先创建被影响的bean.

You can generally trust Spring to do the right thing. It detects configuration problems, such as references to non-existent beans and circular dependencies, at container load-time. Spring sets properties and resolves dependencies as late as possible, when the bean is actually created. This means that a Spring container which has loaded correctly can later generate an exception when you request an object if there is a problem creating that object or one of its dependencies. For example, the bean throws an exception as a result of a missing or invalid property. This potentially delayed visibility of some configuration issues is why ApplicationContext implementations by default pre-instantiate singleton beans. At the cost of some upfront time and memory to create these beans before they are actually needed, you discover configuration issues when the ApplicationContext is created, not later. You can still override this default behavior so that singleton beans will lazy-initialize, rather than be pre-instantiated.

你通常可以相信Spring会去做正确的事情.在容器加载的时候,它检测配置问题,例如引用了不存在的beans和循环依赖.当这个bean被实际创建的时候,Spring设置属性和解析依赖越晚越好.这也意味着当你请求一个对象如果那个对象或它的依赖出现问题时,Spring容器可以在正确的加载后生成一个异常.例如,这个bean在缺失属性或属性无效时会抛出一个异常.这是个潜在的延迟在一些问题出现时,这也就是为什么 ApplicationContext 默认实现预先实例化单例bean.以花费一些预付的时间和内存在你实际需要前去创建这些beans,当 ApplicationContext 被创建时你可以发现配置问题,而不是之后.你依然可以重写这个默认的行为以致于单例beans将会延迟实例化而不是预先实例化.

If no circular dependencies exist, when one or more collaborating beans are being injected into a dependent bean, each collaborating bean is totally configured prior to being injected into the dependent bean. This means that if bean A has a dependency on bean B, the Spring IoC container completely configures bean B prior to invoking the setter method on bean A. In other words, the bean is instantiated (if not a pre-instantiated singleton), its dependencies are set, and the relevant lifecycle methods (such as a configured init method or the InitializingBean callback method) are invoked.

如果不存在循环引用,当一个或多个合作beans被注入到依靠的bean,每个合作bean在被注入到依靠的bean之前被完全配置完毕.这也意味着如果bean A有一个依赖bean B,Spring在调用bean A的setter方法前就完成了bean B的配置.换句话说,这个bean被实例化(如果不是预先实例化单例),它的依赖被设置,相关的生命周期方法(例如一个被配置的初始化方法或实例化bean的回调方法)被调用.

阅读更多

没有更多推荐了,返回首页