对于自动装配,了解一下就可以,实际应用中并不被推荐使用。例子:
Autowire属性取值如下:
byType:
按照类型自动装配,可以根据属性的类型,在容器中寻找跟类型匹配的bean。如果发现
多个,那么会抛出异常。如果没有找到,即属性值为null.
byname:
按照名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有
找到,即属性值为null。
constructor
与byType的方式类似,不同之处在于它应用于构造器参数。如果在人那个其中没有找
到,即属性值为null。
autodetect
通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。(如果发现默认的构造器,那么将使用byType方式)
例子:
<bean id="personDaoxxx" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean"
class="cn.itcast.service.impl.PersonServiceBean" autowire="byType"/>
Autowire属性取值如下:
byType:
按照类型自动装配,可以根据属性的类型,在容器中寻找跟类型匹配的bean。如果发现
多个,那么会抛出异常。如果没有找到,即属性值为null.
byname:
按照名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有
找到,即属性值为null。
constructor
与byType的方式类似,不同之处在于它应用于构造器参数。如果在人那个其中没有找
到,即属性值为null。
autodetect
通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。(如果发现默认的构造器,那么将使用byType方式)
例子:
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
public PersonServiceBean() {
}
public void save()
{
personDao.save();
}
}
<bean id="personDaoxxx" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean"
class="cn.itcast.service.impl.PersonServiceBean" autowire="byType"/>