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

Spring基础与创建

目录

前言

Spring基础与核心概念

Spring是什么

1、什么是容器

2、什么是IoC

3、理解SpringIoC

4、DI(依赖注入)

Spring的创建和使用

1、创建Spring项目

1.1、创建一个普通Maven项目

1.2、添加Spring框架支持

1.3、添加启动类和main方法

2、存储Bean对象

2.1、创建Bean对象

2.2、将Bean对象存储到Spring当中

3、获取并使用Bean对象

3.1、先得到Spring上下文对象

3.2、从Spring中取出Bean对象

3.3、使用Bean

Spring更简单的读取和存储对象

1、存储Bean对象

1.1、配置扫描路径

1.2、添加注解存储Bean对象 

2、获取Bean对象

2.1、属性注入

2.2、构造方法注入

2.3、Setter注入

2.4、使用@Resource(另一种注入方法)实现

Bean作用域和生命周期

1、Bean作用域定义

1.1、singleton(单例作用域)

1.2、prototype(原型作用域/多例作用域)

1.3、request(请求作用域)

1.4、session(回话作用域)

1.5、applicatioon(全局作用域)

1.6、设置作用域

2、Spring执行流程和Bean的生命周期

2.1、Spring执行流程

2.2、Bean生命周期


前言

Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)。 Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,并且可以与 Swing等桌面应用程序AP组合。

Spring基础与核心概念

Spring是什么

Spring指的是Spring Framework(Spring框架),它是一个开源框架。Spring支持广泛的应用场景,它可以让java的企业级的应用程序开发起来更简单

Spring是包含了众多工具方法的IoC容器

1、什么是容器

容器是用来容纳某种物品的(基本)装置

2、什么是IoC

Inversion of Control(控制反转)

传统代码

