Java培训教程给bean的属性赋值

发布时间:2020年09月09日作者:atguigu浏览次数:966

依赖注入的方式

1. 通过bean的setXxx()方法赋值

Hello World中使用的就是这种方式

Java培训教程

2. 通过bean的构造器赋值

  1. Spring自动匹配合适的构造器
     <bean id=“book” class=“com.atguigu.spring.bean.Book” >            <constructor-arg value= “10010”/>            <constructor-arg value= “Book01”/>            <constructor-arg value= “Author01”/>            <constructor-arg value= “20.2”/>      </bean >
  • 通过索引值指定参数位置
     <bean id=“book” class=“com.atguigu.spring.bean.Book” >            <constructor-arg value= “10010” index =“0”/>            <constructor-arg value= “Book01” index =“1”/>            <constructor-arg value= “Author01” index =“2”/>            <constructor-arg value= “20.2” index =“3”/>      </bean >
  • 通过类型区分重载的构造器
<bean id=“book” class=“com.atguigu.spring.bean.Book” >       <constructor-arg value= “10010” index =“0” type=“java.lang.Integer” />       <constructor-arg value= “Book01” index =“1” type=“java.lang.String” />       <constructor-arg value= “Author01” index =“2” type=“java.lang.String” />       <constructor-arg value= “20.2” index =“3” type=“java.lang.Double” /> </bean >

上一篇:
下一篇:
相关课程

java培训 大数据培训 前端培训

关于尚硅谷
教育理念
名师团队
学员心声
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
全国统一咨询电话:010-56253825
地址:北京市昌平区宏福科技园2号楼3层(北京校区)

深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦3层(上海校区)

武汉市东湖高新开发区东湖网谷(武汉校区)

西安市雁塔区和发智能大厦B座3层(西安校区)

成都市成华区北辰星拱青创园综合楼3层(成都校区)