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

news2024/9/20 1:25:29

学习目标

上一篇文章我们介绍了什么是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 {
    @Override
    public 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();
    @Override
    public 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 ....");
    }

    @Override
    public 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 的无参构造");
    }

    @Override
    public 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();
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/835731.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

jenkins准备

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

项目中使用非默认字体

项目场景&#xff1a; 由于开发需要&#xff0c;默认的字体不符合开发需求&#xff0c;有时候我们需要引入某种字体到项目中 解决方案&#xff1a; 首先需要下载或引入字体包 然后创建一个 index.css 文件用于声明引入字体名称 font-face {font-family: "YouSheBiao…

用html+javascript打造公文一键排版系统13:增加半角字符和全角字符的相互转换功能

一、实践发现了bug和不足 今天用了公文一键排版系统对几个PDF文件格式的材料进行文字识别后再重新排版&#xff0c;处理效果还是相当不错的&#xff0c;节约了不少的时间。 但是也发现了三个需要改进的地方&#xff1a; &#xff08;一&#xff09;发现了两个bug&#xff1a…

一起学算法(滑动窗口篇)

前言&#xff1a; 对于滑动窗口&#xff0c;有长度固定的窗口&#xff0c;也有长度可变的窗口&#xff0c;一般是基于数组进行求解&#xff0c;对于一个数组中两个相邻的窗口&#xff0c;势必会有一大部分重叠&#xff0c;这部分重叠的内容是不需要重复计算的&#xff0c;所以我…

Nacos适配人大金仓国产数据库

nacos版本2.2.0 人大金仓版本8.6.0 一、相关文件 Nacos官方文档-数据源插件https://nacos.io/zh-cn/docs/v2/plugin/datasource-plugin.html Nacos2.2.0源码https://github.com/alibaba/nacos/archive/refs/tags/2.2.0.zip 人大金仓驱动https://download.csdn.net/download/q…

无人机航测技术有何特点?主要应用在哪些方面?

无人机航测是航空摄影测量的一种&#xff0c;主要面向低空遥感领域&#xff0c;具有成本低、快速高效、适用范围广等特点。目前&#xff0c;无人机航测主要应用于地形测绘、城市数字化建设、工程建设等方面。 无人机航测技术的特点 1、作业成本低 传统的人工测量技术主要利用…

Bigemap如何查看高清影像图

工具 Bigemap gis office地图软件 分享一个可以查看非常高清影像图的软件&#xff0c;平时外出徒步的时候用来查看路线。 首先要去搜索安装bigemap gis office这个软件&#xff0c;打开软件&#xff0c;要提示你去添加地图的。然后去点击选择地图这个按钮&#xff0c;列表中有…

CobaltStirke BOF技术剖析(一)|BOF实现源码级分析

简介 对BOF(Beacon Object File)的支持是在CobaltStrike4.1版本中新引入的功能。BOF文件是由c代码编译而来的可在Beacon进程中动态加载执行的二进制程序。无文件执行与无新进程创建的特性更加符合OPSEC的原则&#xff0c;适用于严苛的终端对抗场景。低开发门槛与便利的内部Bea…

Linux学习之延时计划任务anacontab和锁文件flock

cat /etc/redhat-release看到操作系统的版本是CentOS Linux release 7.6.1810 (Core)&#xff0c;uname -r可以看到内核版本是3.10.0-957.21.3.el7.x86_64 参考的博客有&#xff1a; 1.《Linux anacron命令用法详解》 2.《详解anacron 命令》 3.《Anacron的用法》 4.《shell脚…

ip网络广播系统网络音频解码终端公共广播SV-7101

SV-7101V网络音频终端产品简介 网络广播终端SV-7101V&#xff0c;接收网络音频流&#xff0c;实时解码播放。本设备只有网络广播功能&#xff0c;是一款简单的网络广播终端。提供一路线路输出接功放或有源音箱。 产品特点 ■ 提供固件网络远程升级■ 标准RJ45网络接口&…