public class App {public static void main(String[] args) {Car car=new Car(30);car.init();}
}public class Car {private Framework framework;public Car(int size){framework=new Framework(size);}public void init(){System.out.println("执行了car init方法");//依赖车身framework.init();}
}public class Framework {private Bottom bottom;public Framework(int size){bottom=new Bottom(size);}public void init(){System.out.println("执行了framework init方法");//依赖底盘bottom.init();}
}public class Bottom {private Tire tire;public Bottom(int size){tire=new Tire(size);}public void init(){System.out.println("执行了buttom init方法");//依赖轮胎tire.init();}
}public class Tire {private int size=20;public Tire(int size){this.size=size;}public void init(){System.out.println("执行了轮胎初始化方法,size:"+this.size);}
}

改进后的代码

public class App {public static void main(String[] args) {Tire tire=new Tire(23);Buttom buttom=new Buttom(tire);FrameWork frameWork=new FrameWork(buttom);Car car=new Car(frameWork);car.init();}
}public class Car {private FrameWork frameWork;public Car(FrameWork frameWork){this.frameWork=frameWork;}public void init(){System.out.println("执行car");//依赖车身frameWork.init();}
}public class FrameWork {public Buttom buttom;public FrameWork(Buttom buttom){this.buttom=buttom;}public void init(){System.out.println("执行framework");//依赖车底buttom.init();}
}public class Buttom {private Tire tire;public Buttom(Tire tire){this.tire=tire;}public void init(){System.out.println("执行buttom");//依赖轮胎tire.init();}
}public class Tire {private int size=23;public Tire(int size){size=this.size;}public void init(){System.out.println("轮胎-size:"+size);}
}

当最底层代码改动之后,整个调用链上的所有代码都需要修改,解决传统开发中的缺陷

3、理解SpringIoC

Spring是一个IoC(控制反转)容器,具备两个最基础的功能:将对象存入到容器;从容器中取出对象。其最核心的功能就是如何将对象存入到Spring中,再从Spring中获取对象的过程

将对象存放到容器中的好处:将对象存储在loC容器相当于将以后可能用的所有工具制作好都放到仓库中,需要的时候直接取就行了,用完再把它放回到仓库。而new对象的方式相当于,每次需要工具了,才现做,用完就扔掉了也不会保存,下次再用的时候还得重新做,这就是IoC容器和普通程序开发的区别。

4、DI(依赖注入)

在程序运行期间,动态地将某个对象引入到当前的机制(或行为)

从广义来说:IoC(设计思想)=DI(具体的实现技术),从不同的维度来描述同一问题

Spring的创建和使用

1、创建Spring项目

1.1、创建一个普通Maven项目

97870b24d7864f6fac26eb9811050fc1.png

1.2、添加Spring框架支持

b78463f8fb8049858e25ee83d1cd9194.png

1.3、添加启动类和main方法

Maven项目导入jar和设置国内源的方法:

配置国内源

bf5b74444a9c4483a8ddfc88216c7b64.png

 02c66dc4f2da4dfd91668907149745b2.png

配置和检测 setting.xml

247358f724d64e249383d688278eb1da.png

maven项目下载jar失败的解决方案:

没有配置国内源;

删除本地仓库的所有jar包,重写下载;

网络运营商访问国内源接口出现问题 

2、存储Bean对象

2.1、创建Bean对象

public class User {public String exo(){return "baekhyun";}
}

2.2、将Bean对象存储到Spring当中

在resources下创建一个spring配置文件

0cd9551276ac498d9cea26884c02da12.png

将Bean对象配置到spring配置文件中

<?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:content="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"><bean id="user" class="User"></bean>
</beans>
<bean id="user" class="User"></bean>

id中是bean对象名称          class中是对象本身:包名+类名

3、获取并使用Bean对象

3.1、先得到Spring上下文对象

8c1093e1f5914105bd29ede0040191b3.png

或者

BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

3.2、从Spring中取出Bean对象

User user= (User) context.getBean("user");

3.3、使用Bean

System.out.println(user.exo());

获取Bean的方式:

1、根据名称获取Bean

User user=(User)context.getBean(“user”)

2、根据Bean类型来获取Bean

User user=context.getBean(User.class)

3、根据Bean名称+Bean类型来获取Bean

User user=context.getBean(“user”,User.class)

ApplicationContext和BeanFactory

相同点:

        1、都是可以得到Spring上下文对象;

        2、都是来自Spring的顶级接口

不同点:

        1、继承关系和功能方面来说: Spring容器有两个顶级的接口: BeanFactory和ApplicationContext。ApplicationContext 属于BeanFactory的子类,其中BeanFactory提供了基础的访问Bean的能力。ApplicationContext除了继承了BeanFactory 的所有功能之外,它还包含更多的功能,如国际化支持、资源访问、事件传播等。
        2、从性能方面来说: ApplicationContext 是一次性加载并初始化所有的Bean对象,而BeanFactory 是需要哪个Bean才去加载Bean对象,因此更加轻量。

Spring更简单的读取和存储对象

1、存储Bean对象

1.1、配置扫描路径

<?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:content="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"><content:component-scan base-package="com.demo.component"></content:component-scan><bean id="user" class="User"></bean>
</beans>

1.2、添加注解存储Bean对象 

类注解:@Controller(控制器)、@Service(服务)、@Repository(仓库)、@Component(组件)、@Configuration(配置)

方法注解:@Bean(将当前修饰方法的方法对象存储到Spring当中)

方式一:类注解 

1.2.1、@Controller(控制器存储)

@Controller
public class ArticleController {public String sayHi(){return "hello,controller";}
}
        ArticleController articleController=context.getBean("articleController",ArticleController.class);System.out.println(articleController.sayHi());

 1.2.2、@Component(组件)

@Component
public class UserComponent {public String sayHi(){return "hi,@component";}
}
        UserComponent userComponent=context.getBean("userComponent",UserComponent.class);System.out.println(userComponent.sayHi());

 项目中没有目录,所有的类都写在Java根路径下

<content:component-scan base-package="**"></content:component-scan>

1.2.3、@Service(服务)

@Service
public class ArticleController {public String sayHi(){return "hello,controller";}
}

 1.2.4、@Repository(仓库)

@Repository
public class ArticleController {public String sayHi(){return "hello,controller";}
}

1.2.5、@Configuration(配置)

@Configuration
public class ArticleController {public String sayHi(){return "hello,controller";}
}

 五大类注解用途:

1、@Controller(控制器):归属于业务逻辑层,用来控制用户的行为,它用来检查用户参数的有效性

2、@Service(服务):归属于服务层,调用持久化类实现相应的功能(不直接和数据库交互,类似于控制中心)

3、@Repository(仓库):归属于持久层,是直接和数据库进行交互的。通常每一个表都会对应一个@Repository

4、@Configuration(配置):归属于配置层,是用来配置当前项目的一些信息

4、@Component(组件):归属于公共工具类,提供某些公共方法

调用流程如下:

7573d465c07d409bab72c5cee3c7b3e7.png

 方式二:方法注解

将返回的对象存储到Spring当中

注意事项:@Bean一定要配合五大类注解

public class Student {private int id;private String name;private int age;@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
@Component
public class StudentBeans {@Beanpublic Student student(){Student stu=new Student();stu.setId(04);stu.setName("baekhyun");stu.setAge(30);return stu;}
}
        Student student=context.getBean("student",Student.class);System.out.println(student);

 或

@Component
public class StudentBeans {@Bean(name = {"s1","s2"})public Student student(){Student stu=new Student();stu.setId(04);stu.setName("baekhyun");stu.setAge(30);return stu;}
}
        Student student=context.getBean("s1",Student.class);System.out.println(student);

 当给@Bean设置了name属性之后,使用原方法名就不能获取到对象了,只能使用设置的名称才能获取

2、获取Bean对象

获取bean对象的过程也叫做对象装配,是把对象取出来放到某个类中,其也叫做对象注入

2.1、属性注入

@Autowired

@Controller
public class StudentController {//1、使用属性注入获取bean@Autowiredprivate StudentService studentService;public void sayHi(){//调用service方法studentService.sayHi();}
}
@Service
public class StudentService {public void sayHi(){//System.out.println("hi,service");}
}
        StudentController sc=context.getBean("studentController",StudentController.class);sc.sayHi();

优点:实现简单

缺点:不能注入不可变(final)对象

d223c69d47bc42328af9f2716d2fa927.png

        只能适用于IoC容器

        针对对象是类,容易违背单一设计原则

2.2、构造方法注入

    //3、构造方法注入private StudentService studentService;@Autowiredpublic StudentController(StudentService studentService){this.studentService=studentService;}

 如果当前类中只有一个构造方法,可以省略@Autowired

优点:可以注入不可变对象;

    //3、构造方法注入private final StudentService studentService;public StudentController(StudentService studentService){this.studentService=studentService;}

        注入对象不会被修改(原因:加了final修饰符;构造方法是随着类加载只执行一次的,不像set有可能执行多次被修改的风险);

        注入对象会被完全初始化;

        通用性更好

缺点:没有属性注入实现简单

2.3、Setter注入

    //2、set注入private StudentService studentService;@Autowiredpublic void setStudentService(StudentService studentService){this.studentService=studentService;}

优点:更加符合单一设计原则(针对对象方法级别)

缺点:不能注入不可变对象

        注入的对象可被修改(set方法是普通set方法,可以被重复调用,在被调用时就存在修改的风险)

2.4、使用@Resource(另一种注入方法)实现

    @Resourceprivate StudentService studentService;

@Resource和@Autowired

相同点:都是用来实现依赖注入的

不同点:

1、功能支持不同:@Autowired支持属性注入、setter注入、构造方法注入;@Resource支持属性注入和setter注入,但不支持构造方法注入

2、出身不同:@Autowired来自Spring框架;@Resource来自于JDK

3、参数支持不同:@Autowired只支持required参数;@Resource支持更多的参数设置

Bean作用域和生命周期

1、Bean作用域定义

Bean在整个Spring框架(项目)中的某种行为模式

1.1、singleton(单例作用域)

描述:该作用域下的Bean在loC容器中只存在一个实例:获取Bean(即通过applicationContext.getBean等方法获取)及装配Bean(即通过@Autowired注入)都是同一个对象

场景:通常无状态的Bean使用该作用域。无状态表示Bean对象的属性状态不需要更新

备注:Spring默认选择该作用域
 

@Controller
public class UserController {@Autowiredprivate User user1;public void getUser(){System.out.println("user1:"+user1);User u=user1;u.setName("边伯贤");System.out.println("u:"+u);}
}
@Controller
public class UserAdviceController {@Resourceprivate User user1;public void getUser(){System.out.println("user1:"+user1);}
}
@Component
public class UserBeans {@Beanpublic User user1(){User user=new User();user.setId(4);user.setName("baekhyun");user.setPassword("30");return user;}
}
        UserController userController=context.getBean("userController",UserController.class);userController.getUser();UserAdviceController userAdviceController=context.getBean("userAdviceController",UserAdviceController.class);userAdviceController.getUser();

b8ba45d2b50e41e8b71decca02b6b1a1.png

1.2、prototype(原型作用域/多例作用域)

描述:每次对该作用域下的Bean的请求都会创建新的实例:获取Bean(即通过applicationContext.getBean等方法获取)及装配Bean(即通过@Autowired注入)都是新的对象实例

场景:通常有状态的Bean使用该作用域

1.3、request(请求作用域)

描述:每次http请求会创建新的Bean实例,类似于prototype

场景:一次http的请求和响应的共享Bean

备注:限定SpringMVC中使用

1.4、session(回话作用域)

描述:在一个http session中,定义一个Bean实例

场景:用户回话的共享Bean,比如:记录一个用户的登陆信息

备注:限定SpringMVC中使用

1.5、applicatioon(全局作用域)

描述:在一个http servlet Context中,定义一个Bean实例

场景:Web应用的上下文信息,比如:记录一个应用的共享信息

备注:限定SpringMVC中使用

singleton(单例作用域)和application(全局作用域):

singleton是Spring Core的作用域,application是Spring Web中的作用域;

singleton作用于IoC的容器,application作用于Servlet容器

1.6、设置作用域

通过使用@Scope来设置Bean的作用域

直接设置值:@Scope("prototype")

全局变量的方式设置:@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

2、Spring执行流程和Bean的生命周期

2.1、Spring执行流程

a.启动容器(启动项目);

b.读取配置文件,初始化(使用xml直接注册bean;配置bean根路径);

c.将bean存储到spring中,通过类注解进行扫描和装配;

d.将spring从注解中读取出来,装配到相应的类

2.2、Bean生命周期

a.实例化Bean(为Bean分配内存空间)

b.设置属性(Bean的注入和装配)

c.Bean初始化

d.使用Bean

e.销毁Bean

@Component
public class BeanLifeComponent implements BeanNameAware {@Overridepublic void setBeanName(String s) {System.out.println("执行了通知");}@PostConstructpublic void postConstruct(){System.out.println("执行了@PostConstruct");}public void init(){System.out.println("执行了init-method方法");}@PreDestroypublic void PreDestory(){System.out.println("执行了销毁方法");}
}
public class App2 {public static void main(String[] args) {ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");BeanLifeComponent beanLifeComponent=context.getBean("beanLifeComponent",BeanLifeComponent.class);System.out.println("使用Bean");//销毁Beancontext.destroy();}
}

案例:如何实现A->B->C

@Component
public class AComponent {@Autowiredprivate BComponent component;@PostConstructpublic void postConstruct(){System.out.println("执行了A对象的postConstruct方法");}
}
@Component
public class BComponent {@Autowiredprivate CComponent component;@PostConstructpublic void postConstruct(){System.out.println("执行了B对象的postConstruct方法");}
}
@Component
public class CComponent {@PostConstructpublic void postConstruct(){System.out.println("执行了C对象的postConstruct方法");}
}
public class App3 {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");AComponent aComponent=context.getBean("AComponent",AComponent.class);}
}

 

相关文章:

Spring基础与创建

目录 前言 Spring基础与核心概念 Spring是什么 1、什么是容器 2、什么是IoC 3、理解SpringIoC 4、DI&#xff08;依赖注入&#xff09; Spring的创建和使用 1、创建Spring项目 1.1、创建一个普通Maven项目 1.2、添加Spring框架支持 1.3、添加启动类和main方法 2、…...

虚拟机系列教程:虚拟机克隆

克隆主要是对磁盘文件进行操作。 1&#xff09;完整克隆 a、拷贝虚拟机磁盘文件 b、生成虚拟机配置文件 centos7-2 291b0480-955a-45e2-a001-690fded69d1b c、导入xml并启动 [rootcentos8 ~]# virt-clone -o centos7 --auto-clone ERROR 要克隆的域必须已经关闭。 [rootcent…...

iperf3主页官方信息

​ iPerf 是一款支持TCP,UDP和SCTP的高速协议测试工具 网络极限性能测试网络中立性检测 主页 下载iPerf安装包 公共的iPerf3服务器 iPerf用户手册 iPerf论坛—法语 联系我们 iPerf / iPerf3简介 iPerf3是一款用于对IP网络的最大带宽进行主动测试的工具。提供对和时间&…...

Linux-0.11 kernel目录进程管理sched.c详解

Linux-0.11 kernel目录进程管理sched.c详解 sched.c主要功能是负责进程的调度&#xff0c;其最核心的函数就是schedule。除schedule以外&#xff0c; sleep_on和wake_up也是相对重要的函数。 schedule void schedule(void)schedule函数的基本功能可以分为两大块&#xff0c;…...

AI已到,普通人的机会在哪里?

“普通人赚到钱很难 但是被骗到钱很容易”。每当火起来一个行业&#xff08;或者仅是一个概念&#xff09;&#xff0c;都会有人来问&#xff1a;现在去做点什么&#xff0c;能够踩上风口&#xff1f;普通人的赚钱机会在哪&#xff1f;怎么做能够暴富&#xff1f;让我们先来看看…...

CSP-J2022入门组二轮补赛试题(山东)T2:宴会

题目链接 CSP-J2022入门组二轮补赛(山东)第2题:宴会 题目背景 今人不见古时月,今月曾经照古人。梦回长安,大唐风华,十里长安花,一日看尽。 唐长安城是当时世界上规模最大、建筑最宏伟、规划布局最为规范化的一座都城。其营建 制度规划布局的特点是规模空前、创设皇城…...

ubuntu 使用 CMake 构建 Qt5 项目

Qt 概述 概念 Qt 是一个跨平台的 C 图形用户界面应用程序框架 常见的 C GUI: Qt 和 MFC 跨平台 Windows Linux MacOS 嵌入式平台 版本 包括商业版和开源免费版 案例 Linux 桌面环境 KDE WPS Office Qt 安装 下载地址: https://download.qt.io/archive/qt/ http…...

ZooKeeper命令及JavaAPI操作

ZooKeeper数据模型 ZooKeeper是一个树形目录服务&#xff0c;其数据模型和Uiix的文件目录树很类似&#xff0c;拥有一个层次化结构。这里面的每一个节点都被称为&#xff1a;ZNode&#xff0c;每个节点上都会保存自己的数据和节点信息。节点可以拥有子节点&#xff0c;同时也允…...

云医疗信息系统源码(云HIS)商业级全套源代码

云his系统源码&#xff0c;有演示 一个好的HIS系统&#xff0c;要具有开放性&#xff0c;便于扩展升级&#xff0c;增加新的功能模块&#xff0c;支撑好医院的业务的拓展&#xff0c;而且可以反过来给医院赋能&#xff0c;最终向更多的患者提供更好地服务。 私信了解更多&…...

u盘拔掉再插上去文件没了原因|文件恢复方法

如果您遇到了“u盘拔了再插文件变空了”的类似问题困扰&#xff0c;请仔细阅读文本&#xff0c;下面将分享几种方法来恢复u盘上丢失的文件&#xff0c;赶紧来试试&#xff01;为什么u盘拔掉再插上去文件没了“我的u盘为什么放进东西后拔出&#xff0c;再插进电脑去东西就没有了…...

CorelDRAW2023详解新增七大功能 ,CorelDRAW2023最新版本更新怎么样?

CorelDRAW2023新功能有哪些&#xff1f;CorelDRAW2023最新版本更新怎么样&#xff1f;让我们带您详细了解&#xff01; CorelDRAW Graphics Suite 2023是矢量制图行业的标杆软件&#xff0c;2023年全新版本为您带来多项新功能和优化改进。本次更新强调易用性&#xff0c;包括更…...

LearnOpenGL-光照-4.光照贴图

本人刚学OpenGL不久且自学&#xff0c;文中定有代码、术语等错误&#xff0c;欢迎指正 我写的项目地址&#xff1a;https://github.com/liujianjie/LearnOpenGLProject 文章目录光照贴图漫反射贴图例子1镜面光贴图例子2 采样镜面光贴图小结什么是光照贴图光照贴图如何影响颜色光…...

ThreadLocal解析

ThreadLocal是一个存储线程本地变量的对象&#xff0c;在ThreadLocal中存储的对象在其他线程中是不可见的&#xff0c;本文介绍ThreadLocal的原理。 1、threadLocal使用 有如下代码&#xff1a; Slf4j public class TestThreadLocal {public static void main(String[] args…...

时间格式表

时间格式化对照表 仅供参考标识符含义aAM/PM(上午/下午)A0~86399999 (一天的第A微秒)c/cc1~7 (一周的第一天, 周天为1)cccSun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写)ccccSunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼)d1~31 (月份的第几天, 带0)D1~36…...

enscape和twinmotion哪个好用?

Twinmotion 和 Enscape这2款渲染软件最近受到了一些初学者的关注。这 2 个软件适用于那些需要 3D 渲染但质量不是他们项目的首要任务的人。在本文中&#xff0c;我们将对Twinmotion 和 Enscape 进行面对面的比较&#xff0c;并帮助您确定哪一个更适合您。什么是 Twinmotion&…...

Canvas

canvas介绍 什么是 Canvas&#xff1f;Canvas 是为了解决 Web 页面中只能显示静态图片这个问题而提出的&#xff0c;一个可以使用 JavaScript 等脚本语言向其中绘制图像的 HTML 标签。 Canvas 解决了什么问题 我在 MSDN&#xff08;《Microsoft Developer Network》是微软一…...

旅游预约APP开发具有什么优势和功能

旅游活动目前正在作为广大用户休闲娱乐的一个首选内容&#xff0c;不仅是公司团建活动可以选择旅游&#xff0c;而且一些节假日也可以集结自己的亲朋好友来一次快乐有趣的旅游活动&#xff0c;随着当代人对于旅游的需求呈现上升的趋势&#xff0c;也让旅游预约APP开发开始流行并…...

Python之函数参数细讲

文章目录前言一、了解形式参数和实际参数1. 通过作用理解2. 通过一个比喻来理解形式参数和实际参数二、位置参数1. 数量必须与定义时一致2. 位置必须与定义时一致三、关键字参数四、为参数设置默认值五、可变参数1. *parameter2. **parameter总结前言 在调用函数时&#xff0c;…...

跑步耳机入耳好还是不入耳好、十大跑步运动耳机品牌排行榜推荐

健身房经常会播放一些节奏较快的歌曲&#xff0c;这样能够激发大家在运动过程中的动力&#xff0c;所以运动时聆听音乐确实比较有效果&#xff0c;居家运动、室外跑步时选择运动耳机就变成了刚需&#xff0c;首先不能影响其他人、佩戴时要稳定&#xff0c;音质和续航要有保证&a…...

Go语言容器之数组和切片

Go语言的容器分为值类型和引用数据类型 一、数组 1.数组的声明和初始化 (1) 数组声明的语法 var 数组变量名 [数组大小]数组类型 举例&#xff1a; package main import "fmt"func main(){//数组的声明var arr[10]int//打印数组长度fmt.Println("arr的长度为…...

基于算法竞赛的c++编程(28)结构体的进阶应用

结构体的嵌套与复杂数据组织 在C中&#xff0c;结构体可以嵌套使用&#xff0c;形成更复杂的数据结构。例如&#xff0c;可以通过嵌套结构体描述多层级数据关系&#xff1a; struct Address {string city;string street;int zipCode; };struct Employee {string name;int id;…...

AspectJ 在 Android 中的完整使用指南

一、环境配置&#xff08;Gradle 7.0 适配&#xff09; 1. 项目级 build.gradle // 注意&#xff1a;沪江插件已停更&#xff0c;推荐官方兼容方案 buildscript {dependencies {classpath org.aspectj:aspectjtools:1.9.9.1 // AspectJ 工具} } 2. 模块级 build.gradle plu…...

技术栈RabbitMq的介绍和使用

目录 1. 什么是消息队列&#xff1f;2. 消息队列的优点3. RabbitMQ 消息队列概述4. RabbitMQ 安装5. Exchange 四种类型5.1 direct 精准匹配5.2 fanout 广播5.3 topic 正则匹配 6. RabbitMQ 队列模式6.1 简单队列模式6.2 工作队列模式6.3 发布/订阅模式6.4 路由模式6.5 主题模式…...

现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?

现有的 Redis 分布式锁库&#xff08;如 Redisson&#xff09;相比于开发者自己基于 Redis 命令&#xff08;如 SETNX, EXPIRE, DEL&#xff09;手动实现分布式锁&#xff0c;提供了巨大的便利性和健壮性。主要体现在以下几个方面&#xff1a; 原子性保证 (Atomicity)&#xff…...

Rust 开发环境搭建

环境搭建 1、开发工具RustRover 或者vs code 2、Cygwin64 安装 https://cygwin.com/install.html 在工具终端执行&#xff1a; rustup toolchain install stable-x86_64-pc-windows-gnu rustup default stable-x86_64-pc-windows-gnu ​ 2、Hello World fn main() { println…...

嵌入式常见 CPU 架构

架构类型架构厂商芯片厂商典型芯片特点与应用场景PICRISC (8/16 位)MicrochipMicrochipPIC16F877A、PIC18F4550简化指令集&#xff0c;单周期执行&#xff1b;低功耗、CIP 独立外设&#xff1b;用于家电、小电机控制、安防面板等嵌入式场景8051CISC (8 位)Intel&#xff08;原始…...

提升移动端网页调试效率:WebDebugX 与常见工具组合实践

在日常移动端开发中&#xff0c;网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时&#xff0c;开发者迫切需要一套高效、可靠且跨平台的调试方案。过去&#xff0c;我们或多或少使用过 Chrome DevTools、Remote Debug…...

图解JavaScript原型:原型链及其分析 | JavaScript图解

​​ 忽略该图的细节&#xff08;如内存地址值没有用二进制&#xff09; 以下是对该图进一步的理解和总结 1. JS 对象概念的辨析 对象是什么&#xff1a;保存在堆中一块区域&#xff0c;同时在栈中有一块区域保存其在堆中的地址&#xff08;也就是我们通常说的该变量指向谁&…...

C++--string的模拟实现

一,引言 string的模拟实现是只对string对象中给的主要功能经行模拟实现&#xff0c;其目的是加强对string的底层了解&#xff0c;以便于在以后的学习或者工作中更加熟练的使用string。本文中的代码仅供参考并不唯一。 二,默认成员函数 string主要有三个成员变量&#xff0c;…...

深入解析光敏传感技术:嵌入式仿真平台如何重塑电子工程教学

一、光敏传感技术的物理本质与系统级实现挑战 光敏电阻作为经典的光电传感器件&#xff0c;其工作原理根植于半导体材料的光电导效应。当入射光子能量超过材料带隙宽度时&#xff0c;价带电子受激发跃迁至导带&#xff0c;形成电子-空穴对&#xff0c;导致材料电导率显著提升。…...