Spring五种autowire模式
在Spring中,有五种autowire模式:byName,byType,constructor,autodetect,no;
下面分别对其进行介绍
1byName:这个很简单,直接按照名字进行查找
Java代码
1.packagecom.jnotnull;
2.
3.publicclassHelloWorld{
4.publicStringmessage=null;
5.
6.publicvoidinit(){
7.this.message="HelloWorld";
8.}
9.
10.publicStringgetMessage(){
11.returnmessage;
12.}
13.
14.publicvoidsetMessage(Stringmessage){
15.this.message=message;
16.}
17.
18.}
packagecom.jnotnull;
publicclassHelloWorld{
publicStringmessage=null;
publicvoidinit(){
this.message="HelloWorld";
}
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
}
config.xml
Java代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
3."http://www.springframework.org/dtd/spring-beans.dtd">
4.<beans>
5.<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="byName">
6.<propertyname="message">
7.<value>HelloWorld</value>
8.</property>
9.<bean>
10.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="byName">
<propertyname="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>
2byType:按照类型进行查找,如果没有找到匹配的,则什么也没有改变,同时可以通过设置dependency-check="objects"属性来在没有匹配到的情况下抛出这种错误。
Java代码
1.packagecom.jnotnull;
2.
3.publicclassHelloWorld{
4.publicStringmessage=null;
5.
6.publicvoidinit(){
7.this.message="HelloWorld";
8.}
9.
10.publicStringgetMessage(){
11.returnmessage;
12.}
13.
14.publicvoidsetMessage(Stringmessage){
15.this.message=message;
16.}
17.
18.}
packagecom.jnotnull;
publicclassHelloWorld{
publicStringmessage=null;
publicvoidinit(){
this.message="HelloWorld";
}
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
}
config.xml
Java代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
3."http://www.springframework.org/dtd/spring-beans.dtd">
4.<beans>
5.<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="byType">
6.<propertyname="message">
7.<value>HelloWorld</value>
8.</property>
9.<bean>
10.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="byType">
<propertyname="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>
3constructor:直接根据构造函数进行匹配
Java代码
1.packagecom.jnotnull;
2.
3.publicclassHelloWorld{
4.publicStringmessage=null;
5.
6.publicvoidinit(){
7.this.message="HelloWorld";
8.}
9.
10.publicStringgetMessage(){
11.returnmessage;
12.}
13.
14.publicvoidsetMessage(Stringmessage){
15.this.message=message;
16.}
17.
18.}
packagecom.jnotnull;
publicclassHelloWorld{
publicStringmessage=null;
publicvoidinit(){
this.message="HelloWorld";
}
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
}
config.xml
Java代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
3."http://www.springframework.org/dtd/spring-beans.dtd">
4.<beans>
5.<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="constrator">
6.<propertyname="message">
7.<value>HelloWorld</value>
8.</property>
9.<bean>
10.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="constrator">
<propertyname="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>
4autodetect:先按constructor匹配,再按byType匹配
Java代码
1.packagecom.jnotnull;
2.
3.publicclassHelloWorld{
4.publicStringmessage=null;
5.
6.publicvoidinit(){
7.this.message="HelloWorld";
8.}
9.
10.publicStringgetMessage(){
11.returnmessage;
12.}
13.
14.publicvoidsetMessage(Stringmessage){
15.this.message=message;
16.}
17.
18.}
packagecom.jnotnull;
publicclassHelloWorld{
publicStringmessage=null;
publicvoidinit(){
this.message="HelloWorld";
}
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
}
config.xml
Java代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
3."http://www.springframework.org/dtd/spring-beans.dtd">
4.<beans>
5.<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="autodetect">
6.<propertyname="message">
7.<value>HelloWorld</value>
8.</property>
9.<bean>
10.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="autodetect">
<propertyname="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>
5no:不用任何模式进行匹配,这个也是默认的匹配模式
Java代码
1.packagecom.jnotnull;
2.
3.publicclassHelloWorld{
4.publicStringmessage=null;
5.
6.publicvoidinit(){
7.this.message="HelloWorld";
8.}
9.
10.publicStringgetMessage(){
11.returnmessage;
12.}
13.
14.publicvoidsetMessage(Stringmessage){
15.this.message=message;
16.}
17.
18.}
packagecom.jnotnull;
publicclassHelloWorld{
publicStringmessage=null;
publicvoidinit(){
this.message="HelloWorld";
}
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
}
config.xml
Java代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
3."http://www.springframework.org/dtd/spring-beans.dtd">
4.<beans>
5.<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="no">
6.<propertyname="message">
7.<value>HelloWorld</value>
8.</property>
9.<bean>
10.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPublic"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="HelloWorld"class="com.jnotnull.HelloWorld"autowire="no">
<propertyname="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>