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" 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" 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" 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" 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" 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" 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" 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" 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" 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" autowire="no">
<property name ="message">
<value>HelloWorld</value>
</property>
<bean>
</beans>