示例代码:
1. Constructor injection
1.1 在Bean的配置文件中,使用<constructor-arg>来表示将使用Constructor injection,由于使用Constructor injection时并不如Setter injection时拥有setXXX()这样易懂的名称,所以必须指定参数的位置索引,index属性就是用于指定对象将注入至建构函式中的哪一个参数,参数的顺序指定中,第一个参数的索引值是0,第二个是1,依此类推。
示例配置:
1.2 如果constructor上只有一个参数,则不必指定index属性。
示例配置:
1.3 若有两个以上的参数,而参数类型各不相同的话,可以使用type来指定constructor上的参数类型。
示例代码:
示例配置:
2. property injection
示例代码:
2.1 直接指定值或是使用<ref>直接指定参考至其它的Bean
示例配置:
2.2 也可以用内部Bean的方式来注入依赖关系
示例配置:
3. 自动绑定
直接指定值或是使用<ref>直接指定参考至其它的Bean,Spring也支持隐式的自动绑定,您可以通过类型(byType)或名称(byName)将Bean绑定至其它Bean上对应的属性:
3.1 byType
在配置文件中,并没有指定helloBean的Date属性,而是透过自动绑定,由于autowire指定了byType,所以会根据helloBean 的 Date属性所接受的类型,判断是否有类似的类型对象,并将之绑定至helloBean的Date属性上,如果byType无法完成绑定,则抛出 org.springrframework.beans.factory.UnsatisfiedDependencyExcpetion异常。
示例配置:
3.2 byName
也可以指定byName来绑定,则Spring会根据bean的别名与属性名称是否符合来进行自动绑定,举个例子来说,如果是byName而Date属性要完成依赖注入的话,则必须修改一下第一个Bean的id值为date:
示例配置:
3.3 constructor也可以尝试进行自动绑定
由于autowire设定为constructor,在建立绑定关系时,Spring容器会试图比对容器中的Bean及相关的建构方法,在类型上是否有符合,如果有的话,则选用该建构方法来建立Bean实例,例如上例中,HelloBean的带参数建构方法与date这个Bean的类型相符,于是选用该构造方法来建构实例,并将date这个Bean注入给它,如果无法完成绑定,则抛出 org.springframework.beans.factory.UnsatisfiedDependencyException异常。
示例配置:
3.4 autodetect
使用autodetect时,会尝试使用constructor,然后使用byType,哪一个先符合就先用。
示例配置:
3.5 其它:
隐式的自动绑定没办法从配置文件中清楚的看到是否每个属性都完成设定,我们可以加入相依检查,在<bean>上加入dependency- check,有四种相依检查方式:simple、objects、all、none。
simple只检查简单的属性,像是原生(primitive)数据型态或字符串对象;
objects检查对象属性;
all则检查全部的属性;
none是预设,表示不检查相依性;
示例配置:
4. 集合对象注入:
对于像数组、java.util.List、java.util.Set、java.util.Map等集合对象,在注入前必须填充入一些对象至集合中,然后再将集合对象注入至所需的Bean中,例如若有个Bean如下:
示例代码:
示例配置:
上面的Bean定义文件是个综合示范,数组与List对象都是用<list>卷标来设定,而Map对象使用<map>卷标设定,并需要一个key值设定。
Set的使用<set>标签,例如:
您也可以注入java.util.Properties,Bean定义档的写法示范如下:
package org.sixsun.spring;
public class HelloBean {
private String name;
private String helloWord;
public HelloBean() {
}
public HelloBean(String name, String helloWord) {
this.name = name;
this.helloWord = helloWord;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
public String getHelloWord() {
return helloWord;
}
}
1. Constructor injection
1.1 在Bean的配置文件中,使用<constructor-arg>来表示将使用Constructor injection,由于使用Constructor injection时并不如Setter injection时拥有setXXX()这样易懂的名称,所以必须指定参数的位置索引,index属性就是用于指定对象将注入至建构函式中的哪一个参数,参数的顺序指定中,第一个参数的索引值是0,第二个是1,依此类推。
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class=" org.sixsun.spring.HelloBean">
<constructor-arg index="0">
<value>sixsun</value>
</constructor-arg>
<constructor-arg index="1">
<value>HelloSpring</value>
</constructor-arg>
</bean>
</beans>
1.2 如果constructor上只有一个参数,则不必指定index属性。
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class=" org.sixsun.spring.HelloBean">
<constructor-arg>
<value>sixsun</value>
</constructor-arg>
</bean>
</beans>
1.3 若有两个以上的参数,而参数类型各不相同的话,可以使用type来指定constructor上的参数类型。
示例代码:
package org.sixsun.spring;
public class HelloBean {
private String name;
private Integer age;
public HelloBean() {
}
public HelloBean(String name, Integer age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
}
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class=" org.sixsun.spring.HelloBean">
<constructor-arg type="java.lang.String">
<value>sixsun</value>
</constructor-arg>
<constructor-arg type="java.lang.Integer">
<value>23</value>
</constructor-arg>
</bean>
</beans>
2. property injection
示例代码:
package org.sixsun.spring;
import java.util.Date;
public class HelloBean {
private String helloWord;
private Date date;
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
public String getHelloWord() {
return helloWord;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDate() {
return date;
}
}
2.1 直接指定值或是使用<ref>直接指定参考至其它的Bean
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean" class="org.sixsun.spring.HelloBean">
<property name="helloWord">
<value>Hello!</value>
</property>
<property name="date">
<ref bean="dateBean"/>
</property>
</bean>
</beans>
2.2 也可以用内部Bean的方式来注入依赖关系
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="org.sixsun.spring.HelloBean">
<property name="helloWord">
<value>Hello!</value>
</property>
<property name="date">
<bean id="dateBean" class="java.util.Date"/>
</property>
</bean>
</beans>
3. 自动绑定
直接指定值或是使用<ref>直接指定参考至其它的Bean,Spring也支持隐式的自动绑定,您可以通过类型(byType)或名称(byName)将Bean绑定至其它Bean上对应的属性:
3.1 byType
在配置文件中,并没有指定helloBean的Date属性,而是透过自动绑定,由于autowire指定了byType,所以会根据helloBean 的 Date属性所接受的类型,判断是否有类似的类型对象,并将之绑定至helloBean的Date属性上,如果byType无法完成绑定,则抛出 org.springrframework.beans.factory.UnsatisfiedDependencyExcpetion异常。
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dateBean" class="java.util.Date"/>
<bean id="helloBean"
class="org.sixsun.spring.HelloBean"
autowire="byType">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
3.2 byName
也可以指定byName来绑定,则Spring会根据bean的别名与属性名称是否符合来进行自动绑定,举个例子来说,如果是byName而Date属性要完成依赖注入的话,则必须修改一下第一个Bean的id值为date:
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="date" class="java.util.Date"/>
<bean id="helloBean"
class="org.sixsun.spring.HelloBean"
autowire="byName">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
3.3 constructor也可以尝试进行自动绑定
由于autowire设定为constructor,在建立绑定关系时,Spring容器会试图比对容器中的Bean及相关的建构方法,在类型上是否有符合,如果有的话,则选用该建构方法来建立Bean实例,例如上例中,HelloBean的带参数建构方法与date这个Bean的类型相符,于是选用该构造方法来建构实例,并将date这个Bean注入给它,如果无法完成绑定,则抛出 org.springframework.beans.factory.UnsatisfiedDependencyException异常。
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="date" class="java.util.Date"/>
<bean id="helloBean"
class="org.sixsun.spring.HelloBean"
autowire="constructor">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
3.4 autodetect
使用autodetect时,会尝试使用constructor,然后使用byType,哪一个先符合就先用。
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="date" class="java.util.Date"/>
<bean id="helloBean"
class="org.sixsun.spring.HelloBean"
autowire="autodetect">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
3.5 其它:
隐式的自动绑定没办法从配置文件中清楚的看到是否每个属性都完成设定,我们可以加入相依检查,在<bean>上加入dependency- check,有四种相依检查方式:simple、objects、all、none。
simple只检查简单的属性,像是原生(primitive)数据型态或字符串对象;
objects检查对象属性;
all则检查全部的属性;
none是预设,表示不检查相依性;
示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="date" class="java.util.Date"/>
<bean id="helloBean"
class="org.sixsun.spring.HelloBean"
autowire="autodetect"
dependency-check="all">
<property name="helloWord">
<value>Hello!</value>
</property>
</bean>
</beans>
4. 集合对象注入:
对于像数组、java.util.List、java.util.Set、java.util.Map等集合对象,在注入前必须填充入一些对象至集合中,然后再将集合对象注入至所需的Bean中,例如若有个Bean如下:
示例代码:
public class SomeBean {
private String[] someStrArray;
private SomeObj[] someObjArray;
private List someList;
private Map someMap;
....
}
示例配置:
<beans>
<bean id="someBean" class="onlyfun.caterpillar.SomeBean">
<property name="someArray">
<list>
<value>Hello!Justin!</value>
<value>Hello!Momor!</value>
<value>Hello!Bush!</value>
</list>
</property>
<property name="someObjArray">
<list>
<ref bean="someObj1"/>
<ref bean="someObj2"/>
</list>
</property>
<property name="someList">
<list>
<value>Hello!Justin!</value>
<ref bean="someObj1"/>
<ref bean="someObj2"/>
</list>
</property>
<property name="someMap">
<map>
<entry key="somekey1">
<ref bean="someObj1"/>
</entry>
<entry key="somekey2">
<value>Hello!Justin!</value>
</entry>
</map>
</property>
</bean>
</beans>
上面的Bean定义文件是个综合示范,数组与List对象都是用<list>卷标来设定,而Map对象使用<map>卷标设定,并需要一个key值设定。
Set的使用<set>标签,例如:
<set>
<value>a set element</value>
<ref bean="otherBean"/>
<ref bean="anotherBean"/>
</set>
您也可以注入java.util.Properties,Bean定义档的写法示范如下:
<bean id=....>
....
<property name="someProperties">
<props>
<prop key="someProkey1">
someProValue1
</prop>
<prop key="someProkey2">
someProValue2
</prop>
</props>
</property>
</bean>