[学习笔记]Spring依赖注入

依赖:

典型的企业应用程序不可能由单个对象(在spring中,也可称之bean)组成,再简单的应用也是由几个对象相互配合工作的,这一章主要介绍bean的定义以及bean之间的相互协作。

依赖注入:

spring中的依赖注入(Dependency injection (DI))主要有两种形式:构造器注入和setter方法注入。

构造器注入:

基于构造函数的方式有其自己的优势,它可以明白地创建出带有特定构造參数的对象。另外它对于创建一些在类中不须要常常变化的域有明显的优势。假设用setter方法来做这样的事情会显得非常不协调。但通常可以採用init的方法在创建时就将其初始化。

当然对于某些类可能有非常多的域,构造函数不可能包括全部的情况,并且当中可以包括的构造參数也是有限的,此时Setter方法注入就可以以发挥其余地。

下面演示样例是一个仅仅能通过构造器注入的类:

[java] view plain copy
  1. public class SimpleMovieLister {  
  2.   
  3.   // the SimpleMovieLister has a dependency on a MovieFinder  
  4.   private MovieFinder movieFinder;  
  5.   
  6.   // a constructor so that the Spring container can 'inject' a MovieFinder  
  7.   public SimpleMovieLister(MovieFinder movieFinder) {  
  8.       this.movieFinder = movieFinder;  
  9.   }  
  10.   
  11.   // business logic that actually 'uses' the injected MovieFinder is omitted...  
  12. }  

构造函数參数匹配时根据的是构造器參数类型,为了不产生歧义,一般构造參数给出的顺序依照构造函数中參数给定的顺序。例如以下:

[java] view plain copy
  1. package x.y;  
  2.   
  3. public class Foo {  
  4.   
  5.   public Foo(Bar bar, Baz baz) {  
  6.       // ...  
  7.   }  
  8. }  

没有潜在的歧义存在,如果Bar和Baz两个类不想关

[html] view plain copy
  1. <span style="color:#333333;"><beans>  
  2.   <bean id="foo" class="x.y.Foo">  
  3.       <</span><span style="color:#ff0000;">constructor-arg</span><span style="color:#333333;"> ref="bar"/>  
  4.       <constructor-arg ref="baz"/>  
  5.   </bean>  
  6.   
  7.   <bean id="bar" class="x.y.Bar"/>  
  8.   <bean id="baz" class="x.y.Baz"/>  
  9.   
  10. </beans></span>  

当还有一个bean被引用时,假设类型一直,匹配就能够发生。当一个简单类型使用时,比如<value>true<value>,Spring不能决定value的类型。就不能进行匹配。

看以下一个演示样例:

[java] view plain copy
  1. package examples;  
  2.   
  3. public class ExampleBean {  
  4.   
  5.   // No. of years to the calculate the Ultimate Answer  
  6.   private int years;  
  7.   
  8.   // The Answer to Life, the Universe, and Everything  
  9.   private String ultimateAnswer;  
  10.   
  11.   public ExampleBean(int years, String ultimateAnswer) {  
  12.       this.years = years;  
  13.       this.ultimateAnswer = ultimateAnswer;  
  14.   }  
  15. }  

在前面这个演示样例中,使用type属性。容器能够进行简单的类型匹配:


[html] view plain copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2. <constructor-arg type="int" value="7500000"/>  
  3. <constructor-arg type="java.lang.String" value="42"/>  
  4. </bean>  

相同。我们也能够使用index属性来指定參数顺序(注意index从0開始):

[html] view plain copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2. <constructor-arg index="0" value="7500000"/>  
  3. <constructor-arg index="1" value="42"/>  
  4. </bean>  

在spring3.0中,我们也能够使用构造器參数名字来制定相应的參数值:

[html] view plain copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2. <constructor-arg name="years" value="7500000"/>  
  3. <constructor-arg name="ultimateanswer" value="42"/>  
  4. </bean>  

注意:Keep in mind that to make this work out of the box your code must be compiled with the debug flag enabled so that Spring can look up the parameter name from the constructor. If you can't compile your code with debug flag (or don't want to) you can use @ConstructorProperties JDK annotation to explicitly name your constructor arguments. The sample class would then have to look as follows:

[java] view plain copy
  1. package examples;  
  2.   
  3. public class ExampleBean {  
  4.   
  5.   // Fields omitted  
  6.   
  7.   @ConstructorProperties({"years""ultimateAnswer"})  
  8.   public ExampleBean(int years, String ultimateAnswer) {  
  9.       this.years = years;  
  10.       this.ultimateAnswer = ultimateAnswer;  
  11.   }  
  12. }  

setter方法注入:

下面是一个简单的仅仅能用setter方法进行注入的样例:

[java] view plain copy
  1. public class SimpleMovieLister {  
  2.   
  3.   // the SimpleMovieLister has a dependency on the MovieFinder  
  4.   private MovieFinder movieFinder;  
  5.   
  6.   // a setter method so that the Spring container can 'inject' a MovieFinder  
  7.   public void setMovieFinder(MovieFinder movieFinder) {  
  8.       this.movieFinder = movieFinder;  
  9.   }  
  10.   
  11.   // business logic that actually 'uses' the injected MovieFinder is omitted...  
  12. }  

ApplicationContext 支持setter注入和构造器注入,也支持在已存在构造器注入的情况下继续进行setter注入。


依赖注入实例:

下面样例为setter注入的xml配置文件:

[html] view plain copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2.   
  3. <!-- setter injection using the nested <ref/> element -->  
  4. <property name="beanOne"><ref bean="anotherExampleBean"/></property>  
  5.   
  6. <!-- setter injection using the neater 'ref' attribute -->  
  7. <property name="beanTwo" ref="yetAnotherBean"/>  
  8. <property name="integerProperty" value="1"/>  
  9. </bean>  
  10.   
  11. <bean id="anotherExampleBean" class="examples.AnotherBean"/>  
  12. <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>  

[java] view plain copy
  1. public class ExampleBean {  
  2.   
  3.   private AnotherBean beanOne;  
  4.   private YetAnotherBean beanTwo;  
  5.   private int i;  
  6.   
  7.   public void setBeanOne(AnotherBean beanOne) {  
  8.       this.beanOne = beanOne;  
  9.   }  
  10.   
  11.   public void setBeanTwo(YetAnotherBean beanTwo) {  
  12.       this.beanTwo = beanTwo;  
  13.   }  
  14.   
  15.   public void setIntegerProperty(int i) {  
  16.       this.i = i;  
  17.   }  
  18. }  

上面这个样例使用的是setter注入。

以下是构造器注入的一个样例:

[html] view plain copy
  1. <bean id="exampleBean" class="examples.ExampleBean">  
  2.   
  3. <!-- constructor injection using the nested <ref/> element -->  
  4. <constructor-arg>  
  5.   <ref bean="anotherExampleBean"/>  
  6. </constructor-arg>  
  7.   
  8. <!-- constructor injection using the neater 'ref' attribute -->  
  9. <constructor-arg ref="yetAnotherBean"/>  
  10.   
  11. <constructor-arg type="int" value="1"/>  
  12. </bean>  
  13.   
  14. <bean id="anotherExampleBean" class="examples.AnotherBean"/>  
  15. <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>  

原文地址:https://www.cnblogs.com/yangykaifa/p/7338713.html