根据 autowire 的配置选择装配策略
byName 选择和属性名 name 一致的 bean 进行装配;
byType 根据类型选择,如果对应的类型匹配到多个bean,则会报错,如下配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6 7 <bean name="userDAO1" class="com.bjsxt.dao.impl.UserDAOImpl"> 8 <property name="daoId" value="1"></property> 9 </bean> 10 11 <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl"> 12 <property name="daoId" value="2"></property> 13 </bean> 14 15 <bean id="userService" class="com.bjsxt.service.UserService" scope="singleton" autowire="byType"> 16 <!-- <property name="userDAO"> 17 <ref bean="userDAO1"/> 18 </property> --> 19 </bean> 20 21 </beans>
报错:
还能配置在 beans 标签下,设置整个配置文件的装配策略,里面的值也是那几个配置。
代码链接: http://pan.baidu.com/s/1slPPUzz 密码: 1d9e
jar包链接: http://pan.baidu.com/s/1pKAKAQB 密码: fjc3
时间: 2024-11-10 18:35:40