一 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
public SpringAutowiredHelloAction(HelloServiceImpl helloservice){
this.helloservice = helloservice;
}
setter方法自動注入:
/*
@Autowired
public void setHelloservice(HelloService helloservice) {
this.helloservice = helloservice;
}
最後在spring的reference文檔中有提到,如果不使用自動注入,盡量使用setter方法,一般通用的也是使用setter方法。
而使用自動注入還是配置檔案方式,如果jdk低于1.5或者spring不是2.5,就隻能夠使用配置檔案方式了。其它的就看實際項目選擇了。