您的位置:首页 > 编程语言 > Java开发

Spring 自学系列之IOC-8【自动装配autowire】

2018-03-18 21:18 543 查看
spring提供了bean的自动装配功能,用autowire来标识,可以使用根据名字来找或者根据类型来找。
1) byName
2) byType【如果同一个类型的在context中对应了多个bean,则会报错】
3) 如果所有的bean都用同一种,可以使用beans的属性:default-autowire
例子如下:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >

<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="1"></property>
</bean>

<bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>

<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType">
</bean>

</beans>因为是根据类型找的,而对应的com.bjsxt.dao.impl.UserDAOImpl类型有两个bean,所以会报错!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: