Spring域属性自动注入byName和byType

byName 方式
 1 <!--byName约束:bean当中的域属性名必须跟所注入bean的id相同-->
 2 <bean id="student" class="cn.spring.entity.Student" autowire="byName">
 3 <property name="stu_id" value="1"></property>
 4 <property name="stu_name" value="张三"></property>
 5 </bean>
 6  
 7 <bean id="teacher" class="cn.spring.entity.Teacher">
 8 <property name="t_id" value="1"></property>
 9 <property name="t_name" value="王洪涛"></property>
10 </bean>
 
bean当中的域属性名必须跟所注入bean的id相同
 
 1 byType 方式
 2 <!--byType:要求被注入域属性类型只能唯一,与其向兼容的类型也同样不可以-->
 3 <bean id="student2" class="cn.spring.entity.Student" autowire="byType">
 4 <property name="stu_id" value="1"></property>
 5 <property name="stu_name" value="张三"></property>
 6 </bean>
 7  
 8 <bean id="teacher" class="cn.spring.entity.Teacher">
 9 <property name="t_id" value="1"></property>
10 <property name="t_name" value="王洪涛"></property>
11 </bean>
12  
这里的teacher类型在student中只能存在一个才能使用byType自动装配,如果出现同类型(同类型的子类),都无法实现自动装配
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/chx9832/p/11752243.html