1、no:不自动装载
2、byName:根据实例变量名称自动装载
按照类中声明的实例变量的名称,与XML配置文件中声明的bean定义的beanName的值进行匹配,相匹配的bean定义将被自动绑定到当前实例变量上。这种方式对类定义和配置的bean定义有一定的限制代码如下:
public class Xxx{
private UserService userService;
}
<bean id="xxx" class="com.xxx...Xxx" autowire="byName"></bean>
<bean id="userService" class="com.xxx...UserService"></bean>
此处Xxx中的变量userService的名字和配置文件中UserService的bean的id一致
3、byType:根据实例变量类型自动装载
按照类型个什么的变量的类型,在容器管理处找到变量类型相同的bean的定义,找到之后自动绑定
public class Xxx{
private UserService userService;
}
<bean id="xxx" class="com.xxx...Xxx" autowire="byType"></bean>
此时Xxx中的变量名userService可以和配置文件中的UserService的bean的id不一致,因为此时是根据类型绑定的,但是如果出现两个或多个同类型的bean定义,将出错,需要自己手动指定
4、constructor:根据构造方法参数类型自动装载
public class Xxx{
private UserService userService;
public Xxx(UserService userService){
this.userService = userService;
}
}
<bean id="xxx" class="com.xxx...Xxx" autowire="constractor"> </bean><bean id="userService1" class="com.xxx...UserService"></bean>
这个跟 byType装载有些相似,如果容器中出现两个相同UserService类型的对象,就会报错
5、autodetect:byType方式和constructor方式结合
text-transform: none; white-space: no如果对象拥有默认无参数的构造方法,容器会优先考虑byType的自动绑定模式。否则,会使用constructor模式。当然,如果通过构造方法注入绑定后还有其他属性没有绑定,容器也会使用byType对剩余的对象属性进行自动绑定。