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

Spring-1-透彻理解Spring XML的Bean创建--IOC

学习目标

上一篇文章我们介绍了什么是Spring,以及Spring的一些核心概念,并且快速快发一个Spring项目,实现IOC和DI,今天具体来讲解IOC

能够说出IOC的基础配置和Bean作用域

了解Bean的生命周期

能够说出Bean的实例化方式

一、Bean的基础配置

问题导入

问题1:在<bean>标签上如何配置别名?

问题2:Bean的默认作用范围是什么?如何修改?

1 Bean基础配置【重点】

配置说明

2 Bean别名配置

配置说明

注意事项:

获取bean无论是通过id还是name获取,如果无法获取到,将抛出异常NoSuchBeanDefinitionException
NoSuchBeanDefinitionException: No bean named 'studentDaoImpl' available

代码演示

【第0步】创建项目名称为10_2_IOC_Bean的maven项目

【第一步】导入Spring坐标

  <dependencies><!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.15</version></dependency><!-- 导入junit的测试包 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency></dependencies>

【第二步】导入Student实体类

@Data
@ToString
@AllArgsConstructor
public class Student {private String name;private String address;private Integer age;private Integer status;
}

【第三步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类

package com.zbbmeta.dao;public interface StudentDao {/*** 添加学生*/void save();
}
package com.zbbmeta.dao.impl;import com.zbbmeta.dao.StudentDao;public class StudentDaoImpl implements StudentDao {@Overridepublic void save() {System.out.println("DAO: 添加学生信息到数据库...");}
}
  • StudentService接口和StudentServiceImpl实现类

package com.zbbmeta.service;public interface StudentService {/*** 添加学生*/void save();
}
package com.zbbmeta.service.impl;import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.dao.impl.StudentDaoImpl;
import com.zbbmeta.service.StudentService;public class StudentServiceImpl implements StudentService {//创建成员对象private StudentDao studentDao = new StudentDaoImpl();@Overridepublic void save() {}
}

【第四步】创建Spring配置文件在resources目录下,配置对应类作为Spring管理的bean对象

  • 定义application.xml配置文件并配置StudentDaoImpl

<?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标签详细的属性应用--><!--name属性:可以设置多个别名,别名之间使用逗号,空格,分号等分隔--><bean name="studentDao2,abc studentDao3" class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao"></bean></beans>

注意事项:bean定义时id属性和name中名称不能有重复的在同一个上下文中(IOC容器中)不能重复

