首页 > 编程语言 > 详细

spring学习日志二

时间:2019-09-06 15:52:56      阅读:75      评论:0      收藏:0      [点我收藏+]

一、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>

 

spring学习日志二

原文:https://www.cnblogs.com/sitian2050/p/11474745.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!