当前位置: 首页 > news >正文

Spring/IoC、DI、Bean

最近在学习SpringBoot,但是发现自己对Spring的一些基础知识不太了解,于是决定先学习Spring相关的一些知识再去了解SpringBoot,此类博客相当于是学习笔记,笔记源自黑马程序员2022新版SSM框架教程

文章目录

  • IoC
  • Bean
  • DI
  • 实操案例IoC(xml实现)
    • 1.导入Spring坐标
    • 2.定义Spring管理的类
    • 3.创建Spring配置文件,配置Bean
    • 4.初始化IoC容器,获取Bean
    • 运行结果:
  • 实操案例DI(XML)
    • 2.配置service与dao之间的关系
    • 运行结果
  • Bean的配置
    • 1.Bean的别名
    • 2.Bean的创建模式
  • Bean的实例化方式
    • 无参构造器实例化
    • FactoryBean实例化
      • 1.创建工厂类
      • 2.设置配置文件
      • 测试运行
  • Bean的生命周期
    • 初始化容器
    • 使用Bean
    • 关闭/销毁容器

IoC

Inversion of Control,控制反转

使用对象时,由主动new产生对象转换成外部提供对象,对象创建的控制权交给外部,而这个"外部"在Spring里面被称为IoC容器

Bean

被创建或被管理的对象在IoC容器中统称为Bean

DI

Dependency Injection,依赖注入
在IoC容器中建立Bean和Bean之间的依赖关系的整个过程称为依赖注入

实操案例IoC(xml实现)

1.导入Spring坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.22</version>
</dependency>

2.定义Spring管理的类

Dao

public interface TestDao {
  void test();
}
public class TestDaoImpl implements TestDao {

  @Override
  public void test() {
    System.out.println("This is an implementation of TestDao");
  }
}

Service

public interface TestService {
  void test();
}
public class TestServiceImpl implements TestService {

  TestDao dao;
  @Override
  public void test() {
    System.out.println("This is an implementation of TestService");
    dao.test();
  }

  public void setDao(TestDao dao){
    this.dao = dao;
  }
}

3.创建Spring配置文件,配置Bean

创建文件

配置Bean

<?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,class路径-->
  <bean id="dao" class="com.Learning1.dao.impl.TestDaoImpl"/>
  <bean id="service" class="com.Learning1.service.impl.TestServiceImpl"/>
</beans>

4.初始化IoC容器,获取Bean

public class Test {

  public static void main(String[] args) {
    //加载配置文件得到上下文容器对象
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    //获取资源
    TestService testService = (TestService) ctx.getBean("service");
    testService.test();
  }
}

运行结果:

This is an implementation of TestService
This is an implementation of TestDao

Process finished with exit code 0

实操案例DI(XML)

##1.删除使用new形式创建对象的代码,并设置setter方法

public class TestServiceImpl implements TestService {

  TestDao dao;
  @Override
  public void Test() {
    System.out.println("This is an implementation of TestService");
  }

  private void setDao(TestDao dao){
    this.dao = dao;
  }
}

2.配置service与dao之间的关系

<bean id="service" class="com.Learning1.service.impl.TestServiceImpl">
  <!--name指引用属性的名称,ref指id名-->
  <property name="dao" ref="dao"/>
</bean>

运行结果

This is an implementation of TestService
This is an implementation of TestDao

Process finished with exit code 0

Bean的配置

1.Bean的别名

可以在配置中添加name指定Bean的别名,可以有多个别名,别名之间可用空格,分号,逗号分隔

<bean id="dao" name="testDao testDao1" class="com.Learning1.dao.impl.TestDaoImpl"/>

2.Bean的创建模式

Bean的默认创建模式为单例模式,每次引用同一个类的对象都是同一个对象,以提高效率。
如以下程序

public class Test2 {

  public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    TestService testService1 = (TestService) ctx.getBean("service");
    TestService testService2 = (TestService) ctx.getBean("service");
    System.out.println(testService1);
    System.out.println(testService2);
  }
}

输出结果为

com.Learning1.service.impl.TestServiceImpl@6b09bb57
com.Learning1.service.impl.TestServiceImpl@6b09bb57

Process finished with exit code 0

这个创建模式可以在配置文件中修改,将scope修改为prototype可以使同一个类创建不同的对象

