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

Spring学习笔记1

目录

  • 1 什么是spring
  • 2 spring的优势
  • 3 IOC的概念和作用
    • 3.1 无参数构造函数的实例化方式
    • 3.2 使用工厂中的普通方法实例化对象
  • 4 Bean
    • 4.1 Bean相关概念
    • 4.2 Bean对象的作用范围
  • 5 DI
    • 5.1 构造函数注入
    • 5.2 set方法注入
    • 5.3 复杂类型数据注入
    • 5.4 基于注解的IOC
      • 5.4.1 包扫描
      • 5.4.2 @Component
      • 5.4.3 由Component衍生的注解
      • 5.4.4 @Autowired
      • 5.4.5 其他注入数据的注解
  • 6 配置类
      • 6.1. `@Configuration` 注解
      • 6.2 `@Import` 注解
      • 6.3 `@PropertySource` 注解
      • 6.4 综合示例

1 什么是spring

Spring 是一个开源的 Java 应用框架,旨在简化企业级应用开发。它利用依赖注入(DI)和面向切面编程(AOP)减少代码耦合,提升可测试性和维护性。Spring 核心容器管理应用对象的生命周期和配置。

2 spring的优势

  1. 简化开发:通过依赖注入(DI)和面向切面编程(AOP),Spring 让代码更加模块化、解耦,减少了样板代码,提升了开发效率。

  2. 灵活性:Spring 提供了多种配置方式(注解、XML、JavaConfig),支持多种持久化框架和第三方库集成,适应不同的应用需求。

  3. 测试友好:依赖注入让单元测试和集成测试更加简便,无需依赖运行环境即可快速测试业务逻辑。

  4. 生态丰富:Spring 有强大的生态系统,包括 Spring Boot(简化配置和部署)、Spring Security(提供安全管理)、Spring Cloud(微服务支持)等,能够满足复杂的应用场景。

  5. 轻量级:Spring 的核心容器是小巧的,开发者可以选择性地使用需要的模块,避免不必要的依赖。

3 IOC的概念和作用

3.1 无参数构造函数的实例化方式

创建一个XML配置文件(例如 applicationContext.XML)

<bean id="userService" class="com.example.UserService"/>

使用Spring的ApplicationContext加载XML配置文件

 	// 加载 Spring XML 配置文件ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取 BeanUserService userService = (UserService) context.getBean("userService");

3.2 使用工厂中的普通方法实例化对象

首先,创建一个工厂类,其中包含一个普通方法来创建 Bean 实例。

package com.example;public class UserFactory {public UserService createUserService() {return new UserService();}
}

在 Spring 的 XML 配置文件中,配置工厂 Bean 和使用工厂方法来创建 Bean。

   <!-- 定义工厂 Bean --><bean id="userFactory" class="com.example.UserFactory"/><!-- 使用工厂方法来创建 Bean --><bean id="userService" factory-bean="userFactory" factory-method="createUserService"/>

4 Bean

4.1 Bean相关概念

  1. Bean: 在计算机英语中,有可重用组件(如持久层和业务层的接口和重用)的含义。

  2. JavaBean: 其实JavaBean并不完全等于实体类,因为实体类只是可重用组件的一部分,也就是JavaBean的范围大于实体类的范围。

JavaBean的含义:用Java语言编写的可重用组件。

4.2 Bean对象的作用范围

bean标签的scope属性,

作用:用于指定bean的作用范围

取值:

  • singleton:单例的(默认值)

  • prototype:多例的

  • ···等

5 DI

依赖注入:Dependency Injection

5.1 构造函数注入

bean标签内部的constructor-arg标签

标签中的属性:

  • name:用于给构造函数中指定名称的参数赋值
  • value:用于提供标准类型和String类型的数据
  • ref:用于指定其他的bean类型数据(就是在ioc容器中出现过的bean对象)

需要提供一个含参数的构造器

