一、spring依赖注入的方式
1.通过set方法来完成注入
<bean id="student" class="com.zhiyou100.xz.spring.Student" scope="prototype">
<!-- property:通过set方法来注入Student类 的name属性值-->
<property name="name" value="李四"/>
<property name="age" value="18"/>
</bean>
2.通过构造方法来完成依赖注入
<bean id="student2" class="com.zhiyou100.xz.spring.Student"> <!-- constructor-arg:构造方法的参数 index:第几个参数 索引从0开始 只有一个参数时,即当index="0"时value的值先默认的是string类型,要用name区分不同的参数名 name:通过构造方法的参数名 --> <!-- 当一个构造方法有两个参数时,要用两个constructor-arg表示 --> <constructor-arg index="0" value="里斯"/> <constructor-arg index="1" value="18"/> </bean>
原文:https://www.cnblogs.com/sitian2050/p/11474745.html