当前位置:编程学习 > JAVA >>

Spring 3 Dependency injection

[html] 
DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection. 
1. 基于构造方法的DI (Constructor-based dependency injection)

构造方法中的参数可以是原生数据类型,也可以是引用类型

引用类型:

[html] 
public class SimpleMovieLister { 
 
  // the SimpleMovieLister has a dependency on a MovieFinder 
  private MovieFinder movieFinder; 
 
  // a constructor so that the Spring container can 'inject' a MovieFinder 
  public SimpleMovieLister(MovieFinder movieFinder) { 
      this.movieFinder = movieFinder; 
  } 
 
  // business logic that actually 'uses' the injected MovieFinder is omitted... 

[html] 
package x.y; 
public class Foo {   
public Foo(Bar bar, Baz baz) {  
     // ...   

 

[html] 
<beans> 
  <bean id="foo" class="x.y.Foo"> 
      <constructor-arg ref="bar"/> 
      <constructor-arg ref="baz"/> 
  </bean> 
 
  <bean id="bar" class="x.y.Bar"/> 
  <bean id="baz" class="x.y.Baz"/> 
 
</beans> 


简单类型:

[html]
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; 
  } 

方式一通过类型:
[html] 
<bean id="exampleBean" class="examples.ExampleBean"> 
<constructor-arg type="int" value="7500000"/> 
<constructor-arg type="java.lang.String" value="42"/> 
</bean> 
方式二通过索引:

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

在Spring3.0后,还提供了@ConstructorProperties注解的方法对构造方法的参数进行注入

[html] 
<bean id="exampleBean" class="examples.ExampleBean"> 
<constructor-arg name="years" value="7500000"/> 
<constructor-arg name="ultimateanswer" value="42"/> 
</bean> 
[html]
package examples; 
 
public class ExampleBean { 
 
  // Fields omitted 
 
  @ConstructorProperties({"years", "ultimateAnswer"}) 
  public ExampleBean(int years, String ultimateAnswer) { 
      this.years = years; 
      this.ultimateAnswer = ultimateAnswer; 
  } 

2. 基于Setter方法的DI (Constructor-based dependency injection)
Setter-based DI: 通过setter方法完成注入

[html]
<bean id="exampleBean" class="examples.ExampleBean"> 
 
<!-- setter injection using the nested <ref/> element --> 
<property name="beanOne"><ref bean="anotherExampleBean"/></property> 
 
<!-- setter injection using the neater 'ref' attribute --> 
<property name="beanTwo" ref="yetAnotherBean"/> 
<property name="integerProperty" value="1"/> 
</bean> 
 
<bean id="anotherExampleBean" class="examples.AnotherBean"/> 
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/> 
[html] 
public class ExampleBean { 
 
  private AnotherBean beanOne; 
  private YetAnotherBean beanTwo; 
  private int i; 
 
  public void setBeanOne(AnotherBean beanOne) { 
      this.beanOne = beanOne; 
  } 
 
  public void setBeanTwo(YetAnotherBean beanTwo) { 
      this.beanTwo = beanTwo; 
  } 
 
  public void setIntegerProperty(int i) { 
      this.i = i; 
  } 

constructor-based DI:

[html] 
<bean id="exampleBean" class="examples.ExampleBean"> 
 
<!-- constructor injection using the nested <ref/> element --> 
<constructor-arg> 
  <ref bean="anotherExampleBean"/> 
</constructor-arg> 
 
<!-- constructor injection using the neater 'ref' attribute --> 
<constructor-arg ref="yetAnotherBean"/> 
 
<constructor-arg type="int" value="1"/> 
</bean> 
 
<bean id="anotherExampleBean" class="examples.AnotherBean"/> 
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/> 

[html]
public class ExampleBean {   
  private AnotherBean beanOne; 
  private YetAnotherBean beanTwo;  
  private int i;   
  public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { 
      this.beanOne = anotherBean;       
      this.beanTwo = yetAnotherBean; 
      this.i = i;   
 } 

通过静态工厂方法进行注入
[html] 
<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance"> 
<constructor-arg ref="anotherExampleBean"/> 
<constructor-arg ref="yetAnotherBean"/> 
<constructor-arg value="1"/> 
</bean> 
 
<bean id="anotherExampleBean" class="examples.AnotherBean"/> 
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/> 
[html] 
public class ExampleBean { 
 
  // a private constructor 
  private ExampleBean(...) { 
    ... 
  } 
   
  // a static factory method; the arguments to this method can be 
  // considered the dependencies of the bean that

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,