public class Account{private Integer id;private String name;public Account(Integer id,String name){this.id = id;this.name = name;}
}
<bean id = "account" class = "com.example.Account"><constructor-arg name = "id" value = 10></constructor-arg><constructor-arg name = "name" value ="张三"></constructor-arg>
</bean>

5.2 set方法注入

bean标签内部的property标签

标签的属性:

  • name:用于给构造函数中指定名称的参数赋值
  • value:用于提供标准类型和String类型的数据
  • ref:用于指定其他的bean类型数据(就是在ioc容器中出现过的bean对象)

需要提供set方法

public class Account{private Integer id;private String name;public void setId(Integer id){this.id = id;}public void setName(String name){this.name = name;}
}
<bean id="account" class="com.example.Account"><property name = "id" value = 10></property><property name = "name" value ="张三"></property>
</bean>

5.3 复杂类型数据注入

在 Spring 框架中,除了注入简单的类型(如 Stringint 等),还可以注入复杂类型的数据,例如 ListSetMapProperties。这些复杂类型的数据可以通过 property 标签及其子标签来配置。

package com.example;
public class UserService {private List<String> emails;private Set<String> phoneNumbers;private Map<String, String> userRoles;private Properties props;public void setEmails(List<String> emails) {this.emails = emails;}public void setPhoneNumbers(Set<String> phoneNumbers) {this.phoneNumbers = phoneNumbers;}public void setUserRoles(Map<String, String> userRoles) {this.userRoles = userRoles;}public void setProps(Properties props) {this.props = props;}
}
    <bean id="userService" class="com.example.UserService"><!-- 注入 List --><property name="emails"><list><value>john@example.com</value><value>jane@example.com</value></list></property><!-- 注入 Set --><property name="phoneNumbers"><set><value>+123456789</value><value>+987654321</value></set></property><!-- 注入 Map --><property name="userRoles"><map><entry key="john" value="admin"/><entry key="jane" value="user"/></map></property><!-- 注入 Properties --><property name="props"><props><prop key="language">English</prop><prop key="country">USA</prop></props></property></bean></beans>

5.4 基于注解的IOC

5.4.1 包扫描

使用注解时要告知spring在创建容器时要扫描的包

<context:component-scan base-package="com.example"></context:component-scan>

5.4.2 @Component

  • 用法:定义在类名上

  • 作用:用于把当前类对象存入spring容器中

  • 属性:value:用于指定bean的id,默认值是类名(首字母小写)

5.4.3 由Component衍生的注解

  • @Controller:一般用在表现层
  • @Service:一般用在业务
  • @Repository:一般用在持久层

这三个注解他们的作用和属性与@Component相同,他们三个是spring框架为我们提供明确的三层使用的注解。

5.4.4 @Autowired

@Autowired 是 Spring 框架中用于实现依赖注入(Dependency Injection, DI)的核心注解之一。它可以自动将 Spring 容器中匹配的 Bean 注入到需要的地方。@Autowired 可以用于构造函数、字段、setter 方法或任意方法。

@Autowired 的详细使用

  1. 字段注入

这是最常见的方式,直接在字段上使用 @Autowired 注解,Spring 会自动将匹配的 Bean 注入到该字段。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class UserService {@Autowiredprivate UserRepository userRepository;public void displayUser() {userRepository.findUser();}
}
  1. 构造函数注入

在构造函数上使用 @Autowired 注解,Spring 会在实例化时通过构造函数注入依赖。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class UserService {private final UserRepository userRepository;@Autowiredpublic UserService(UserRepository userRepository) {this.userRepository = userRepository;}public void displayUser() {userRepository.findUser();}
}
  1. Setter 方法注入

通过在 setter 方法上使用 @Autowired 注解,Spring 会在 Bean 创建后调用该方法完成注入。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class UserService {private UserRepository userRepository;@Autowiredpublic void setUserRepository(UserRepository userRepository) {this.userRepository = userRepository;}public void displayUser() {userRepository.findUser();}
}
  1. 任意方法注入

除了构造函数和 setter 方法,@Autowired 还可以用在任意方法上。Spring 会在 Bean 创建后调用该方法完成注入。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class UserService {private UserRepository userRepository;@Autowiredpublic void arbitraryMethod(UserRepository userRepository) {this.userRepository = userRepository;}public void displayUser() {userRepository.findUser();}
}
  1. 默认行为与 @Autowired 的要求
  • 默认行为@Autowired 注解默认采用 byType 的方式进行注入。如果找到多个匹配的 Bean,Spring 会尝试根据 @Qualifier 注解进一步确定要注入的 Bean。
  • 必填性:默认情况下,@Autowired 是必填的(required=true),即 Spring 必须找到一个匹配的 Bean,否则会抛出 NoSuchBeanDefinitionException 异常。
  • 非必填性:可以通过设置 @Autowired(required = false) 来使依赖成为可选的,这样如果找不到匹配的 Bean,Spring 将不会注入任何东西。

5.4.5 其他注入数据的注解

@Qualifier@Resource@Value 都是 Spring 框架中用于依赖注入(Dependency Injection, DI)的注解,但它们的用途和使用场景有所不同。

  1. @Qualifier

@Qualifier 注解用于在多个相同类型的 Bean 中指定注入哪一个具体的 Bean。它通常与 @Autowired 一起使用。

使用场景

当有多个相同类型的 Bean 存在时,Spring 无法确定注入哪一个 Bean,这时可以使用 @Qualifier 注解来指定具体的 Bean。

示例

假设我们有两个实现 UserRepository 接口的 Bean:

import org.springframework.stereotype.Repository;@Repository("primaryUserRepository")
public class PrimaryUserRepository implements UserRepository {// 实现
}@Repository("secondaryUserRepository")
public class SecondaryUserRepository implements UserRepository {// 实现
}

UserService 中,我们可以使用 @Qualifier 注解来指定注入 secondaryUserRepository

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component
public class UserService {private final UserRepository userRepository;@Autowiredpublic UserService(@Qualifier("secondaryUserRepository") UserRepository userRepository) {this.userRepository = userRepository;}public void displayUser() {userRepository.findUser();}
}
  1. @Resource

@Resource 注解是 Java 的标准注解(JSR-250),用于依赖注入。它可以根据名称或类型进行注入。默认情况下,@Resource 是按名称注入的,如果没有找到匹配的名称,则按类型注入。

使用场景

@Resource 注解通常用于按名称注入 Bean,如果不指定名称,则会使用默认的名称(即字段名)。

示例

假设我们有两个实现 UserRepository 接口的 Bean:

import org.springframework.stereotype.Repository;@Repository("primaryUserRepository")
public class PrimaryUserRepository implements UserRepository {// 实现
}@Repository("secondaryUserRepository")
public class SecondaryUserRepository implements UserRepository {// 实现
}

UserService 中,我们可以使用 @Resource 注解来指定注入 secondaryUserRepository

import javax.annotation.Resource;
import org.springframework.stereotype.Component;@Component
public class UserService {@Resource(name = "secondaryUserRepository")private UserRepository userRepository;public void displayUser() {userRepository.findUser();}
}
  1. @Value

@Value 注解用于注入配置文件中的值或直接注入字符串、数字等常量值。它通常用于注入简单的属性,例如字符串、数字、布尔值等。

使用场景

@Value 注解通常用于注入配置文件中的属性值,或者直接注入常量值。

示例

假设我们在 application.properties 文件中定义了一个属性:

app.name=MyApplication

UserService 中,我们可以使用 @Value 注解来注入这个属性值:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class UserService {@Value("${app.name}")private String appName;public void displayAppName() {System.out.println("Application Name: " + appName);}
}

我们也可以直接注入常量值:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class UserService {@Value("Hello, World!")private String greeting;public void displayGreeting() {System.out.println(greeting);}
}

4.@Scope

