一 setter方法注入
配置文件如下:
<bean id="helloAction" class="org.yoo.action.SpringSetterHelloAction">
<!-- setter injection using the nested <ref/> element -->
<property name="helloservice"><ref bean="helloService"/></property>
<!--可以不設置類型 -->
<property name="name" value="yoo"></property>
</bean>
action實現類中代碼:
private IHelloService helloservice;
private String name ;
public void sayHello(){
helloservice.sayHello();
System.out.println(this.name);
}
public void setHelloservice(IHelloService helloservice) {
this.helloservice = helloservice;
}
public void setName(String name) {
this.name = name;
}
這裏的name和helloservice都採用屬性的setter方法注入。即類中設置一個全局屬性,並對屬性有setter方法,以供容器注入。
二 構造器注入
spring也支持構造器注入,也即有些資料中的構造子或者構造函數注入。
先看配置文件:
<!--普通構造器注入-->
<bean id="helloAction" class="org.yoo.action.ConstructorHelloAction">
<!--type 必須爲java.lang.String 因爲是按類型匹配的,不是按順序匹配-->
<constructor-arg type="java.lang.String" value="yoo"/>
<!-- 也可以使用index來匹配-->
<!--<constructor-arg index="1" value="42"/>-->
<constructor-arg><ref bean="helloService"/></constructor-arg>
</bean>
action實現類中代碼:
private HelloServiceImpl helloservice;
private String name ;
public SpringConstructorHelloAction(HelloServiceImpl helloservice,String name){
this.helloservice = helloservice;
this.name = name ;
}
@Override
public void sayHello() {
helloservice.sayHello();
System.out.println(this.name);
}
同樣設置2個屬性,然後在構造函數中注入。
三靜態工廠注入
配置文件如下:
<!--靜態工廠構造注入-->
<!--注意這裏的class 帶factory-method參數-->
<!--factory-method的值是FactoryHelloAction中的工廠方法名createInstance-->
<bean id="helloAction" class="org.yoo.di.FactoryHelloAction" factory-method="createInstance">
<constructor-arg type="java.lang.String" value="yoo"/>
<constructor-arg><ref bean="helloService"/></constructor-arg>
</bean>
action實現類:
private HelloServiceImpl helloservice;
private String name = null;
private SpringFactoryHelloAction(String name ,HelloServiceImpl helloservice){
this.helloservice = helloservice ;
this.name = name ;
}
public static SpringFactoryHelloAction createInstance(String name ,HelloServiceImpl helloservice) {
SpringFactoryHelloAction fha = new SpringFactoryHelloAction (name,helloservice);
// some other operations
return fha;
}
@Override
public void sayHello() {
helloservice.sayHello();
System.out.println(this.name);
}
四 無配置文件注入(自動注入)
上面三種方法都需要編寫配置文件,在spring2.5中還提供了不編寫配置文件的ioc實現。需要注意的是,無配置文件指bean之間依賴,不基於配置文件,而不是指沒有spring配置文件。
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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 class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="helloService" class="org.yoo.service.HelloServiceImpl">
</bean>
<bean id="helloAction" class="org.yoo.action.AutowiredHelloAction">
</bean>
</beans>
可見上面只是設置了helloService和helloAction兩個bean,並沒有設置helloAction對helloService的依賴。
另外<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>是必須的,有此才支持自動注入。
action實現類:
/*
* 屬性自動裝載
*/
/*
@Autowired
private HelloService helloservice;
@Override
public void sayHello() {
helloservice.sayHello();
。
上面代碼很簡單,在屬性上加上 @Autowired註釋,spring會自動注入HelloService 。
同樣也支持構造器和setteer方法的注入,如下:
構造器自動注入:
/*
* 還可以使用構造函數設置
*/
@Autowired
public SpringAutowiredHelloAction(HelloServiceImpl helloservice){
this.helloservice = helloservice;
}
setter方法自動注入:
/*
* 也可以使用set方法設置
*/
/*
@Autowired
public void setHelloservice(HelloService helloservice) {
this.helloservice = helloservice;
}
最後在spring的reference文檔中有提到,如果不使用自動注入,儘量使用setter方法,一般通用的也是使用setter方法。
而使用自動注入還是配置文件方式,如果jdk低於1.5或者spring不是2.5,就只能夠使用配置文件方式了。其它的就看實際項目選擇了。