Spring中Bean的5种自动装载模式

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

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

<?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"属性来在没有匹配到的情况下抛出这种错误。

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

<?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:直接根据构造函数进行匹配

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

<?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匹配

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

<?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:不用任何模式进行匹配,这个也是默认的匹配模式

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

<?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>

通过上面的介绍,你是不是发现很简单。

你可能感兴趣的:(spring,bean,xml)