Spring五种autowire模式

在Spring中,有五种autowire模式:byName,byType,constructor,autodetect,no;
下面分别对其进行介绍
1 byName:这个很简单,直接按照名字进行查找
Java代码

1. package com.jnotnull;
2.
3. public class HelloWorld {
4. public String message = null;
5.
6. public void init(){
7. this.message = "HelloWorld";
8. }
9.
10. public String getMessage() {
11. return message;
12. }
13.
14. public void setMessage(String message) {
15. this.message = message;
16. }
17.
18. }

package com.jnotnull;

public class HelloWorld {
public String message = null;

public void init(){
this.message = "HelloWorld";
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


config.xml
Java代码

1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
3. "http://www.springframework.org/dtd/spring-beans.dtd">
4. <beans>
5. <bean id ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="byName">
6. <property name ="message">
7. <value>HelloWorld</value>
8. </property>
9. <bean>
10. </beans>

<?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 ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="byName">
<property name ="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>


2 byType:按照类型进行查找,如果没有找到匹配的,则什么也没有改变,同时可以通过设置dependency-check="objects"属性来在没有匹配到的情况下抛出这种错误。
Java代码

1. package com.jnotnull;
2.
3. public class HelloWorld {
4. public String message = null;
5.
6. public void init(){
7. this.message = "HelloWorld";
8. }
9.
10. public String getMessage() {
11. return message;
12. }
13.
14. public void setMessage(String message) {
15. this.message = message;
16. }
17.
18. }

package com.jnotnull;

public class HelloWorld {
public String message = null;

public void init(){
this.message = "HelloWorld";
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


config.xml
Java代码

1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
3. "http://www.springframework.org/dtd/spring-beans.dtd">
4. <beans>
5. <bean id ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="byType">
6. <property name ="message">
7. <value>HelloWorld</value>
8. </property>
9. <bean>
10. </beans>

<?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 ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="byType">
<property name ="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>


3 constructor:直接根据构造函数进行匹配
Java代码

1. package com.jnotnull;
2.
3. public class HelloWorld {
4. public String message = null;
5.
6. public void init(){
7. this.message = "HelloWorld";
8. }
9.
10. public String getMessage() {
11. return message;
12. }
13.
14. public void setMessage(String message) {
15. this.message = message;
16. }
17.
18. }

package com.jnotnull;

public class HelloWorld {
public String message = null;

public void init(){
this.message = "HelloWorld";
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


config.xml
Java代码

1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
3. "http://www.springframework.org/dtd/spring-beans.dtd">
4. <beans>
5. <bean id ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="constrator">
6. <property name ="message">
7. <value>HelloWorld</value>
8. </property>
9. <bean>
10. </beans>

<?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 ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="constrator">
<property name ="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>


4 autodetect:先按constructor匹配,再按byType匹配
Java代码

1. package com.jnotnull;
2.
3. public class HelloWorld {
4. public String message = null;
5.
6. public void init(){
7. this.message = "HelloWorld";
8. }
9.
10. public String getMessage() {
11. return message;
12. }
13.
14. public void setMessage(String message) {
15. this.message = message;
16. }
17.
18. }

package com.jnotnull;

public class HelloWorld {
public String message = null;

public void init(){
this.message = "HelloWorld";
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


config.xml
Java代码

1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
3. "http://www.springframework.org/dtd/spring-beans.dtd">
4. <beans>
5. <bean id ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="autodetect">
6. <property name ="message">
7. <value>HelloWorld</value>
8. </property>
9. <bean>
10. </beans>

<?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 ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="autodetect">
<property name ="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>


5 no:不用任何模式进行匹配,这个也是默认的匹配模式
Java代码

1. package com.jnotnull;
2.
3. public class HelloWorld {
4. public String message = null;
5.
6. public void init(){
7. this.message = "HelloWorld";
8. }
9.
10. public String getMessage() {
11. return message;
12. }
13.
14. public void setMessage(String message) {
15. this.message = message;
16. }
17.
18. }

package com.jnotnull;

public class HelloWorld {
public String message = null;

public void init(){
this.message = "HelloWorld";
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


config.xml
Java代码

1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans Public "-//SPRING//DTD BEAN//EN"
3. "http://www.springframework.org/dtd/spring-beans.dtd">
4. <beans>
5. <bean id ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="no">
6. <property name ="message">
7. <value>HelloWorld</value>
8. </property>
9. <bean>
10. </beans>

<?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 ="HelloWorld" class="com.jnotnull.HelloWorld" autowire="no">
<property name ="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值