【第四步】根据容器别名获取Bean对象

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class NameApplication {public static void main(String[] args) {/*** 从IOC容器里面根据别名获取对象执行*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="abc"对象StudentDao studentDao = (StudentDao) ac.getBean("abc");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}

打印结果

3 Bean作用范围配置【重点】

配置说明

扩展: scope的取值不仅仅只有singleton和prototype,还有request、session、application、 websocket ,表示创建出的对象放置在web容器(tomcat)对应的位置。比如:request表示保存到request域中。

代码演示

在application.xml中配置prototype格式

  • 定义application.xml配置文件并配置StudentDaoImpl

<?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标签详细的属性应用--><!--scope属性:定义bean的作用范围,一共有5个singleton: 设置单例创建对象(推荐,也是默认值),好处:节省资源prototype: 设置多例创建对象,每次从IOC容器获取的时候都会创建对象,获取多次创建多次。request: 在web开发环境中,IOC容器会将对象放到request请求域中,对象存活的范围在一次请求内。请求开始创建对象,请求结束销毁对象session: 在web开发环境中,IOC容器会将对象放到session会话域中,对象存活的范围在一次会话内。会话开始开始创建对象,会话销毁对象销毁。global-session: 是多台服务器共享同一个会话存储的数据。--><bean class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao4" scope="prototype"></bean></beans>

根据容器别名获取Bean对象

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ScopeApplication {public static void main(String[] args) {/*** Bean的作用域范围演示*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象System.out.println("=========singleton(单例)模式=========");StudentDao studentDao = (StudentDao) ac.getBean("studentDao");StudentDao studentDao1 = (StudentDao) ac.getBean("studentDao");System.out.println("studentDao = " + studentDao);System.out.println("studentDao1 = " + studentDao1);System.out.println("=========prototype模式=========");StudentDao studentDao2 = (StudentDao) ac.getBean("studentDao4");StudentDao studentDao3 = (StudentDao) ac.getBean("studentDao4");System.out.println("studentDao2 = " + studentDao2);System.out.println("studentDao3 = " + studentDao3);//4.关闭容器ac.close();}
}

打印结果

注意:在我们的实际开发当中,绝大部分的Bean是单例的,也就是说绝大部分Bean不需要配置scope属性

二、Bean的实例化

思考:Bean的实例化方式有几种?

2 实例化Bean的三种方式

2.1 构造方法方式【重点】

  • BookDaoImpl实现类

public class StudentDaoImpl implements StudentDao {public StudentDaoImpl() {System.out.println("Student dao constructor is running ....");}@Overridepublic void save() {System.out.println("DAO: 添加学生信息到数据库...");}
}
  • application.xml配置

    <!--目标:讲解bean创建方式--><!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建--><bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>
  • AppForInstanceBook测试类

public class OneApplication {public static void main(String[] args) {/*** 无参构造方式创建Bean*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}
  • 运行结果

注意:无参构造方法如果不存在,将抛出异常BeanCreationException

2.2 静态工厂方式

  • StudentDaoFactory工厂类

package com.zbbmeta.factory;import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.dao.impl.StudentDaoImpl;public class StudentDaoFactory {
//    静态工厂创建对象public static StudentDao getStudentDao(){System.out.println("Student static factory setup....");return new StudentDaoImpl();}
}
  • applicationContext.xml配置

<?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创建方式--><!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建-->
<!--    <bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>--><!--创建StudentDaoImpl对象方式2:调用静态工厂方法创建对象加入IOC容器class="com.zbbmeta.factory.StudentDaoFactory" 设置工厂类全名factory-method="getStudentDao" 调用工厂的静态方法
--><bean class="com.zbbmeta.factory.StudentDaoFactory" factory-method="getStudentDao" id="studentDao2"></bean></beans>

注意:测试前最好把之前使用Bean标签创建的对象进行注释

  • TwoApplication测试类

public class TwoApplication {public static void main(String[] args) {/*** 无参构造方式创建Bean*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao2");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}
  • 运行结果

2.3 实例工厂方式

  • UserDao接口和UserDaoImpl实现类

    //利用实例方法创建StudentDao对象public StudentDao getStudentDao2(){System.out.println("调用了实例工厂方法");return new StudentDaoImpl();}
  • StudentDaoFactory工厂类添加方法

//实例工厂创建对象
public class UserDaoFactory {public UserDao getUserDao(){return new UserDaoImpl();}
}
  • applicationContext.xml配置

<?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创建方式--><!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建-->
<!--    <bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>--><!--创建StudentDaoImpl对象方式2:调用静态工厂方法创建对象加入IOC容器class="com.zbbmeta.factory.StudentDaoFactory" 设置工厂类全名factory-method="getStudentDao" 调用工厂的静态方法
-->
<!--    <bean class="com.zbbmeta.factory.StudentDaoFactory" factory-method="getStudentDao" id="studentDao2"></bean>--><!--创建BookDaoImpl对象方式3:调用实例工厂方法创建对象加入IOC容器class="com.itheima.factory.BookDaoFactory" 设置工厂类全名factory-method="getBookDao" 调用工厂的静态方法
--><!--第一步:创建工厂StudentDaoFactory对象--><bean class="com.zbbmeta.factory.StudentDaoFactory" id="studentDaoFactory"></bean><!--第一步:调用工厂对象的getStudentDao2()实例方法创建StudentDaoImpl对象加入IOC容器factory-bean="studentDaoFactory" 获取IOC容器中指定id值的对象factory-method="getStudentDao2" 如果配置了factory-bean,那么这里设置的就是实例方法名--><bean factory-bean="studentDaoFactory" factory-method="getStudentDao2" id="studentDao3"></bean></beans>
  • ThreeApplication测试类

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ThreeApplication {public static void main(String[] args) {/*** 无参构造方式创建Bean*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao3");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}
  • 运行结果

三、Bean的生命周期【了解】

问题导入

问题1:多例的Bean能够配置并执行销毁的方法?

问题2:如何做才执行Bean销毁的方法?

1 生命周期相关概念介绍

  • 生命周期:从创建到消亡的完整过程

  • bean生命周期:bean从创建到销毁的整体过程

  • bean生命周期控制:在bean创建后到销毁前做一些事情

1.1生命周期过程

  • 初始化容器
    • 创建对象(内存分配)

    • 执行构造方法

    • 执行属性注入(set操作)

    • 执行bean初始化方法

  • 使用bean
    • 执行业务操作

  • 关闭/销毁容器
    • 执行bean销毁方法

2 代码演示

2.1 Bean生命周期控制

【第0步】创建项目名称为10_4_IOC_BeanLifeCycle的maven项目

【第一步】导入Spring坐标

  <dependencies><!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.15</version></dependency><!-- 导入junit的测试包 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency></dependencies>

【第二步】导入Student实体类

@Data
@ToString
@AllArgsConstructor
public class Student {private String name;private String address;private Integer age;private Integer status;
}

【第三步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类

package com.zbbmeta.dao;public interface StudentDao {/*** 添加学生*/void save();
}
package com.zbbmeta.dao.impl;import com.zbbmeta.dao.StudentDao;public class StudentDaoImpl implements StudentDao {public StudentDaoImpl(){System.out.println("Student Dao 的无参构造");}@Overridepublic void save() {System.out.println("DAO: 添加学生信息到数据库...");}public void init(){System.out.println("Student Dao 的初始化方法");}public void destroy(){System.out.println("Student Dao 的销毁方法");}
}

【第四步】创建Spring配置文件在resources目录下,配置对应类作为Spring管理的bean对象

  • 定义application.xml配置文件并配置StudentDaoImpl

<?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"><!--目标:创建StudentDaoImpl对象:设置生命周期方法init-method="init" 在对象创建后立即调用初始化方法destroy-method="destroy":在容器执行销毁前立即调用销毁的方法注意:只有单例对象才会运行销毁生命周期方法
--><bean class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao" init-method="init" destroy-method="destroy"></bean>
</beans>

【第四步】根据容器别名获取Bean对象

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class LifeCycleApplication {public static void main(String[] args) {//1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}

打印结果

3 Bean销毁时机

  • 容器关闭前触发bean的销毁

  • 关闭容器方式:
    • 手工关闭容器 调用容器的close()操作

    • 注册关闭钩子(类似于注册一个事件),在虚拟机退出前先关闭容器再退出虚拟机 调用容器的registerShutdownHook()操作

public class LifeCycleApplication {public static void main(String[] args) {//1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao");//3.执行对象方法studentDao.save();//4.关闭容器
//            ac.close();//注册关闭钩子函数,在虚拟机退出之前回调此函数,关闭容器ac.registerShutdownHook();}
}

相关文章:

Spring-1-透彻理解Spring XML的Bean创建--IOC

学习目标 上一篇文章我们介绍了什么是Spring,以及Spring的一些核心概念&#xff0c;并且快速快发一个Spring项目&#xff0c;实现IOC和DI&#xff0c;今天具体来讲解IOC 能够说出IOC的基础配置和Bean作用域 了解Bean的生命周期 能够说出Bean的实例化方式 一、Bean的基础配置 …...

【JAVA】类和对象

作者主页&#xff1a;paper jie的博客 本文作者&#xff1a;大家好&#xff0c;我是paper jie&#xff0c;感谢你阅读本文&#xff0c;欢迎一建三连哦。 本文录入于《JAVASE语法系列》专栏&#xff0c;本专栏是针对于大学生&#xff0c;编程小白精心打造的。笔者用重金(时间和精…...

jenkins准备

回到目录 jenkins是一个开源的、提供友好操作界面的持续集成(CI)工具&#xff0c;主要用于持续、自动的构建/测试软件项目、监控外部任务的运行。Jenkins用Java语言编写&#xff0c;可在Tomcat等流行的servlet容器中运行&#xff0c;也可独立运行。通常与版本管理工具(SCM)、构…...

【Rust】Rust学习

文档&#xff1a;Rust 程序设计语言 - Rust 程序设计语言 简体中文版 (bootcss.com) 墙裂推荐这个文档 第一章入门 入门指南 - Rust 程序设计语言 简体中文版 第二章猜猜看游戏 猜猜看游戏教程 - Rust 程序设计语言 简体中文版 (bootcss.com) // 导入库 use std::io; use s…...

Linux 常用命令之配置环境变量 PATH

PATH是系统环境变量中的一种&#xff0c;同时将一些二进制文件的绝对路径追加进去&#xff0c;则在系统终端中可以发现这些路径下的文件。 一. 环境变量设置 export PATH<二进制文件的绝对路径>:$PATH 以下为结合实际例子的操作 1、临时设置 打开一个终端执行如下命令 e…...

flask-----蓝图

1.引入蓝图 flask都写在一个文件中&#xff0c;项目这样肯定不行&#xff0c;会导致循环导入的问题&#xff0c;分目录&#xff0c;分包&#xff0c;使用蓝图划分目录。 2.使用蓝图 步骤如下&#xff1a; -1 实例化得到一个蓝图对象-order_blueBlueprint(order,__name__,tem…...

学习左耳听风栏目90天——第一天 1-90(学习左耳朵耗子的工匠精神,对技术的热爱)【洞悉技术的本质,享受科技的乐趣】

洞悉技术的本质&#xff0c;享受科技的乐趣 第一篇&#xff0c;我的感受就是 耗叔是一个热爱技术&#xff0c;可以通过代码找到快乐的技术人。 作为it从业者&#xff0c;我们如何可以通过代码找到快乐呢&#xff1f;这是一个问题&#xff1f; 至少目前&#xff0c;我还没有这种…...

后端登录安全的一种思路

PS:作者是小白能接触到的就只会这样写。勿喷。 前提 思路: 结合io流将登录token存储到配置文件中,不将token存储到浏览器端&#xff0c;从而避免盗取。 下面jwt的学习可以参考下这个: JWT --- 入门学习_本郡主是喵的博客-CSDN博客 JWT工具类 Component public class JWTtU…...

【深度学习_TensorFlow】激活函数

写在前面 上篇文章我们了解到感知机使用的阶跃函数和符号函数&#xff0c;它们都是非连续&#xff0c;导数为0的函数&#xff1a; 建议回顾上篇文章&#xff0c;本篇文章将介绍神经网络中的常见激活函数&#xff0c;这些函数都是平滑可导的&#xff0c;适合于梯度下降算法。 写…...

机器学习笔记之优化算法(七)线搜索方法(步长角度;非精确搜索;Wolfe Condition)

机器学习笔记之优化算法——线搜索方法[步长角度&#xff0c;非精确搜索&#xff0c;Wolfe Condition] 引言回顾&#xff1a; Armijo \text{Armijo} Armijo准则及其弊端 Glodstein \text{Glodstein} Glodstein准则及其弊端 Wolfe Condition \text{Wolfe Condition} Wolfe Condi…...

十四.redis哨兵模式

redis哨兵模式 1.概述2.测试3.哨兵模式优缺点 redis哨兵模式基础是主从复制 1.概述 主从切换的技术方法&#xff1a;当主节点服务器宕机后&#xff0c;需要手动把一台从服务器切换为主服务器&#xff0c;这就需要人工干预&#xff0c;费时费力&#xff0c;还会造成一段时间内服…...

采用UWB技术开发的智慧工厂人员定位系统源码【UWB定位基站、卡牌】

UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术&#xff0c;它不采用正弦载波&#xff0c;而是利用纳秒级的非正弦波窄脉冲传输数据&#xff0c;因此其所占的频谱范围很宽。UWB定位系统依托在移动通信&#xff0c;雷达&#xff0c;微波电路&#xff0c;云计算与大数据…...

当你软件测试遇上加密接口,是不是就不能测了?

相信大家在工作中做接口测试的时候&#xff0c;肯定会遇到一个场景&#xff0c;那就是你们的软件&#xff0c;密码是加密存储的。 那么这样的话&#xff0c;我们在执行接口的时候&#xff0c;对于密码的处理就开始头疼了。 所以&#xff0c;本文将使用jmeter这款java开源的接…...

Flink

Flink&#xff08;Apache Flink&#xff09;是一个开源的分布式流处理引擎和批处理框架。它是由 Apache 软件基金会维护的项目&#xff0c;旨在处理大规模数据的实时流式处理和批处理任务。Flink 提供了强大的流处理和批处理功能&#xff0c;具有低延迟、高吞吐量和高容错性&am…...

python入门常用操作

python常用操作 1、ndarry数组的切片2、print用法2.1格式化输出format2.2字符串格式化输出 3、均值滤波函数 1、ndarry数组的切片 例如一个5列的ndarry数组&#xff0c;想要获取第2列和第3列数据&#xff0c;可以用 #&#xff08;1&#xff09;用法1 data[:,1:3]&#xff0c;…...

SpringBoot复习:(21)自定义ImportBeanDefinitionRegistrar

要达到的目的&#xff1a;将某个包下使用了某个自定义注解&#xff08;比如MyClassMapper)的类注册到Spring 容器。 一、自定义注解&#xff1a; package com.example.demo.service;import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy;Rete…...

小黑子—JavaWeb:第五章 - JSP与会话跟踪技术

JavaWeb入门5.0 1. JSP1.1 JSP快速入门1.2 JSP原理1.3 JSP脚本1.3.1 JSP缺点 1.4 EL 表达式1.5 JSTL 标签1.5.1 JSTL 快速入门1.5.1 - I JSTL标签if1.5.1 - II JSTL标签forEach 1.6 MVC模式1.7 三层架构1.8 实现案例1.8.1 环境准备1.8.2 查询所有1.8.3 添加数据1.8.4 修改1.8.4…...

Python - 【socket】 客户端client重连处理简单示例Demo(一)

一. 前言 在Python中&#xff0c;使用socket进行网络通信时&#xff0c;如果连接断开&#xff0c;可以通过以下步骤实现重连处理 二. 示例代码 1. 定义一个函数&#xff0c;用于建立socket连接 import socketdef connect_socket(host, port):while True:try:# 建立socket连…...

Redis 基础

1.定义 Redis 是一个高性能的key-value数据库&#xff0c;key是字符串类型。 2.核心特点&#xff1a; 单进程&#xff1a; Redis的服务器程序采用的是单进程模型来处理客户端的请求。对读写时间的响 应是通过对epoll函数的包装来做到的。 3.数据类型&#xff1a; 键的类型…...

【0805作业】Linux中 AB终端通过两根有名管道进行通信聊天(半双工)

作业一&#xff1a;打开两个终端&#xff0c;要求实现AB进程对话【两根管道】 打开两个终端&#xff0c;要求实现AB进程对话 A进程先发送一句话给B进程&#xff0c;B进程接收后打印B进程再回复一句话给A进程&#xff0c;A进程接收后打印重复1.2步骤&#xff0c;当收到quit后&am…...

基于FPGA的PID算法学习———实现PID比例控制算法

基于FPGA的PID算法学习 前言一、PID算法分析二、PID仿真分析1. PID代码2.PI代码3.P代码4.顶层5.测试文件6.仿真波形 总结 前言 学习内容&#xff1a;参考网站&#xff1a; PID算法控制 PID即&#xff1a;Proportional&#xff08;比例&#xff09;、Integral&#xff08;积分&…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)

概述 在 Swift 开发语言中&#xff0c;各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过&#xff0c;在涉及到多个子类派生于基类进行多态模拟的场景下&#xff0c;…...

解锁数据库简洁之道:FastAPI与SQLModel实战指南

在构建现代Web应用程序时&#xff0c;与数据库的交互无疑是核心环节。虽然传统的数据库操作方式&#xff08;如直接编写SQL语句与psycopg2交互&#xff09;赋予了我们精细的控制权&#xff0c;但在面对日益复杂的业务逻辑和快速迭代的需求时&#xff0c;这种方式的开发效率和可…...

蓝桥杯 2024 15届国赛 A组 儿童节快乐

P10576 [蓝桥杯 2024 国 A] 儿童节快乐 题目描述 五彩斑斓的气球在蓝天下悠然飘荡&#xff0c;轻快的音乐在耳边持续回荡&#xff0c;小朋友们手牵着手一同畅快欢笑。在这样一片安乐祥和的氛围下&#xff0c;六一来了。 今天是六一儿童节&#xff0c;小蓝老师为了让大家在节…...

376. Wiggle Subsequence

376. Wiggle Subsequence 代码 class Solution { public:int wiggleMaxLength(vector<int>& nums) {int n nums.size();int res 1;int prediff 0;int curdiff 0;for(int i 0;i < n-1;i){curdiff nums[i1] - nums[i];if( (prediff > 0 && curdif…...

基于IDIG-GAN的小样本电机轴承故障诊断

目录 🔍 核心问题 一、IDIG-GAN模型原理 1. 整体架构 2. 核心创新点 (1) ​梯度归一化(Gradient Normalization)​​ (2) ​判别器梯度间隙正则化(Discriminator Gradient Gap Regularization)​​ (3) ​自注意力机制(Self-Attention)​​ 3. 完整损失函数 二…...

Python Einops库:深度学习中的张量操作革命

Einops&#xff08;爱因斯坦操作库&#xff09;就像给张量操作戴上了一副"语义眼镜"——让你用人类能理解的方式告诉计算机如何操作多维数组。这个基于爱因斯坦求和约定的库&#xff0c;用类似自然语言的表达式替代了晦涩的API调用&#xff0c;彻底改变了深度学习工程…...

MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)

macos brew国内镜像加速方法 brew install 加速formula.jws.json下载慢加速 &#x1f37a; 最新版brew安装慢到怀疑人生&#xff1f;别怕&#xff0c;教你轻松起飞&#xff01; 最近Homebrew更新至最新版&#xff0c;每次执行 brew 命令时都会自动从官方地址 https://formulae.…...

LangChain 中的文档加载器(Loader)与文本切分器(Splitter)详解《二》

&#x1f9e0; LangChain 中 TextSplitter 的使用详解&#xff1a;从基础到进阶&#xff08;附代码&#xff09; 一、前言 在处理大规模文本数据时&#xff0c;特别是在构建知识库或进行大模型训练与推理时&#xff0c;文本切分&#xff08;Text Splitting&#xff09; 是一个…...

DAY 45 超大力王爱学Python

来自超大力王的友情提示&#xff1a;在用tensordoard的时候一定一定要用绝对位置&#xff0c;例如&#xff1a;tensorboard --logdir"D:\代码\archive (1)\runs\cifar10_mlp_experiment_2" 不然读取不了数据 知识点回顾&#xff1a; tensorboard的发展历史和原理tens…...