@Scope 是 Spring 框架中的一个注解,用于定义 Bean 的作用域。Bean 的作用域决定了 Bean 的生命周期以及 Spring 容器如何管理 Bean 的实例。通过 @Scope 注解,你可以指定 Bean 在应用中的生命周期和可访问范围。

常见的作用域

  1. singleton(单例)

    • 描述:这是 Spring 的默认作用域。在单例作用域中,每个 Spring IoC 容器仅创建一个 Bean 实例,并且所有对 Bean 的请求都会共用这一个实例。
    • 适用场景:大多数无状态的 Bean 都适合使用单例作用域。
  2. prototype(原型)

    • 描述:每次请求都会创建一个新的 Bean 实例。
    • 适用场景:当 Bean 是有状态的,即每个实例需要独立的状态时,应使用原型作用域。
  3. request(请求)

    • 描述:在 Web 应用中,每个 HTTP 请求都会创建一个 Bean 实例。该 Bean 只在当前 HTTP 请求内有效。
    • 适用场景:需要在单个 HTTP 请求中维护状态的 Bean。
  4. session(会话)

    • 描述:在 Web 应用中,每个 HTTP Session 都会创建一个 Bean 实例。该 Bean 只在当前 HTTP Session 内有效。
    • 适用场景:需要在用户 Session 范围内维护状态的 Bean。
  5. application(应用)

    • 描述:在 Web 应用中,每个 ServletContext(即整个应用)只有一个 Bean 实例。该 Bean 在整个应用范围内有效。
    • 适用场景:需要在应用范围内共享数据的 Bean。
  6. websocket(WebSocket)

    • 描述:在 Web 应用中,每个 WebSocket 连接都会创建一个 Bean 实例。该 Bean 只在当前 WebSocket 连接内有效。
    • 适用场景:需要在 WebSocket 连接范围内维护状态的 Bean。

使用 @Scope 注解

你可以使用 @Scope 注解来显式地指定 Bean 的作用域。以下是一些示例:

Singleton Scope

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Component
@Scope("singleton")
public class SingletonBean {// Bean implementation
}

6 配置类

好的,@Configuration@Import@PropertySource 是 Spring 框架中常用的注解,分别用于配置类、模块化配置和外部属性文件的加载。下面分别介绍这三个注解的用途和使用方法。

6.1. @Configuration 注解

@Configuration 注解用于标记一个类为 Spring 配置类。配置类中可以定义 @Bean 注解的方法,用于创建和配置 Spring 容器中的 Bean。配置类可以替代传统的 XML 配置文件。

特点

  • 替代 XML 配置:使用 Java 代码代替 XML 文件进行配置。
  • 集中管理 Bean:在一个类中定义多个 Bean,方便管理。
  • 支持依赖注入:可以在配置类中注入其他 Bean。

示例

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Beanpublic MyBean myBean() {return new MyBean();}
}

6.2 @Import 注解

@Import 注解用于将一个或多个配置类导入到当前配置类中。通过 @Import,可以实现模块化配置,将多个配置类组合在一起。

特点

  • 模块化配置:将多个配置类组合在一起,便于管理和维护。
  • 代码复用:可以在多个配置类中导入相同的配置类,避免重复代码。

示例

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;@Configuration
@Import({DatabaseConfig.class, ServiceConfig.class})
public class AppConfig {// 其他 Bean 定义
}

6.3 @PropertySource 注解

@PropertySource 注解用于加载外部属性文件(如 .properties.yml 文件),并将其中的属性值注入到 Spring 环境中。通常与 @Value@ConfigurationProperties 注解一起使用。

特点

  • 加载外部属性文件:支持加载 .properties.yml 文件。
  • 动态配置:可以通过属性文件动态配置应用的行为。
  • 支持多文件:可以加载多个属性文件。

示例

属性文件 app.properties

app.name=MyApp
app.version=1.0

配置类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;@Configuration
@PropertySource("classpath:app.properties")
public class AppConfig {@Value("${app.name}")private String appName;@Value("${app.version}")private String appVersion;public void printAppInfo() {System.out.println("App Name: " + appName);System.out.println("App Version: " + appVersion);}
}

