你的位置:首页 > 软件开发 > Java > Spring基础——在Spring Config 文件中基于 XML 的 Bean 的自动装配

Spring基础——在Spring Config 文件中基于 XML 的 Bean 的自动装配

发布时间:2016-07-18 12:00:08
一、Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过程。二、配置:在 <bean> 的 autowire 属性里指定自动 ...

一、Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过程。

二、配置:

在 <bean> 的 autowire 属性里指定自动装配的模式。默认为 no 。可以通过 <beans> 根元素的 default-autowire 属性改变默认值。

三、自动装配的三种模式:

1.byType(根据类型自动装配):若 IOC 容器中出现多个与目标 Bean 类型一致的 Bean。Spring 无法识别使用哪个,有二义性。报异常。

2.byName(根据名称的自动装配):必须将当前 Bean 的属性名和目标 Bean 的 id 值设置为相同。

3.construtor(根据构造器自动装配):不推荐使用。

四、例子

e1:之前的方式,通过 <property> 来指定。

<bean class="com.nucsoft.spring.Address" id="address">  <property name="addressName" value="beijing"/></bean><bean class="com.nucsoft.spring.Phone" id="phone" p:phoneNum="123456789"/><bean id="employee" class="com.nucsoft.spring.Employee">  <property name="empName" value="emp01"/>  <property name="phone" ref="phone"/>  <property name="address" ref="address"/></bean>

原标题:Spring基础——在Spring Config 文件中基于 XML 的 Bean 的自动装配

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录