<bean id="service" class="com.Learning1.service.impl.TestServiceImpl" scope="prototype">

再次运行上一个程序

com.Learning1.service.impl.TestServiceImpl@6b09bb57
com.Learning1.service.impl.TestServiceImpl@6536e911

Process finished with exit code 0

Bean的实例化方式

无参构造器实例化

Spring默认会调用Bean的无参构造器实例化Bean,如果没有无参构造器,将会报错

给TestServiceImpl重写一个无参构造器

public class TestServiceImpl implements TestService {

  public TestServiceImpl() {
    System.out.println("Generator is Running....");
  }

  TestDao dao;
  @Override
  public void test() {
    System.out.println("This is an implementation of TestService");
    dao.test();
  }

  public void setDao(TestDao dao){
    this.dao = dao;
  }
}
public class Test3 {

  public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    TestService testService = (TestService) ctx.getBean("service");
  }
}

运行结果

Generator is Running....

Process finished with exit code 0

FactoryBean实例化

通过工厂的方式来实例化Bean也是比较实用的方式

1.创建工厂类

public class TestDaoFactory implements FactoryBean<TestDao> {

  @Override
  public TestDao getObject() throws Exception {
    return new TestDaoImpl();
  }

  @Override
  public Class<?> getObjectType() {
    return TestDao.class;
  }

  @Override
  public boolean isSingleton() {
    return true;
  }
}

其中isSingleton()方法用来设置是否创建单例

2.设置配置文件

<bean id="dao" name="testDao" class="com.Learning1.factory.TestDaoFactoryBean"/>

测试运行

public class Test4 {

  public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    TestDao dao = (TestDao) ctx.getBean("dao");
    System.out.println(dao);
  }
}

运行结果

com.Learning1.dao.impl.TestDaoImpl@192b07fd

Process finished with exit code 0

Bean的生命周期

初始化容器

1.创建对象(内存分配)
2.执行构造方法
3.执行属性注入(set操作)
4.执行bean初始化方法

使用Bean

执行业务逻辑操作

关闭/销毁容器

执行Bean销毁方法


我叫Kallen,一名快乐且有梦的程序员,欢迎访问我的个人博客

相关文章:

  • 信息管理java毕业设计项目分享【含源码+论文】
  • MASA Stack 第五期社区例会
  • JavaScript 30. JSON
  • CAS:26915-72-0,mPEG-Methacrylate,mPEG-MAC,甲氧基-聚乙二醇-甲基丙烯酸酯
  • 分布式事务最经典的八种解决方案
  • 2.ROS编程学习:话题通信c++
  • 【python】计算偏度和峰度
  • 单片机原理指令系统习题超详细讲解
  • 使用Python,Keras和TensorFlow训练第一个CNN
  • Flutter: Dart 参数,以及 @required 与 required
  • 基于JAVA网上商城系统演示录像计算机毕业设计源码+数据库+lw文档+系统+部署
  • 【复杂网络】关于复杂网络中的动力学系统重构的文献资料整理
  • 应对数据安全典型薄弱点,这家医院“外防内控”筑牢屏障
  • 嵌入式开发:注释C代码的10个技巧
  • Qt5开发从入门到精通——第九篇一节( Qt5 文件及磁盘处理—— 读写文本文件)
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • CSS 提示工具(Tooltip)
  • gcc介绍及安装
  • Git同步原始仓库到Fork仓库中
  • Java超时控制的实现
  • JS 面试题总结
  • Just for fun——迅速写完快速排序
  • leetcode386. Lexicographical Numbers
  • nodejs实现webservice问题总结
  • Python3爬取英雄联盟英雄皮肤大图
  • Python学习之路16-使用API
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • SQLServer之创建显式事务
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • vue 配置sass、scss全局变量
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • vue--为什么data属性必须是一个函数
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 思否第一天
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 自制字幕遮挡器
  • ​linux启动进程的方式
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • (4) PIVOT 和 UPIVOT 的使用
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (C#)获取字符编码的类
  • (二)构建dubbo分布式平台-平台功能导图
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (九十四)函数和二维数组
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (三)模仿学习-Action数据的模仿
  • (顺序)容器的好伴侣 --- 容器适配器
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查