6.4 综合示例

以下是一个综合使用 @Configuration@Import@PropertySource 的示例:

  1. 属性文件 database.properties
db.url=jdbc:mysql://localhost:3306/mydb
db.username=root
db.password=secret
  1. 配置类 DatabaseConfig
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;@Configuration
@PropertySource("classpath:database.properties")
public class DatabaseConfig {@Value("${db.url}")private String dbUrl;@Value("${db.username}")private String dbUsername;@Value("${db.password}")private String dbPassword;@Beanpublic DataSource dataSource() {// 使用属性值创建数据源return new DataSource(dbUrl, dbUsername, dbPassword);}
}
  1. 配置类 ServiceConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ServiceConfig {@Beanpublic MyService myService() {return new MyService();}
}
  1. 主配置类 AppConfig
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;@Configuration
@Import({DatabaseConfig.class, ServiceConfig.class})
public class AppConfig {// 其他 Bean 定义
}

相关文章:

Spring学习笔记1

目录 1 什么是spring2 spring的优势3 IOC的概念和作用3.1 无参数构造函数的实例化方式3.2 使用工厂中的普通方法实例化对象 4 Bean4.1 Bean相关概念4.2 Bean对象的作用范围 5 DI5.1 构造函数注入5.2 set方法注入5.3 复杂类型数据注入5.4 基于注解的IOC5.4.1 包扫描5.4.2 Compon…...

LeetCode 2185. Counting Words With a Given Prefix

&#x1f517; https://leetcode.com/problems/counting-words-with-a-given-prefix 题目 给一个字符串数组&#xff0c;返回其中前缀为 pref 的个数 思路 模拟 代码 class Solution { public:int prefixCount(vector<string>& words, string pref) {int count…...

图漾相机基础操作

1.客户端概述 1.1 简介 PercipioViewer是图漾基于Percipio Camport SDK开发的一款看图软件&#xff0c;可实时预览相机输出的深度图、彩色图、IR红外图和点云图,并保存对应数据&#xff0c;还支持查看设备基础信息&#xff0c;在线修改gain、曝光等各种调节相机成像的参数功能…...

前端开发中页面优化的方法

前端页面优化是指通过改进网页的加载速度、提高用户体验和SEO优化等手段来优化页面性能的过程。以下是一些常见的前端页面优化方法&#xff1a; 压缩和合并文件&#xff1a;通过压缩CSS和JavaScript文件&#xff0c;并将多个文件合并成一个文件&#xff0c;减少网络传输和HTTP请…...

Qt QDockWidget详解以及例程

Qt QDockWidget详解以及例程 引言一、基本用法二、深入了解2.1 窗口功能相关2.2 停靠区域限制2.3 在主窗体布局 引言 QDockWidget类提供了一个可以停靠在QMainWindow内的小窗口 (理论上可以在QMainWindow中任意排列)&#xff0c;也可以作为QMainWindow上的顶级窗口浮动 (类似一…...

机器学习之贝叶斯分类器和混淆矩阵可视化

贝叶斯分类器 目录 贝叶斯分类器1 贝叶斯分类器1.1 概念1.2算法理解1.3 算法导入1.4 函数 2 混淆矩阵可视化2.1 概念2.2 理解2.3 函数导入2.4 函数及参数2.5 绘制函数 3 实际预测3.1 数据及理解3.2 代码测试 1 贝叶斯分类器 1.1 概念 贝叶斯分类器是基于贝叶斯定理构建的分类…...

关于大数据的基础知识(一)——定义特征结构要素

成长路上不孤单&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a; 【14后&#x1f60a;///计算机爱好者&#x1f60a;///持续分享所学&#x1f60a;///如有需要欢迎收藏转发///&#x1f60a;】 今日分享关于大数据的基础知识&#xff08;一&a…...

2025 GitCode 开发者冬日嘉年华:AI 与开源的深度交融之旅

在科技的浪潮中&#xff0c;AI 技术与开源探索的火花不断碰撞&#xff0c;催生出无限可能。2025 年 1 月 4 日&#xff0c;由 GitCode 联合 CSDN COC 城市开发者社区精心打造的开年首场开发者活动&#xff1a;冬日嘉年华在北京中关村 • 鼎好 DH3-A 座 22 层盛大举行&#xff0…...

【MyBatis-Plus 进阶功能】开发中常用场景剖析

MyBatis-Plus&#xff08;MP&#xff09;除了封装常见的 CRUD 操作&#xff0c;还提供了一些高级功能&#xff0c;进一步简化复杂场景下的开发工作。本文将逐一讲解 逻辑删除、自动填充、多表关联查询的原理与使用方式&#xff0c;让你快速掌握这些技巧&#xff01; 一、逻辑删…...

【C++/控制台】2048小游戏

源代码&#xff1a; #include <iostream> #include <windows.h> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <conio.h> #include <time.h>// #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)…...

Linux 中 top 命令的使用与实例解读

目录 Linux 中 top 命令的使用与实例解读一、top 命令参数二、输出字段含义&#xff08;一&#xff09;系统信息&#xff08;二&#xff09;任务信息&#xff08;三&#xff09;CPU 信息&#xff08;四&#xff09;内存信息 三、实例解读系统信息任务信息CPU信息内存信息进程列…...

C++ STL 中的 `unordered_map` 和 `unordered_set` 总结

1. unordered_map unordered_map 是一个基于哈希表实现的容器&#xff0c;存储键值对&#xff08;key-value&#xff09;&#xff0c;每个键必须唯一&#xff0c;可以快速插入、删除、查找。 基本特性 存储结构&#xff1a;键值对 (key-value)。键唯一性&#xff1a;每个键在…...

机器学习基础-概率图模型

&#xff08;一阶&#xff09;马尔科夫模型的基本概念 状态、状态转换概率、初始概率 状态转移矩阵的基本概念 隐马尔可夫模型&#xff08;HMM&#xff09;的基本概念 条件随机场&#xff08;CRF&#xff09;的基本概念 实际应用中的马尔科夫性 自然语言处理&#xff1a; 在词性…...

【MySQL】九、表的内外连接

文章目录 前言Ⅰ. 内连接案例&#xff1a;显示SMITH的名字和部门名称 Ⅱ. 外连接1、左外连接案例&#xff1a;查询所有学生的成绩&#xff0c;如果这个学生没有成绩&#xff0c;也要将学生的个人信息显示出来 2、右外连接案例&#xff1a;对stu表和exam表联合查询&#xff0c;把…...

芯片详细讲解,从而区分CPU、MPU、DSP、GPU、FPGA、MCU、SOC、ECU

目录 芯片的概念结构 芯片的派系划分 通用芯片&#xff08;CPU&#xff0c;MPU&#xff0c;GPU&#xff0c;DSP&#xff09; 定制芯片&#xff08;FPGA&#xff0c;ASIC&#xff09; 芯片之上的集成&#xff08;MCU&#xff0c;SOC&#xff0c;ECU&#xff09; 软硬件的匹…...

halcon三维点云数据处理(十)locate_cylinder_3d

目录 一、locate_cylinder_3d例程代码二、gen_binocular_rectification_map函数三、binocular_disparity函数四、自定义函数select_best_candidates五、自定义函数remove_shadowed_regions 一、locate_cylinder_3d例程代码 1、读取或者创建3D形状模型&#xff0c; 2、根据双目…...

vue(2,3), react (16及以上)开发者工具资源

在前端开发的广阔领域中&#xff0c;Vue.js 和 React.js 作为两大主流框架&#xff0c;各自拥有庞大的用户群体和丰富的生态系统。为了帮助开发者更高效地进行调试和开发&#xff0c;Vue Devtools 和 React 开发者工具应运而生&#xff0c;成为这两个框架不可或缺的辅助工具。本…...

2025年华为OD上机考试真题(Java)——整数对最小和

题目&#xff1a; 给定两个整数数组array1、array2&#xff0c;数组元素按升序排列。假设从array1、array2中分别取出一个元素可构成一对元素&#xff0c;现在需要取出k对元素&#xff0c;并对取出的所有元素求和&#xff0c;计算和的最小值。 注意&#xff1a;两对元素如果对应…...

进程间通信——网络通信——UDP

进程间通信&#xff08;分类&#xff09;&#xff1a;网络通信、无名管道、有名管道、信号、消息队列、共享内存、信号量集 OSI七层模型&#xff1a;&#xff08;理论模型&#xff09; 应用层 : 要传输的数据信息&#xff0c;如文件传输&#xff0c;电子邮件等 表示层 : 数…...

【我的 PWN 学习手札】IO_FILE 之 FSOP

FSOP&#xff1a;File Stream Oriented Programming 通过劫持 _IO_list_all 指向伪造的 _IO_FILE_plus&#xff0c;进而调用fake IO_FILE 结构体对象中被伪造的vtable指向的恶意函数。 目录 前言 一、glibc-exit函数浅析 二、FSOP 三、Largebin attack FSOP &#xff08;…...

Linux 文件类型,目录与路径,文件与目录管理

文件类型 后面的字符表示文件类型标志 普通文件&#xff1a;-&#xff08;纯文本文件&#xff0c;二进制文件&#xff0c;数据格式文件&#xff09; 如文本文件、图片、程序文件等。 目录文件&#xff1a;d&#xff08;directory&#xff09; 用来存放其他文件或子目录。 设备…...

CentOS下的分布式内存计算Spark环境部署

一、Spark 核心架构与应用场景 1.1 分布式计算引擎的核心优势 Spark 是基于内存的分布式计算框架&#xff0c;相比 MapReduce 具有以下核心优势&#xff1a; 内存计算&#xff1a;数据可常驻内存&#xff0c;迭代计算性能提升 10-100 倍&#xff08;文档段落&#xff1a;3-79…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

linux 下常用变更-8

1、删除普通用户 查询用户初始UID和GIDls -l /home/ ###家目录中查看UID cat /etc/group ###此文件查看GID删除用户1.编辑文件 /etc/passwd 找到对应的行&#xff0c;YW343:x:0:0::/home/YW343:/bin/bash 2.将标红的位置修改为用户对应初始UID和GID&#xff1a; YW3…...

三体问题详解

从物理学角度&#xff0c;三体问题之所以不稳定&#xff0c;是因为三个天体在万有引力作用下相互作用&#xff0c;形成一个非线性耦合系统。我们可以从牛顿经典力学出发&#xff0c;列出具体的运动方程&#xff0c;并说明为何这个系统本质上是混沌的&#xff0c;无法得到一般解…...

Spring AI 入门:Java 开发者的生成式 AI 实践之路

一、Spring AI 简介 在人工智能技术快速迭代的今天&#xff0c;Spring AI 作为 Spring 生态系统的新生力量&#xff0c;正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务&#xff08;如 OpenAI、Anthropic&#xff09;的无缝对接&…...

什么是Ansible Jinja2

理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具&#xff0c;可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板&#xff0c;允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板&#xff0c;并通…...

基于matlab策略迭代和值迭代法的动态规划

经典的基于策略迭代和值迭代法的动态规划matlab代码&#xff0c;实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...

Python ROS2【机器人中间件框架】 简介

销量过万TEEIS德国护膝夏天用薄款 优惠券冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜巨奇严选 鞋子除臭剂360ml 多芬身体磨砂膏280g健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖妙洁棉…...

【笔记】WSL 中 Rust 安装与测试完整记录

#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统&#xff1a;Ubuntu 24.04 LTS (WSL2)架构&#xff1a;x86_64 (GNU/Linux)Rust 版本&#xff1a;rustc 1.87.0 (2025-05-09)Cargo 版本&#xff1a;cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...