[ java ] Spring 构造注入!
来源:互联网 发布:二阶滤波算法 编辑:程序博客网 时间:2024/06/04 20:25
3.3.1. Injecting dependencies
The basic principle behind Dependency Injection (DI) is that objects define their dependencies (that is to say the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method. Then, it is the job of the container to actuallyinject those dependencies when it creates the bean. This is fundamentally the inverse, hence the nameInversion of Control (IoC), of the bean itself being in control of instantiating or locating its dependencies on its own using direct construction of classes, or something like theService Locator pattern.
It becomes evident upon usage that code gets much cleaner when the DI principle is applied, and reaching a higher grade of decoupling is much easier when objects do not look up their dependencies, but are provided with them (and additionally do not even know where the dependencies are located and of what concrete class they are). DI exists in two major variants, namelyConstructor Injection and Setter Injection.
3.3.1.1. Constructor Injection
Constructor-based DI is effected by invoking a constructor with a number of arguments, each representing a dependency. Additionally, calling astatic
factory method with specific arguments to construct the bean, can be considered almost equivalent, and the rest of this text will consider arguments to a constructor and arguments to astatic
factory method similarly. Find below an example of a class that could only be dependency injected using constructor injection. Notice that there is nothingspecial about this class.
public class SimpleMovieLister { // theSimpleMovieLister
has a dependency on aMovieFinder
private MovieFinder movieFinder; // a constructor so that the Spring container can 'inject' aMovieFinder
public SimpleMovieLister(MovieFinder movieFinder) { this.movieFinder = movieFinder; } // business logic that actually 'uses' the injectedMovieFinder
is omitted...}
3.3.1.1.1. Constructor Argument Resolution
Constructor argument resolution matching occurs using the argument's type. If there is no potential for ambiguity in the constructor arguments of a bean definition, then the order in which the constructor arguments are defined in a bean definition is the order in which those arguments will be supplied to the appropriate constructor when it is being instantiated. Consider the following class:
package x.y;public class Foo { public Foo(Bar bar, Baz baz) { // ... }}
There is no potential for ambiguity here (assuming of course that Bar
and Baz
classes are not related in an inheritance hierarchy). Thus the following configuration will work just fine, and you do not need to specify the constructor argument indexes and / or types explicitly.
<beans> <bean name="foo" class="x.y.Foo"> <constructor-arg> <bean class="x.y.Bar"/> </constructor-arg> <constructor-arg> <bean class="x.y.Baz"/> </constructor-arg> </bean></beans>
When another bean is referenced, the type is known, and matching can occur (as was the case with the preceding example). When a simple type is used, such as<value>true<value>
, Spring cannot determine the type of the value, and so cannot match by type without help. Consider the following class:
package examples;public class ExampleBean { // No. of years to the calculate the Ultimate Answer private int years; // The Answer to Life, the Universe, and Everything private String ultimateAnswer; public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; }}
3.3.1.1.1.1. Constructor Argument Type Matching
The above scenario can use type matching with simple types by explicitly specifying the type of the constructor argument using the'type'
attribute. For example:
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/></bean>
3.3.1.1.1.2. Constructor Argument Index
Constructor arguments can have their index specified explicitly by use of the index
attribute. For example:
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/> <constructor-arg index="1" value="42"/></bean>
As well as solving the ambiguity problem of multiple simple values, specifying an index also solves the problem of ambiguity where a constructor may have two arguments of the same type. Note that theindex is 0 based.
- [ java ] Spring 构造注入!
- spring注入:构造注入
- spring构造注入
- Spring 构造函数注入
- Spring构造注入
- spring构造器注入
- spring 构造注入
- spring构造方法注入
- spring 构造函数注入
- spring 构造器注入
- spring 构造函数注入
- Spring构造器注入
- Spring构造器注入
- Spring构造注入
- spring通过构造注入
- Spring注入:设值注入、构造注入
- Spring设置注入和构造注入
- Spring设置注入和构造注入
- VC++中关于字节对齐的问题
- CSS positon absolute
- uva 10014 - Simple calculations
- HTML5 FileReader详解与实例---读取并显示图像文件
- MySql的批量添加实现
- [ java ] Spring 构造注入!
- 深入理解ThreadLocal
- 职业化所包含的行为模式
- unixODBC , freetds , tsql
- Winform工程反编译后的工作
- Android应用开发学习笔记之Fragment
- 公司自动化测试从无到有蛋碎的过程。。
- Android x86 设置竖屏
- 汉字转换成拼音