Bean的配置示例代码如下:
<?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.xsd">
<!-- 使用id属性定义myTestDIDao,其对应的实现类为dao.TestDIDaoImpl-->
<bean id="myTestDIDao" class="dao.TestDIDaoImpl" />
<!-- 使用构造方法注入 -->
<bean id="testDIService" class="service.TestDIServiceImpl">
<!--给构造方法传引用类型的参数值myTestDIDao-->
<constructor-arg index="0" ref="myTestDIDao"/>
</bean>
</beans>
属性或子元素名称 | 描述 |
---|---|
id | Bean在BeanFactory中唯一的标识 |
class | Bean的具体实现类,使用类的名(例如dao.TestDIDaoImpl) |
scope | 指定Bean实例的作用域 |
< constructor > | 使用构造方法注入。该元素的index属性指定参数的序号,ref属性指定对BeanFactory中其他Bean的引用关系 |
< property > | 用于设置一个属性,该元素的name属性指定Bean实例中相应的属性名称,value属性指定Bean的属性值,ref属性指定属性对BeanFactory中其他Bean的引用关系。 |
< list > | < property >的子元素,用于封装List或数组类型的依赖注入。 |
< set > | < property >的子元素,用于封装Set类型的依赖注入 |
< map > | < property >的子元素,用于封装Map类型的依赖注入 |