自动装配:
根据指定的装配规则(属性名称或者类型),spring自动将匹配的属性值进行注入
手动装配
public class Employee {
private Department dept ;
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee{" +
"dept=" + dept +
'}';
}
public void Test(){
System.out.println(dept);
}
}
手动装配:
<bean id ="emp" class="Autowire.Employee">
<property name="dept" ref="dept"></property>
</bean>
<bean id ="dept" class="Autowire.Department"></bean>
自动装配,是在bean里面设置属性antowire的值,其值有byname(根据属性名称)、bytype(根据属性类型)
<bean id ="emp" class="Autowire.Employee" autowire="byName">
<!--property name="dept" ref="dept"></property-->
</bean>
<bean id ="dept" class="Autowire.Department"></bean>
<bean id ="emp" class="Autowire.Employee" autowire="byType">
</bean>
<bean id ="dept" class="Autowire.Department"></bean>
如果有多个外部bean,则只能byname注入,调用过程会根据需要注入的属性名称为dept,从而调用外部bean中id为dept的类然后注入;