360SDN.COM

spring依赖注入(dependency injection)的几种实现方式

来源:  2013-05-04 20:37:09    评论:0点击:

spring依赖注入(dependency injection)的几种实现方式

依赖注入(Dependency Injection )主要是解决类之间的依赖关系,主要有两种注入方式setter属性注入(Setter Injection)和构造器注入(Constructor Injection)。

spring的setter注入示例一

这种方式是使用最广泛的和最简单的依赖注入(DI)方式。通过一个setter方法注入。

java示例代码:

 package com.company; 
import com.company.ExcelParser;
 
public class Excel{
	IExcelParser excelParser;
 
	public void setExcelParser(IExcelParser excelParser){
		this.excelParser = excelParser;
	}
 
}
  

Spring的xml配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean id="excel" class="com.company.Excel">
		<property name=" excelParser ">
			<ref bean="poiExcelParser" />
		</property>
	</bean>
<bean id=" poiExcelParser " class="com.company.PoiExcelParser " /> 
 
</beans>

以上属性注入也可以写成如下:

<!—通过ref属性进行setter注入 -->
<property name=" excelParser " ref=" poiExcelParser "/>

spring的setter注入示例二

java代码如下:

public class ExampleBean {

  private OneBean oneBean;
  private SecondBean secondBean;
  private int i;

  public void setOneBean(OneBean oneBean) {
      this.oneBean = oneBean;
  }

  public void setSecondBean(SecondBean secondBean) {
      this.secondBean = secondBean;
  }

  public void setIntProperty(int i) {
      this.i = i;
  }
}

Spring配置文件如下:

<bean id="exampleBean" class="examples.ExampleBean">

<!-- 使用嵌套的 <ref/>元素 -->
<property name="oneBean"><ref bean="oneBean"/></property>

<!--使用ref属性 -->
<property name="secondBean" ref="secondBean"/>
<property name="intProperty" value="1"/>
</bean>
<bean id="oneBean" class="examples.OneBean"/>
<bean id="secondBean" class="examples.SecondBean"/>

spring的构造器注入

java示例代码如下:

package com.company;
  
public class Excel{
		IExcelParser excelParser;
 
        Excel(IExcelParser excelParser){
		this.excelParser = excelParser;
	}
}

Spring的配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean id="excel" class="com.company.Excel">
		<constructor-arg>
			<bean class="com.company.PoiExcelParser" />
		</constructor-arg>
	</bean>
 
<bean id="poiExcelParser" class="com.company.PoiExcelParser" /> 
 
</beans>

spring的构造器注入示例二

java代码如下:


public class ExampleBean {

  private OneBean oneBean;
  private SecondBean secondBean;
  private int i;

  public ExampleBean(
      OneBean oneBean, SecondBean secondBean, int i) {
      this.oneBean = oneBean;
      this.secondBean = secondBean;
      this.i = i;
  }
}

Spring的xml配置文件如下:

<bean id="exampleBean" class="examples.ExampleBean">
 
<constructor-arg>
  <ref bean="oneBean"/>
</constructor-arg> 
<constructor-arg ref="secondBean"/>

<constructor-arg type="int" value="1"/>
</bean>

<bean id="oneBean" class="examples.OneBean"/>
<bean id="secondBean" class="examples.SecondBean"/>
为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权