[SSM]Spring6整合JUnit5与集成MyBatis3.5

news2024/9/24 1:21:22

目录

十七、Spring6整合JUnit5

17.1Spring对JUnit4的支持

17.2Spring对JUnit5的支持

十八、Spring6集成MyBatis3.5

18.1实现步骤

18.2具体实现

18.3spring配置文件的import


十七、Spring6整合JUnit5

17.1Spring对JUnit4的支持

  • 准备工作:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.3</version>
    </dependency>
    <!--spring对junit4支持的依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <!--这个版本spring6,既支持JUnit4又支持JUnit5-->
        <version>6.0.3</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

声明Bean

package com.hhb.bean;
​
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
​
@Component
public class User {
    @Value("张三")
    private String name;
​
    public User(String name) {
        this.name = name;
    }
​
    public User() {
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

spring.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
​
    <context:component-scan base-package="com.hhb.bean"/>
</beans>

单元测试

package com.hhb.test;
​
import com.hhb.bean.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
​
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJUnit4Test {
    @Autowired
    private User user;
​
    @Test
    public void testUser() {
        System.out.println(user.getName());
    }
​
    @Test
    public void testUser1() {
        System.out.println(user.getName());
    }
​
    @Test
    public void testUser2() {
        System.out.println(user.getName());
    }
}

 

  • Spring提供的方便主要是这几个注解:

    • @RunWith(SpringJUnit4ClassRunner.class)

    • @ContextConfiguration("classpath:spring.xml")

  • 在单元测试类上使用这两个注解之后,在单元测试类中的属性上可以使用@Autowired,比较方便。

17.2Spring对JUnit5的支持

  • 引入JUnit5的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.3</version>
    </dependency>
    <!--spring对junit4支持的依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <!--这个版本spring6,既支持JUnit4又支持JUnit5-->
        <version>6.0.3</version>
    </dependency>
​
    <!--junit5依赖-->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

单元测试

package com.hhb.test;
​
import com.hhb.bean.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
​
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJUnit5Test {
​
    @Autowired
    private User user;
​
    @Test
    public void testUser(){
        System.out.println(user.getName());
    }
​
    @Test
    public void testUser2(){
        System.out.println(user.getName());
    }
}
  • 在JUnit5当中,可以使用Spring提供的以下两个注解,标注到单元测试类上,这样类当中就可以使用@Autowired注解了。

    • ExtendWith(SpringExtension.class)

    • ContextConfiguration("classpath:spring.xml")

十八、Spring6集成MyBatis3.5

18.1实现步骤

  • 第一步:准备数据库表

    • 使用t_act表(账户表)

  • 第二步:引入依赖

    • spring-context

    • spring-jdbc

    • mysql驱动

    • mybatis

    • mybatis-spring:mybatis提供的与spring框架集成的依赖

    • 德鲁伊连接池

    • junit

  • 第三步:基于三层架构实现,提前准备好所有的包

    • com.hhb.bank.mapper

    • com.hhb.bank.service

    • com.hhb.bank.service.impl

    • com.hhb.bank.pojo

  • 第四步:编写pojo

    • Account,属性私有化,提供公开的setter、getter和toString。

  • 第五步:编写mapper接口

    • AccountMapper接口,定义方法

  • 第六步:编写mapper配置文件

    • 在配置文件中配置命名空间,以及每一个方法对应的sql。

  • 第七步:编写service接口和service接口实现类

    • AccountService

    • AccountServiceImpl

  • 第八步:编写jdbc.properties配置文件

    • 数据库连接池相关信息

  • 第九步:编写mybatis-config.xml配置文件

    • 该文件可以没有,大部分的配置可以转移到spring配置文件中。

    • 如果遇到mybatis相关的系统级配置,还是需要这个文件。

  • 第十步:编写spring.xml配置文件

    • 组件扫描

    • 引入外部的属性文件

    • 数据源

    • SqlSessionFactoryBean配置

      • 注入mybatis核心配置文件路径

      • 指定别名包

      • 注入数据源

    • Mapper扫描配置器

      • 指定扫描的包

    • 事务管理器DataSourceTransactionManager

      • 注入数据源

    • 启用事务注解

      • 注入事务管理器

  • 第十一步:编写测试程序,并添加事务,进行测试。

18.2具体实现

  • 第一步:准备数据库表

  • 第二步:引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>org.example</groupId>
    <artifactId>spring016-sm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
​

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.0.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.13</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
​
    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
​
</project>

 

  • 第三步:基于三层架构实现,所以提前创建好所有的包

  • 第四步:编写pojo

 
package com.hhb.bank.pojo;
​
public class Account {
    private String actno;
    private Double balance;
​
    public Account() {
    }
​
    @Override
    public String toString() {
        return "Account{" +
                "actno='" + actno + '\'' +
                ", balance=" + balance +
                '}';
    }
​
    public Account(String actno, Double balance) {
        this.actno = actno;
        this.balance = balance;
    }
​
    public String getActno() {
        return actno;
    }
​
    public void setActno(String actno) {
        this.actno = actno;
    }
​
    public Double getBalance() {
        return balance;
    }
​
    public void setBalance(Double balance) {
        this.balance = balance;
    }
}

 

  • 第五步:编写mapper接口

package com.hhb.bank.mapper;
​
import com.hhb.bank.pojo.Account;
​
import java.util.List;
​
//这就是DAO
public interface AccountMapper {//该接口的实现类不需要写,是mybatis通过动态代理机制生成的实现类
​
    //新增账户
    int insert(Account account);
​
    //根据账号删除账户
    int deleteByActno(String actno);
​
    //修改账户
    int update(Account account);
​
    //根据账号查询账户
    Account selectByActno(String actno);
​
    //查询所有账户
    List<Account> selectAll();
}
  • 第六步:编写mapper配置文件,如果接⼝叫做AccountMapper,配置⽂件必须是AccountMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hhb.bank.mapper.AccountMapper">
​
    <insert id="insert">
        insert into t_act
        values (#{actno}, #{balance})
    </insert>
​
    <delete id="deleteByActno">
        delete
        from t_act
        where actno = #{actno}
    </delete>
​
    <update id="update">
        update t_act
        set balance = #{balance}
        where actno = #{actno}
    </update>
​
    <select id="selectByActno" resultType="Account">
        select *
        from t_act
        where actno = #{actno}
    </select>
​
    <select id="selectAll" resultType="Account">
        select *
        from t_act
    </select>
​
</mapper>
  • 第七步:编写service接口和service接口实现类,注意编写的service实现类纳入IoC容器管理

package com.hhb.bank.service;
​
import com.hhb.bank.pojo.Account;
​
import java.util.List;
​
public interface AccountService {
    //开户
    int save(Account act);
​
    //销户
    int deleteByActno(String actno);
​
    //修改账户
    int modify(Account act);
​
    //查询账户
    Account getByActno(String actno);
​
    //获取所有账户
    List<Account> getAll();
​
    //转账
    void transfer(String fromActno, String toActno, double money);
​
}
package com.hhb.bank.service.impl;
​
import com.hhb.bank.mapper.AccountMapper;
import com.hhb.bank.pojo.Account;
import com.hhb.bank.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
​
import java.util.List;
​
@Transactional
@Service("accountService")
public class AccountServiceImpl implements AccountService {
​
    @Autowired
    private AccountMapper accountMapper;
​
    @Override
    public int save(Account act) {
        return accountMapper.insert(act);
    }
​
    @Override
    public int deleteByActno(String actno) {
        return accountMapper.deleteByActno(actno);
    }
​
    @Override
    public int modify(Account account) {
        return accountMapper.update(account);
    }
​
    @Override
    public Account getByActno(String actno) {
        return accountMapper.selectByActno(actno);
    }
​
    @Override
    public List<Account> getAll() {
        return accountMapper.selectAll();
    }
​
    @Override
    public void transfer(String fromActno, String toActno, double money) {
        Account fromAct = accountMapper.selectByActno(fromActno);
        if (fromAct.getBalance() < money) {
            throw new RuntimeException("余额不足");
        }
        Account toAct = accountMapper.selectByActno(toActno);
        fromAct.setBalance(fromAct.getBalance() - money);
        toAct.setBalance(toAct.getBalance() + money);
        int count = accountMapper.update(fromAct);
        /*String s = null;
        s.toString();*/
​
        count += accountMapper.update(toAct);
        if (count != 2) {
            throw new RuntimeException("转账失败");
        }
    }
}
  • 第八步:编写jdbc.properties配置文件,放在类的根路径下

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring6
jdbc.username=root
jdbc.password=030522
  • 第九步:编写mybatis-config.xml配置文件,放在类的根路径下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--帮助我们打印mybatis的日志信息。sql语句等。-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>
  • 第十步:编写spring.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"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
​
    <!--组件扫描-->
    <!--<context:component-scan base-package="com.hhb.bank"/>-->
    <!--在spring的核心配置文件中引入子spring配置文件-->
    <import resource="common.xml"/>
​
    <!--引入外部的属性配置文件-->
    <context:property-placeholder location="jdbc.properties"/>
​
    <!--数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
​
    <!--配置SqlSessionFactoryBean-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定mybatis核心配置文件-->
        <property name="configLocation" value="mybatis-config.xml"/>
        <!--指定别名-->
        <property name="typeAliasesPackage" value="com.hhb.bank.pojo"/>
    </bean>
​
    <!--Mapper扫描配置器,主要扫描Mapper接口,生成代理类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hhb.bank.mapper"/>
    </bean>
​
    <!--事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
​
    <!--启用事务注解-->
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>
  • 第十一步:编写测试程序,并添加事务,进行测试

package com.hhb.bank.test;
​
import com.hhb.bank.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
public class BankSMTest {
   @Test
    public void testSM(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        try {
            accountService.transfer("act001", "act002", 10000);
            System.out.println("转账成功");
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

18.3spring配置文件的import

  • spring配置文件有多个,并且可以在spring的核心配置文件中使用import进行引入,我们可以将组件扫描单独定义到一个配置文件中,如下:

common.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
​
    <!--组件扫描-->
    <context:component-scan base-package="com.hhb.bank"/>
</beans>
  • 在核心配置文件中引入:

<import resource="common.xml"/>
  • 注意:在实际开发中,service单独配置到一个文件中,dao单独配置到一个文件中,然后在核心配置文件中引入,养成好习惯。

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

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

相关文章

python——案例四:判断字符串中的元素组成

案例四&#xff1a;判断字符串中的元素组成str"Hello World! 666" print(str.isalnum()) #判读所有的字符都是数字或者是字母 print(str.isalpha()) #判读所有的字符都是字母 print(str.isdigit()) #判读所有的字符都是数字 print(str.islower()) #判读所有的字符都是…

从 0 到 1!得物如何打造通用大模型训练和推理平台

1.背景 近期&#xff0c;GPT 大模型的发布给自然语言处理&#xff08;NLP&#xff09;领域带来了令人震撼的体验。随着这一事件的发生&#xff0c;一系列开源大模型也迅速崛起。依据一些评估机构的评估&#xff0c;这些开源模型大模型的表现也相当不错。一些大模型的评测情况可…

【Linux取经路】进度条小程序

文章目录 一、预备知识1.1 回车换行1.2 缓冲区 二、倒计时2.1 注意事项 三、进度条3.1 源代码3.2 代码分析3.2 实际使用场景 一、预备知识 1.1 回车换行 一般意义上的回车换行是两个独立的独立的动作&#xff0c;而C语言中的\n则同时完成了回车和换行的工作。回车是将光标移动…

接口压测实践——压力测试常见参数解释说明

使用场景​ 对指定接口进行性能测试时&#xff0c;一些常见参数解释说明。 一键并发​ 可以通过下载最新版的 Apipost 客户端实现单接口的高性能一键并发压测&#xff0c;如下图所示 注意&#xff1a;请勿设置太大的并发量或者循环次数&#xff0c;这有可能导致直接将被压服…

实时协作:团队效率倍增的关键

实时协作是指团队在当前时刻共同完成项目的能力。无论是否使用技术&#xff0c;都能实现这一点。然而&#xff0c;随着远程工作的盛行&#xff0c;安全的协作工具被用来让团队成员在项目和一般业务之间保持联系和同步。 传统协作与实时协作的区别 两种类型的协作最明显的区别…

JPEG有损图像压缩编码器(附源码)

概述 一个基本由自己实现的JPEG有损图像压缩编码器&#xff0c;基于JFIF&#xff08;JPEG文件交换格式&#xff09;标准&#xff1a; 色彩空间转换&#xff08;RGB to YUV&#xff09;色度抽样&#xff08;采样因子4:2:0&#xff09;MCU分块&#xff08;16x16的最小编码单元&…

抄写Linux源码(Day1:获取并运行 Linux0.11)

Day1&#xff1a;获取并运行 Linux0.11 参考资料&#xff1a;https://zhuanlan.zhihu.com/p/438577225 这是我参考的一个别人写的 Linux0.11 解读&#xff1a;https://github.com/dibingfa/flash-linux0.11-talk 我获取 Linux-0.11 源码的链接&#xff1a;https://github.com/…

最适合新手的Java项目/SpringBoot+SSM项目《苍穹外卖》/项目实战、笔记(超详细、新手)[持续更新……]

小知识 软件设计中提到的UI设计中的UI是什么意思&#xff1f; 在软件设计中&#xff0c;UI设计中的UI是User Interface的简称&#xff0c;即用户界面。UI设计是指对软件的人机交互、操作逻辑、界面美观的整体设计。好的UI设计可以让软件变得有个性有品位&#xff0c;同时让操作…

实例讲解:通过三个案例搞懂tcp的那些冷门知识

最近在做数据库相关的事情&#xff0c;碰到了很多TCP相关的问题&#xff0c;新的场景新的挑战&#xff0c;有很多之前并没有掌握透彻的点&#xff0c;大大开了一把眼界&#xff0c;选了几个案例分享一下。 案例一&#xff1a;TCP中并不是所有的RST都有效 背景知识 在TCP协议…

ChatGPT+知乎,20分钟超越专业大V的调教方法

AI技术正在迅速发展&#xff0c;渗透到我们的生活中&#xff0c;尤其在内容营销领域。 AI算法帮助我们生成文本、优化搜索引擎排名&#xff0c;提升用户体验等&#xff0c;这些创新正在塑造时代的前进方向&#xff0c;AI也将引领未来十年的变革。对于每个创业者、内容创作者和…

什么是MES,什么是WMS,MES与WMS有什么区别?

什么是MES&#xff1f;什么是WMS&#xff1f;以及MES&#xff08;制造执行系统&#xff09;与WMS&#xff08;仓库管理系统&#xff09;的区别&#xff0c;下面分为三块跟大家详细讲解。 一、什么是MES&#xff1f; 1、概念&#xff1a; MES&#xff08;英文全称&#xff1a…

Socket本质、实战演示两个进程建立TCP连接通信的过程

文章目录 Socket是什么引入面试题, 使你更深刻的理解四元组 Socket网络通信大体流程实战演示TCP连接建立过程需要用到的linux 查看网络的一些命令测试的程序一些准备工作启动服务端, 并没有调用accept启动客户端开启服务accept Socket是什么 通俗来说,Socket是套接字,是一种编…

文件IO练习

一、用read函数完成文件大小计算 #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argc, const char *argv[]) {int fd open("./1.tx…

子组件因多次监听导致重复调用接口问题

问题原因&#xff1a; 有多个tab切换页面&#xff0c;因为内容一致&#xff0c;写了一个公共组件&#xff0c;这个组件我每次监听当前tab点击的index值&#xff0c;因为有3个tab&#xff0c;就会监听三次&#xff0c;所以每个tab里对应的接口就会相应的调用3次接口&#xff0c…

分布式ID性能评测:CosId VS 美团 Leaf

环境 MacBook Pro (M1)JDK 17JMH 1.36运行在本机 Docker 内的 mariadb:10.6.4 运行 CosId SegmentChainId 模式&#xff0c;基准测试代码&#xff1a; Benchmarkpublic long generate() {return segmentChainId.generate();}Leaf 基准测试代码&#xff1a; Benchmarkpublic l…

如何进行软件回归测试

什么是软件回归测试&#xff0c;如何进行回归测试&#xff0c;进行回归测试时有哪些常用的方法&#xff1f; 回归测试是指修改了旧代码后&#xff0c;重新进行测试以确认修改没有引入新的错误或导致其他代码产生错误的一种测试方法。回归测试是指重复以前的全部或部分的相同功能…

Java版本spring cloud + spring boot企业电子招投标系统源代码

&#xfeff;项目说明 随着公司的快速发展&#xff0c;企业人员和经营规模不断壮大&#xff0c;公司对内部招采管理的提升提出了更高的要求。在企业里建立一个公平、公开、公正的采购环境&#xff0c;最大限度控制采购成本至关重要。符合国家电子招投标法律法规及相关规范&…

07 定时器处理非活动连接(上)

07 定时器处理非活动连接&#xff08;上&#xff09; 基础知识 非活跃&#xff0c;是指客户端&#xff08;这里是浏览器&#xff09;与服务器端建立连接后&#xff0c;长时间不交换数据&#xff0c;一直占用服务器端的文件描述符&#xff0c;导致连接资源的浪费。 定时事件&a…

kotlin 编写一个简单的天气预报app(三)broadcast换成eventbus

使用eventbus替换broadcast 将从Broadcast切换到EventBus有以下几个好处&#xff1a; 解耦性&#xff1a;通过使用EventBus&#xff0c;您可以实现组件之间的解耦。传统的Broadcast机制需要发送方和接收方明确知道对方的存在&#xff0c;并且需要在代码中设置Intent过滤器和广…