The recommended way to find out about the actual runtime type of a particular bean is a BeanFactory.getType call for the specified bean name.
找出特定bean的实际运行时类型的推荐方法是使用BeanFactory.getType方法调用其bean名。
This takes all of the above cases into account and returns the type of object that a BeanFactory.getBean call is going to return for the same bean name.
这种方法考虑到了上述的各种情况并返回对象的类型,BeanFactory.getBean方法将返回相同的bean名。
1.4. Dependencies
依赖
A typical enterprise application does not consist of a single object (or bean in the Spring parlance).
一个典型的企业级应用不会由单个对象组成(或者以Sping的说法为bean)
Even the simplest application has a few objects that work together to present what the end-user sees as a coherent application.
即便是最简单的应用有一些都有一些共同工作的对象来呈现最终用户看到的整体应用。【拗口】
This next section explains how you go from defining a number of bean definitions that stand alone to a fully realized application where objects collaborate to achieve
a goal.
接下来的部分展示了如何从大量独立的bean定义到一个完全成型的应用,即对象们合作实现的目标。
1.4.1. Dependency Injection
1.4.1依赖注入
Dependency injection (DI) is a process whereby objects define their dependencies (that is, the other objects with which they work) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.
依赖注入是一个这样的过程,对象定义他们的依赖(也就是说,和他们共同作用的其他对象)仅通过构造器参数,工厂方法的参数,或者set在对象实例上的属性在它们被构造(实例化?)或者被工厂方法返回后。
The container then injects those dependencies when it creates the bean.
容器在创建这些bean是注入这些依赖。
This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes or the Service Locator pattern.
这个过程根本上反转了(因此得名,控制反转),Bean本身通过直接使用构造器或服务定位模式去控制实例化或其本身依赖的位置。
Code is cleaner with the DI principle, and decoupling is more effective when objects are provided with their dependencies.
使用DI原则的代码更加清爽,而且当向对象提供它们依赖时更加解耦得更加高效。
The object does not look up its dependencies and does not know the location or class of the dependencies.
对象不(需要)查找他的依赖以及不知道其依赖的位置和类
As a result, your classes become easier to test, particularly when the dependencies are on interfaces or abstract base classes, which allow for stub or mock implementations to be used in unit tests.
其结果为,你的类变得更加容易去测试,尤其是当依赖是接口或抽象类时,他们需要在在单元测试中使用存根或模拟实现类。
DI exists in two major variants: Constructor-based dependency injection and Setter-based dependency injection.
依赖注入存在两个主要的形式:基于构造器的依赖注入和基于Setter方法的依赖注入。
Constructor-based Dependency Injection
基于构造器的依赖注入
Constructor-based DI is accomplished by the container invoking a constructor with a number of arguments, each representing a dependency.
基于构造器DI由容器调用一个构造器完成,构造器有着许多参数,每一个参数代表一个依赖。