Android:自己写一个简单记事本

一、前言&#xff1a;我的app是点击加号跳转到另一个界面 那么我遇到的问题的是点击加号是一个从一个Fragment跳转到另一个Fragment跳转失败。 二、解决方案&#xff1a; //相应控件的监听里面实现跳转FragmentManager fragmentManagergetFragmentManager();fragmentManager.b…

51单片机学习-AT24C02数据存储秒表(定时器扫描按键数码管)

首先编写I2C模块&#xff0c;根据下面的原理图进行位声明&#xff1a; sbit I2C_SCL P2^1; sbit I2C_SDA P2^0;再根据下面的时序结构图编写函数&#xff1a; /*** brief I2C开始* param 无* retval 无*/ void I2C_Start(void) {I2C_SDA 1; I2C_SCL 1; I2C_SDA 0;I2C_S…

HTML+CSS+JavaScript:利用事件委托实现tab栏切换

一、需求 实现tab栏切换 二、代码素材 以下是缺失JS部分的代码&#xff0c;感兴趣的小伙伴可以先自己试着写一写 <!-- JS方法实现tab栏切换 --> <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta …

[java刷算法]牛客—剑指offer链表复习、手写简易正则匹配

&#x1f9db;‍♂️个人主页&#xff1a;杯咖啡&#x1f4a1;进步是今天的活动&#xff0c;明天的保证&#xff01;✨目前正在学习&#xff1a;SSM框架,算法刷题&#x1f449;本文收录专栏 &#xff1a; java刷算法牛客—剑指offer&#x1f64c;牛客网&#xff0c;刷算法过面试…

【传统视觉】C#创建、封装、调用类库

任务 因为实现代码相对简单&#xff0c;然后又没有使用Opencv&#xff0c;所以就直接用C#实现&#xff0c;C#调用。 1.创建类库 1.1新建一个类库 vs2015 > 文件 > 新建 > 项目 using System; using System.Collections.Generic; using System.Linq;namespace Yo…

使用ChatGPT编写技术文档

技术文档对于任何项目都是至关重要的&#xff0c;因为它确保所有利益相关者都在同一层面上&#xff0c;并允许有效的沟通和协作。创建详细而准确的技术文档可能既耗时又具有挑战性&#xff0c;特别是对于那些不熟悉主题或缺乏强大写作技巧的人来说。ChatGPT 是一个强大的人工智…

服务器流量

1.服务器流量分为入流量和出流量 入流量&#xff08;Inbound Traffic&#xff09;是指流向服务器的数据流量&#xff0c;也就是客户端发送到服务器的数据。这些数据可能包括请求信息、文件上传等。 出流量&#xff08;Outbound Traffic&#xff09;是指从服务器流向客户端的数…

[C++] 类与对象(中)类中六个默认成员函数(2)-- 运算符重载 -- 取地址及const取地址操作符重载

1、前言 本篇我们以日期类来展开讲。对于一个日期&#xff0c;我们如何去比大小呢&#xff1f;对年月日依次进行比较可以&#xff0c;但是可以直接比较吗? 我们可以看到&#xff0c;对于自定义类型的日期类直接去比较两个日期的大小是错误的&#xff0c;因此我们需要对运算符赋…

C. Candy Store

Problem - 1798C - Codeforces 思路&#xff1a;要求的最小的标签数量&#xff0c;我们可以先考虑贪心&#xff0c;对于第一个来说它一定会使用一个标签&#xff0c;然后就让后面尽可能多的跟当前这个共用一个标签&#xff0c;如果不行&#xff0c;则在使用一个新的。那么对于共…

Python+Robot Framework实现接口自动化测试

最近在研究PythonRobot Framework的接口自动化&#xff0c;摸清了一些套路&#xff0c;想着总结一下&#xff0c;分享给大家&#xff0c;希望对做自动化的同学有所启发。 主要用到了Python的requests&#xff0c;json&#xff0c;hashlib库&#xff0c;下面以登录和开启文档/目…