3.依赖注入setter注入和构造器注入
依赖注入的第一种方式,叫setter注入。
其实就是通过我们当前为属性为成员变量提前设置好的一些方式,然后通过这些方式,比如说set,比如说构造器,通过这个方式,为我们的成员变量赋值的一个过程
ioc中配置的bean :
<bean id="studentTwo" class="com.tian.spring.pojo.Student"><!-- property标签:通过组件类的setXxx()方法给组件对象设置属性 --><!-- name属性:指定属性名(这个属性名是getXxx()、setXxx()方法定义的,和成员变量无关)--><!-- value属性:指定属性值 --><property name="sid" value="1001"></property><property name="sname" value="张三"></property><property name="age" value="23"></property><property name="gender" value="男"></property></bean>
测试类:
@Testpublic void testDI(){//获取IOC容器ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");//获取beanStudent student = ioc.getBean("studentTwo", Student.class);System.out.println(student);}
构造器注入
ioc中配置的bean :
<bean id="studentThree" class="com.tian.spring.pojo.Student"><constructor-arg value="1002"></constructor-arg><constructor-arg value="李四"></constructor-arg><constructor-arg value="女"></constructor-arg><constructor-arg value="24" name="age"></constructor-arg></bean>
测试类:
@Testpublic void testDI(){//获取IOC容器ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");//获取beanStudent student = ioc.getBean("studentThree", Student.class);System.out.println(student);}
实体类
package com.tian.spring.pojo;public class Student implements Person{private Integer sid;private String sname;private Integer age;private String gender;private Double score;public Student() {}public Student(Integer sid, String sname, String gender,Integer age) {this.sid = sid;this.sname = sname;this.gender = gender;this.age = age;}public Student(Integer sid, String sname, String gender,Double score) {this.sid = sid;this.sname = sname;this.gender = gender;this.score = score;}public Integer getSid() {return sid;}public void setSid(Integer sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Double getScore() {return score;}public void setScore(Double score) {this.score = score;}@Overridepublic String toString() {return "Student{" +"sid=" + sid +", sname='" + sname + '\'' +", age=" + age +", gender='" + gender + '\'' +", score=" + score +'}';}
}
会发现我们使用构造器注入出现的结果不正确
Student{sid=1002, sname='李四', age=null, gender='女', score=24.0}
所以需要我们去ioc容器中配置
注意:
constructor-arg标签还有两个属性可以进一步描述构造器参数:
- index属性:指定参数所在位置的索引(从0开始)
- name属性:指定参数名
测试类:
@Testpublic void testDI(){//获取IOC容器ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");//获取beanStudent student = ioc.getBean("studentThree", Student.class);System.out.println(student);}
正确的结果:
Student{sid=1002, sname='李四', age=80, gender='女', score=null}
3.依赖注入setter注入和构造器注入相关推荐
- Spring 5 DI 相关内容 5.2 构造器 注入 5.2.5 构造器注入的一些问题
Spring [黑马程序员2022新版SSM框架教程_Spring+SpringMVC+Maven高级+SpringBoot+MyBatisPlus企业实用开发技术] 5 DI 相关内容 文章目录 S ...
- 【源码分析】Spring的循环依赖(setter注入、构造器注入、多例、AOP)
写在前面 首先最简单的循环依赖demo就是:A->B 且 B->A.本文围绕这个例子去讲解setter注入的循环依赖.构造器注入循环依赖.多例的循环依赖.带AOP的循环依赖.以下是一些结论 ...
- Spring的setter方法注入和构造器注入的对比
我们知道,Spring的依赖注入,有setter方法注入,实例变量注入,构造器注入等. Spring官方文档里,提到: 依赖注入存在两种主要形式: 构造器注入 setter方法注入 注:其实对于Spr ...
- Spring的三种注入方式(为什么推荐构造器注入?)
Spring的三种注入方式 一.属性注入 @Resource和@Autowired的区别 为什么尽量使用 @Resource? 二.setter 注入 三.构造器注入 具体可以看看Spring官网 为 ...
- 依赖注入_set方法注入_构造器注入
属性注入 属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象 属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性 ...
- @Autowired注入、构造器注入、setter注入的使用方式?区别?
文章目录 三种注入方式的使用 1.@Autowired注入(不推荐) 2.1.构造器注入(推荐) 2.2.构造器注入-lombok升级版(推荐) 3.setter注入 循环依赖问题如何解决? 注入方式 ...
- spring:setter注入构造器注入注解注入
一.内容回顾 简述对spring框架的了解 优点 高内聚低耦合 可拔插式设计模式 XML配置 便于维护 Sping 可以和各大优秀的框架很好结合(Struts2 springmvc mybatis h ...
- Spring构造器注入循环依赖的解决方案及原理探索
前言 我们都知道Spring解决了Setter注入或者Field注入的循环依赖问题,依靠的是三个Map(earlySingletonObjects.singletonFactories.singlet ...
- setter注入和构造器注入
bean 实例在调用无参构造器创建对象后,就要对 bean 对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类: setter注入 构造器注入 目录 sette ...
最新文章
- 证明利用快慢指针寻找有环单链表中环的起点算法
- 机器学习(MACHINE LEARNING)多属性决策模型
- 在WildFly中运行多个standalone模式的实例
- 存在于实数域的微观粒子5-可分类性
- 13个JavaScript单行式代码
- vue中的uri_浅谈vue-resource和vue-axios的区别
- openstack网络指南_性格内向的战术网络指南
- java开发过程中几种常用算法
- python程序设计基础第三版_Python程序设计(第三版)PPT及源码
- php和ajax实现聊天功能,怎么在PHP项目中使用jquery与ajax实现一个即时聊天功能
- 纯C#实现JPEG解码器在超大图片切割中的应用
- bzoj 2827 千山鸟飞绝 平衡树
- 深挖ThreadLocal
- gerrit 描述信息批量导入导出
- JAVAEE大项目(作业提交/管理系统)
- MVC详解:mvc是什么?为什么要用MVC?MVC工作原理以及MVC优缺点
- ANSYS workbench的模态分析基本原理和步骤
- 闪光灯 flash 问题
- solidworks大型装配体慢卡顿怎么办?来看专业的装配设计与仿真工作站是怎么解决的!
- 中国地图着色C语言实验报告,中国地图着色源代码