具体步骤:

  1. 创建一个maven项目 spring-day1-constructor

  2. 导入依赖

        <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--这里是java 版本号--><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><!--这里是方便版本控制--><spring.version>5.3.1</spring.version><lombok.version>1.18.20</lombok.version><junit.version>4.12</junit.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency></dependencies>
    
  3. 工程项目结构

样例1:

  1. 创建一个Student类

    public class Student {private Long number;private String name;private String school;public void setNumber(Long number) {this.number = number;}public void setName(String name) {this.name = name;}public void setSchool(String school) {this.school = school;}public Student() {}public Student(Long number, String name, String school) {this.number = number;this.name = name;this.school = school;}@Overridepublic String toString() {return "Student{" +"number=" + number +", name='" + name + '\'' +", school='" + school + '\'' +'}';}
    }
  2. 写一个配置文件

    <?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"><!--这里是根据构造函数内的顺序往里面注入--><bean id="s1" class="com.crush.pojo.Student"><constructor-arg index="0" value="12"/><constructor-arg index="1" value="wyh"/><constructor-arg index="2" value="北大"/></bean><!--这里是根据构造函数中的 类型来进行注入 --><bean id="s2" class="com.crush.pojo.Student"><constructor-arg type="java.lang.Long" value="123"/><constructor-arg type="java.lang.String" value="crush"/><constructor-arg type="java.lang.String" value="浙江大学"/></bean>
    </beans>
    
  3. 测试

        @org.junit.Testpublic void testStudent(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");Student student = applicationContext.getBean("s2", Student.class);System.out.println(student);}
    

样例2:

  1. 创建Teacher类

    public class Teacher {private String name;private String school;private List<Student> studentList;private Map<String,String> map;private Set<String> set;public Teacher(String name, String school, List<Student> studentList, Map<String, String> map, Set<String> set) {this.name = name;this.school = school;this.studentList = studentList;this.map = map;this.set = set;}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +", school='" + school + '\'' +", studentList=" + studentList +", map=" + map +", set=" + set +'}';}
    }public class Teacher {private String name;private String school;private List<Student> studentList;private Map<String,String> map;private Set<String> set;public Teacher(String name, String school, List<Student> studentList, Map<String, String> map, Set<String> set) {this.name = name;this.school = school;this.studentList = studentList;this.map = map;this.set = set;}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +", school='" + school + '\'' +", studentList=" + studentList +", map=" + map +", set=" + set +'}';}
    }
    
  2. beans.xml

    <bean id="teacher" class="com.crush.pojo.Teacher"><constructor-arg index="0" value="xxx"/><constructor-arg index="1" value="北京大学"/><constructor-arg index="2" ><list><ref bean="s1"/><ref bean="s2"/></list></constructor-arg><constructor-arg index="3"><map><entry key="k1" value="xiaowang"/></map></constructor-arg><constructor-arg index="4"><set><value>1</value><value>2</value></set></constructor-arg>
    </bean>
    
  3. 测试

        @org.junit.Testpublic void testTeacher(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");Teacher teacher  = applicationContext.getBean("teacher", Teacher.class);System.out.println(teacher);}

Spring单例模式和原型模式

一、单例模式

Spring默认是单例模式的。

以Student的那个样例1 为例。 scope=“singleton” 加上这么一个设置 当然默认也是它。

<bean id="s1" class="com.crush.pojo.Student" scope="singleton"><constructor-arg index="0" value="12"/><constructor-arg index="1" value="wyh"/><constructor-arg index="2" value="北大"/>
</bean>

这个时候我们来进行测试

    @org.junit.Testpublic void testStudent(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");Student student1 = applicationContext.getBean("s1", Student.class);Student student2 = applicationContext.getBean("s1", Student.class);// 并且如果我们对其中一个做了修改 ,其余也会跟着一起被修改// 可以看到我们只修改了一个student1.setSchool("梦中的学校");System.out.println(student1);System.out.println(student2);System.out.println(student1==student2);}

二、原型模式

我们还是以**Student来做例子讲解 ** 注意:我们把原来设置改成了 scope=“prototype” 也就是原型模式

<!--这里是根据构造函数中的 类型来进行注入 -->
<bean id="s2" class="com.crush.pojo.Student" scope="prototype"><constructor-arg type="java.lang.Long" value="123"/><constructor-arg type="java.lang.String" value="crush"/><constructor-arg type="java.lang.String" value="浙江大学"/>
</bean>

接着测试

    @org.junit.Testpublic void testStudent(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");Student student1 = applicationContext.getBean("s2", Student.class);Student student2 = applicationContext.getBean("s2", Student.class);// 并且如果我们对其中一个做了修改 ,其余也会跟着一起被修改// 可以看到我们只修改了一个student1.setSchool("梦中的学校");System.out.println(student1);System.out.println(student2);System.out.println(student1==student2);}

思考 为什么需要依赖注入

为什么我们以前用一个对象 new一下就好了,但用了Spring 之后,反而还需要写

这样一段代码再去获取勒?明明感觉更麻烦啦丫?用这个又有什么样的好处呢?

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student student1 = applicationContext.getBean("s2", Student.class);

这是为什么呢?

如果想要知道为什么?那么就随我一起去看下面这篇文章吧。

用小说的形式讲解Spring-为什么需要依赖注入

参考:
https://blog.csdn.net/hzy38324/article/details/78013136
https://blog.csdn.net/weixin_45821811/article/details/117651505

自言自语

其实复习挺好的。

Spring(3)bean 注入-构造方法注入 那么又为什么需要依赖注入呢?相关推荐

  1. go 依赖注入 哪个好_go与java的依赖注入实现的一些差异

    go语言是一门开源的语言,我这里说开源,并不是指go的编译器等是开源,而是指go在机制上决定了当我们引入一个类库的时候,实质上是引入类库的源码. 纯go实现的类库.模块,基本是无法以编译后二进制的形式 ...

  2. spring boot 菜鸟教程学习:spring是一个超级大工厂能够管理java对象(bean)和他们之间的关系(依赖注入)

    springboot的java对象叫做bean 用一个叫依赖注入的方法来管理bean的依赖关系 说白了 就是bean是节点 依赖注入能够构建节点之间的关系 创建bean的三种方式 如何依赖注入?

  3. Spring基础专题——第三章(反转控制与依赖注入)

    前言:去年到现在一直没有很好的时间完成这个spring基础+源码的博客目标,去年一年比较懒吧,所以今年我希望我的知识可以分享给正在奋斗中的互联网开发人员,以及未来想往架构师上走的道友们我们一起进步,从 ...

  4. 依赖注入及AOP简述(五)——依赖注入的方式 .

    二.依赖注入的应用模式 前面我们了解了依赖注入的基本概念,也对一些依赖注入框架进行了简单的介绍,这一章我们主要来讨论作为开发者如何利用依赖注入框架来实现依赖注入的设计思想. 1.     依赖注入的方 ...

  5. php反射机制与依赖注入,利用反射机制实现基本的依赖注入

    ReflectionClass实现了 Reflector 接口,使得我们可以使用该类查看另一个类的相关信息.所谓的反射,大概的意思就是将一个类的相关信息给反射 (映射.反映) 出来,转载. 无依赖的情 ...

  6. phalapi可以依赖注入么_phalapi-进阶篇2(DI依赖注入和单例模式)

    phalapi-进阶篇2(DI依赖注入和单例模式) 前言 先在这里感谢phalapi框架创始人@dogstar,为我们提供了这样一个优秀的开源框架. 离上一次更新过去了快两周,在其中编写了一个关于DB ...

  7. laravel mysql注入_详解 Laravel 中的依赖注入和 IoC

    Laravel 作为开发者,我们一直在尝试通过使用设计模式和尝试新的健壮型框架来寻找新的方式来编写设计良好且健壮的代码.在本篇文章中,我们将通过 Laravel 的 IoC 组件探索依赖注入设计模式, ...

  8. laravel mysql注入_laravel中如何利用反射实现依赖注入

    依赖注入 在一个类中经常会依赖于其他的对象,先看一下经典的写法 class Foo { public $bar; public function __construct() { $this->b ...

  9. java中依赖注入_关于Java:什么是依赖注入?

    本问题已经有最佳答案,请猛点这里访问. Possible Duplicate: What is Inversion of Control? 我真的很困惑依赖注入的概念. 我对软件领域非常陌生,我对下面 ...

  10. 五篇教你掌握spring之三:详解Spring的bean以及注解开发

    详解Spring的bean以及注解开发 各种复杂类型的依赖注入 我们采用一个类的大杂烩的形式,新建一个Student package com.lwh.pojo;import java.util.*;p ...

最新文章

  1. BS-XX-007基于JSP实现户籍管理系统
  2. andorid手机电脑操作
  3. python赋值方式
  4. raid卡的结构示意图
  5. c语言空白字符的aci,c语言的保留字符有32个是那些啊???代表什么于是啊??...
  6. php网站mysql数据库导入工具_phpstudy通过phpMyAdmin导入mysql数据库方法
  7. android开发那些事儿(二)--Drawable资源
  8. 【python】Tkinter窗口可视化(二)
  9. ASP.net:URL重写实现IHttpHandler接口
  10. coreldraw怎么打印荣誉证书_使用Word 2010制作并打印荣誉证书的方法
  11. android手机录屏工具,安卓手机录屏软件哪个好用
  12. USGS官网批量下载卫星数据方法
  13. CSS实现图片层闪光效果
  14. 微信公众号模版消息推送
  15. 英语3500词(16/20)trade主题(2022.1.28)
  16. Swift 版本很好的卡片切换效果基于ZLSwipeableView(类似于[陌陌点点][探探])
  17. Mybatis学习笔记(尚硅谷版整理)
  18. python彩色七段数码管绘制
  19. 微信公众号之渲染静态模板
  20. googleMap 谷歌地图

热门文章

  1. SAP实施要重视用户体验
  2. SAP 动态设置 GUI STATUS 灰色不可用 或者隐藏
  3. 百度智能云一周连签三个新基建大单,“非对称竞争”优势凸显?
  4. 在线音频“三国争霸”,谁能率先登陆资本市场?
  5. java 持续集成工具_Jenkins集成式项目控件下载
  6. 河北大学计算机二级报名时间,河北大学关于2018年下半年学位授予工作安排的通知...
  7. CTF-不一样的凯撒密码
  8. python 减少可调用对象的参数个数
  9. 30段极简Python代码:这些小技巧你都Get了么?
  10. 直接插入排序比较次数_插入排序(C++)