Spring - 4 ( 11000 字 Spring 入门级教程 )

news2024/10/5 19:16:55

一:Spring IoC&DI

在前⾯的章节中, 我们学习了 Spring Boot 和 Spring MVC 的开发, 可以完成⼀些基本功能的开发了, 但是什么是 Spring 呢? Spring, Spring Boot 和 SpringMVC 又有什么关系呢? 咱们还是带着问题去学习.我们先看什么是Spring

1.1 Spring 是什么?

Spring 是⼀个开源框架, 他让我们的开发更加简单. 他支持广泛的应用场景, 有着活跃而庞大的社区, 这也是 Spring 能够长久不衰的原因,但是这个概念相对来说还是比较抽象,我们还是不太理解什么是 Spring

用⼀句更具体的话来概括 Spring,:包含了众多工具方法的 IoC 容器

那什么又是 IoC 容器?接下来我们⼀起来看

1.1.1 什么是容器

容器是用来容纳某种物品的装置。

生活中的水杯, 垃圾桶, 冰箱等等这些都是容器,我们想想,之前课程我们接触的容器有哪些?

  • List/Map -> 数据存储容器
  • Tomcat -> Web 容器

1.1.2 什么是 IoC?

IoC: Inversion of Control (控制反转),IoC 是 Spring 的核心思想, Spring 是⼀个 “控制反转” 的容器.

控制反转也就是获得对象的控制权发生了反转,当我们需要某个对象时, 传统开发模式中需要自己通过 new 创建对象,而现在不需要再自己进行创建对象了, 我们只需要把创建对象的任务交给 loc 容器,程序中依赖注入就可以了.

其实 IoC 我们在前面已经使用了, 我们在前面讲到,在类上面添加 @RestController 和
@Controller 注解, 就是把这个对象交给 Spring 管理, Spring 框架启动时就会加载该类. 把对象交给 Spring 管理, 就是 IoC 思想.

控制反转是⼀种思想, 在生活中也是处处体现:比如招聘, 企业的员工招聘,入职, 解雇等控制权, 由老板转交给给HR(人力资源)来处理

1.2 IoC 介绍

接下来我们通过案例来了解⼀下什么是 IoC,需求: 造⼀辆⻋

1.2.1 传统程序开发

我们的实现思路是这样的:

先设计轮子,然后根据轮子的大小设计底盘,接着根据底盘设计车身,最后根据车身设计好整个汽⻋。这⾥就出现了⼀个 “依赖” 关系:汽车依赖车身,车身依赖底盘,底盘依赖轮子.

在这里插入图片描述
最终程序的实现代码如下:

public class NewCarExample {
    public static void main(String[] args) {
        Car car = new Car();
        car.run();
    }
    /**
     * 汽⻋对象
     */
    static class Car {
        private Framework framework;
        public Car() {
            framework = new Framework();
            System.out.println("Car init....");
        }
        public void run(){
            System.out.println("Car run...");
        }
    }
    /**
     * ⻋⾝类
     */
    static class Framework {
        private Bottom bottom;
        public Framework() {
            bottom = new Bottom();
            System.out.println("Framework init...");
        }
    }
    /**
     * 底盘类
     */
    static class Bottom {
        private Tire tire;
        public Bottom() {
            this.tire = new Tire();
            System.out.println("Bottom init...");
        }
    }
    /**
     * 轮胎类
     */
    static class Tire {
        // 尺⼨
        private int size;
        public Tire(){
            this.size = 17;
            System.out.println("轮胎尺⼨:" + size);
        }
    }
}

这样的设计看起来没问题,但是可维护性却很低.

接下来需求有了变更: 随着对的车的需求量越来越大, 个性化需求也会越来越多,我们需要加工多种尺寸的轮胎.那这个时候就要对上面的程序进行修改了,修改后的代码如下所示:

在这里插入图片描述

修改之后, 其他调用程序也会报错, 我们需要继续修改

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
完整代码如下:

public class NewCarExample {
    public static void main(String[] args) {
        Car car = new Car(20);
        car.run();
    }
    /**
     * 汽⻋对象
     */
    static class Car {
        private Framework framework;
        public Car(int size) {
            framework = new Framework(size);
            System.out.println("Car init....");
        }
        public void run(){
            System.out.println("Car run...");
        }
    }
    /**
     * ⻋⾝类
     */
    static class Framework {
        private Bottom bottom;
        public Framework(int size) {
            bottom = new Bottom(size);
            System.out.println("Framework init...");
        }
    }
    /**
     * 底盘类
     */
    static class Bottom {
        private Tire tire;
        public Bottom(int size) {
            this.tire = new Tire(size);
            System.out.println("Bottom init...");
        }
    }
    /**
     * 轮胎类
     */
    static class Tire {
        // 尺⼨
        private int size;
        public Tire(int size){
            this.size = size;
            System.out.println("轮胎尺⼨:" + size);
        }
    }
}

从以上代码可以看出,以上程序的问题是:当最底层代码改动之后,整个调用链上的所有代码都需要修改,程序的耦合度非常高

1.2.2 解决方案

我们尝试换⼀种思路, 我们先设计汽车的大概样子,然后根据汽车的样子来设计车身,根据车身来设计底盘,最后根据底盘来设计轮子. 这时候,依赖关系就倒置过来了:轮子依赖底盘, 底盘依赖车身,车身依赖汽车

在这里插入图片描述

那么如何来实现呢:

此时,我们只需要将原来由自己创建的下级类,改为传递的方式(也就是注入的方式),因为我们不需要在当前类中创建下级类了,所以下级类即使发生变化(创建或减少参数),当前类本身也无需修改任何代码,这样就完成了程序的解耦

1.2.3 IoC 程序开发

基于以上思路,我们把调⽤汽车的程序示例改造⼀下,把创建子类的方式,改为注入传递的方式.

具体实现代码如下:.

public class IocCarExample {
    public static void main(String[] args) {
        Tire tire = new Tire(20);
        Bottom bottom = new Bottom(tire);
        Framework framework = new Framework(bottom);
        Car car = new Car(framework);
        car.run();
    }
    static class Car {
        private Framework framework;
        public Car(Framework framework) {
            this.framework = framework;
            System.out.println("Car init....");
        }
        public void run() {
            System.out.println("Car run...");
        }
    }
    static class Framework {
        private Bottom bottom;
        public Framework(Bottom bottom) {
            this.bottom = bottom;
            System.out.println("Framework init...");
        }
    }
    static class Bottom {
        private Tire tire;
        public Bottom(Tire tire) {
            this.tire = tire;
            System.out.println("Bottom init...");
        }
    }
    static class Tire {
        private int size;
        public Tire(int size) {
            this.size = size;
            System.out.println("轮胎尺⼨:" + size);
        }
    }
}

码经过以上调整,无论底层类如何变化,整个调⽤链是不用做任何改变的,这样就完成了代码之间的解耦,从而实现了更加灵活、通用的程序设计了。

1.2.4 IoC 优势

在传统的代码中对象创建顺序是:Car -> Framework -> Bottom -> Tire

改进之后解耦的代码的对象创建顺序是:Tire -> Bottom -> Framework -> Car

在这里插入图片描述

我们发现了⼀个规律,通用程序的实现代码,类的创建顺序是反的,传统代码是 Car 控制并创建了Framework,Framework 创建并创建了 Bottom,依次往下

而改进之后的控制权发生的反转,不再是使用方对象创建并控制依赖对象了,而是把依赖对象注入将当前对象中,依赖对象的控制权不再由当前类控制了.

这样的话, 即使依赖类发生任何改变,当前类都是不受影响的,这就是典型的控制反转,也就是 IoC 的实现思想,学到这里, 我们大概就知道了什么是控制反转了, 那什么是控制反转容器呢, 也就是IoC容器

在这里插入图片描述
这部分代码, 就是 IoC 容器做的工作,Spring 就是⼀种 IoC 容器, 帮助我们来做了这些资源管理,从上面也可以看出来, IoC 容器具备以下优点:

  1. 第⼀,资源集中管理,实现资源的可配置和易管理。

IoC容器会帮我们管理⼀些资源(对象等), 我们需要使⽤时, 只需要从IoC容器中去取就可以了

  1. 第⼆,降低了使用资源双方的依赖程度,也就是我们说的耦合度。

所以我们在创建实例的时候不需要了解其中的细节,

1.3 DI 介绍

DI: Dependency Injection(依赖注⼊),容器在运行期间, 动态的为应用程序提供运行时所依赖的资源,称之为依赖注入,程序运行时需要某个资源,此时容器就为其提供这个资源.

从这点来看, 依赖注入(DI)和控制反转(IoC)是从不同的角度的描述的同⼀件事情,就是指通过引⼊ IoC 容器,利用依赖关系注入的方式,实现对象之间的解耦。

上述代码中, 是通过构造函数的方式, 把依赖对象注入到需要使用的对象中的

在这里插入图片描述
IoC 是⼀种思想,思想只是⼀种指导原则,最终还是要有可行的落地方案,而 DI 就属于具体的实现。所以也可以说 DI 是 IoC 的⼀种实现.

1.4 IoC & DI 使⽤

对 IoC 和 DI 有了初步的了解, 我们接下来具体学习 Spring IoC 和 DI 的代码实现.

既然 Spring 是⼀个 IoC(控制反转)容器,作为容器, 那么它就具备两个最基础的功能:

Spring 容器管理的主要是对象, 这些对象, 我们称之为 “Bean”. 我们把这些对象交由 Spring 管理, 由 Spring 来负责对象的创建和销毁. 我们程序只需要告诉 Spring , 哪些需要存, 以及如何从 Spring 中取出对象

目标:

把 BookDao, BookService 交给 Spring 管理, 完成 Controller 层, Service 层, Dao 层的解耦

步骤:

  1. Service 层及 Dao 层的实现类,交给 Spring 管理: 使用注解: @Component
  2. 在Controller 层 和 Service 层 注入运行时依赖的对象: 使用注解 @Autowired

实现:

  1. 把 BookDao 交给 Spring 管理, 由 Spring 来管理对象
@Component
public class BookDao {
    /**
     * 数据Mock 获取图书信息
     *
     * @return
     */
    public List<BookInfo> mockData() {
        List<BookInfo> books = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            BookInfo book = new BookInfo();
            book.setId(i);
            book.setBookName("书籍" + i);
            book.setAuthor("作者" + i);
            book.setCount(i * 5 + 3);
            book.setPrice(new BigDecimal(new Random().nextInt(100)));
            book.setPublish("出版社" + i);
            book.setStatus(1);
            books.add(book);
        }
        return books;
    }
}
  1. 把BookService 交给 Spring 管理, 由 Spring 来管理对象
@Component
public class BookService {
    private BookDao bookDao = new BookDao();
    public List<BookInfo> getBookList() {
        List<BookInfo> books = bookDao.mockData();
        for (BookInfo book : books) {
            if (book.getStatus() == 1) {
                book.setStatusCN("可借阅");
            } else {
                book.setStatusCN("不可借阅");
            }
        }
        return books;
    }
}
  1. 删除创建 BookDao 的代码, 从 Spring 中获取对象
@Component
public class BookService {
    @Autowired
    private BookDao bookDao;
    public List<BookInfo> getBookList() {
        List<BookInfo> books = bookDao.mockData();
        for (BookInfo book : books) {
            if (book.getStatus() == 1) {
                book.setStatusCN("可借阅");
            } else {
                book.setStatusCN("不可借阅");
            }
        }
        return books;
    }
}
  1. 删除创建 BookService 的代码, 从 Spring 中获取对象
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/getList")
    public List<BookInfo> getList(){
//获取数据
        List<BookInfo> books = bookService.getBookList();
        return books;
    }
}
  1. 重新运行程序, http://127.0.0.1:8080/book_list.html

在这里插入图片描述

1.5 IoC 详解

通过上面的案例, 我们已经知道了 Spring IoC 和 DI 的基本操作, 接下来我们来系统的学习 Spring IoC 和 DI 的操作.

前面我们提到 IoC 控制反转,就是将对象的控制权交给 Spring 的 IOC 容器,由 IOC 容器创建及管理对象。也就是 bean 的存储.

1.5.1 Bean的存储

在之前的入门案例中,要把某个对象交给IOC容器管理,需要在类上添加⼀个注解:

  • @Component

⽽ Spring 框架为了更好的服务 web 应用程序, 提供了更丰富的注解.

共有两类注解类型可以实现:

  1. 类注解:@Controller、@Service、@Repository、@Component、@Configuration.
  2. 方法注解:@Bean.

接下来我们分别来看

1.5.1.1 @Controller(控制器存储)

使用 @Controller 存储 bean 的代码如下所示:

@Controller // 将对象存储到 Spring 中
public class UserController {
    public void sayHi(){
        System.out.println("hi,UserController...");
    }
}

如何观察这个对象已经存在 Spring 容器当中了呢,接下来我们学习如何从 Spring 容器中获取对象

@SpringBootApplication
public class SpringIocDemoApplication {
    public static void main(String[] args) {
		//获取Spring上下⽂对象
        ApplicationContext context = SpringApplication.run(SpringIocDemoApplicatio
		//从Spring上下⽂中获取对象
        UserController userController = context.getBean(UserController.class);
		//使⽤对象
        userController.sayHi();
    }
}

ApplicationContext 翻译过来就是: Spring 上下文,因为对象都交给 Spring 管理了,所以获取对象要从 Spring 中获取,自然就得先得到 Spring 的上下文

观察运行结果, 发现成功从 Spring 中获取到 Controller 对象, 并执行 Controller 的 sayHi 方法

在这里插入图片描述

如果把 @Controller 删掉, 再观察运行结果

在这里插入图片描述
报错信息显示: 找不到类型是:com.example.demo.controller.UserController的 bean

1.5.1.2 获取 bean 对象的其他方式

上述代码是根据类型来查找对象, 如果 Spring 容器中, 同⼀个类型存在多个 bean 的话, 怎么来获取呢?

ApplicationContext 获取 bean 对象的功能, 是父类 BeanFactory 提供的

public interface BeanFactory {
    //以上省略...
	// 1. 根据bean名称获取bean
    Object getBean(String var1) throws BeansException;
    // 2. 根据bean名称和类型获取bean
    <T> T getBean(String var1, Class<T> var2) throws BeansException;
    // 3. 按bean名称和构造函数参数动态创建bean,只适⽤于具有原型(prototype)作⽤域的bean
    Object getBean(String var1, Object... var2) throws BeansException;
    // 4. 根据类型获取bean
    <T> T getBean(Class<T> var1) throws BeansException;
    // 5. 按bean类型和构造函数参数动态创建bean, 只适⽤于具有原型(prototype)作⽤域的bean
    <T> T getBean(Class<T> var1, Object... var2) throws BeansException;
//以下省略...
}

常⽤的是上述1,2,4种, 这三种⽅式,获取到的 bean 是⼀样的,其中1,2种都涉及到根据名称来获取对象. bean 的名称是什么呢

Spring bean 是 Spring 框架在运行时管理的对象, Spring 会给管理的对象起⼀个名字,根据Bean的名称(BeanId)就可以获取到对应的对象.

1.5.1.3 Bean 命名约定

程序开发人员不需要为 bean 指定名称(BeanId), 如果没有显式的提供名称(BeanId), Spring 容器将为该 bean 生成唯⼀的名称,命名约定 bean 名称以小写字母开头,然后使用驼峰式大小写.

比如:

  • 类名: UserController, Bean的名称为: userController
  • 类名: AccountManager, Bean的名称为: accountManager
  • 类名: AccountService, Bean的名称为: accountService

也有⼀些特殊情况, 当有多个字符并且第⼀个和第⼆个字符都是大写时, 将保留原始的大小写,比如

  • 类名: UController, Bean的名称为: UController
  • 类名: AManager, Bean的名称为: AManager

根据这个命名规则, 我们来获取Bean.

@SpringBootApplication
public class SpringIocDemoApplication {
    public static void main(String[] args) {
		//获取Spring上下⽂对象
        ApplicationContext context = SpringApplication.run(SpringIocDemoApplicatio
		//从Spring上下⽂中获取对象
		//根据bean类型, 从Spring上下⽂中获取对象
        UserController userController1 = context.getBean(UserController.class);
		//根据bean名称, 从Spring上下⽂中获取对象
        UserController userController2 = (UserController) context.getBean("userController");
		//根据bean类型+名称, 从Spring上下⽂中获取对象
        UserController userController3 = context.getBean("userController", UserController.class);
        System.out.println(userController1);
        System.out.println(userController2);
        System.out.println(userController3);
    }
}

运行结果:

在这里插入图片描述
地址⼀样, 说明对象是⼀个

1.5.1.4 @Service(服务存储)

使⽤ @Service 存储 bean 的代码如下所⽰:

@Service
public class UserService {
    public void sayHi(String name) {
        System.out.println("Hi," + name);
    }
}

读取 bean 的代码:

@SpringBootApplication
public class SpringIocDemoApplication {
    public static void main(String[] args) {
		//获取Spring上下⽂对象
        ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
		//从Spring中获取UserService对象
        UserService userService = context.getBean(UserService.class);
		//使⽤对象
        userService.sayHi();
    }
}

观察运行结果, 发现成功从 Spring 中获取到 UserService 对象, 并执行 UserService 的sayHi 方法

在这里插入图片描述

1.5.1.5 @Repository(仓库存储)

使⽤ @Repository 存储 bean 的代码如下所⽰:

@Repository
public class UserRepository {
    public void sayHi() {
        System.out.println("Hi, UserRepository~");
    }
}

读取 bean 的代码:

@SpringBootApplication
public class SpringIocDemoApplication {
    public static void main(String[] args) {
		//获取Spring上下⽂对象
        ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
		//从Spring上下⽂中获取对象
        UserRepository userRepository = context.getBean(UserRepository.class);
		//使⽤对象
        userRepository.sayHi();
    }
}

观察运行结果, 发现成功从 Spring 中获取到 UserRepository 对象, 并执行 UserRepository 的 say 方法

在这里插入图片描述

1.5.1.6 @Component(组件存储)

使⽤ @Component 存储 bean 的代码如下所示:

@Component
public class UserComponent {
    public void sayHi() {
        System.out.println("Hi, UserComponent~");
    }
}

读取 bean 的代码:

@SpringBootApplication
public class SpringIocDemoApplication {
    public static void main(String[] args) {
		//获取Spring上下⽂对象
        ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
		//从Spring上下⽂中获取对象
        UserComponent userComponent = context.getBean(UserComponent.class);
		//使⽤对象
        userComponent.sayHi();
    }
}

观察运行结果, 发现成功从 Spring 中获取到 UserComponent 对象, 并执行 UserComponent 的 sayHi 方法

在这里插入图片描述

1.5.1.7 @Configuration(配置存储)

使⽤ @Configuration 存储 bean 的代码如下所⽰:

@Configuration
public class UserConfiguration {
    public void sayHi() {
        System.out.println("Hi,UserConfiguration~");
    }
}

读取 bean 的代码:

@SpringBootApplication
public class SpringIocDemoApplication {
    public static void main(String[] args) {
		//获取Spring上下⽂对象
        ApplicationContext context = SpringApplication.run(SpringIocDemoApplication.class, args);
		//从Spring上下⽂中获取对象
        UserConfiguration userConfiguration = context.getBean(UserConfiguration.cl
		//使⽤对象
                        userConfiguration.sayHi();
    }
}

观察运行结果, 发现成功从 Spring 中获取到 UserConfiguration 对象, 并执行 UserConfiguration 的 sayHi 方法

在这里插入图片描述

1.6 为什么要这么多类注解?

这个也是和咱们前面讲的应用分层是呼应的. 让程序员看到类注解之后,就能直接了解当前类的用途.

  • @Controller:控制层, 接收请求, 对请求进行处理, 并进行响应.
  • @Servie:业务逻辑层, 处理具体的业务逻辑.
  • @Repository:数据访问层,也称为持久层. 负责数据访问操作
  • @Configuration:配置层. 处理项目中的⼀些配置信息.

程序的应用分层,调用流程如下:

在这里插入图片描述
类注解之间的关系:查看 @Controller / @Service / @Repository / @Configuration 等注解的源码发现

在这里插入图片描述
其实这些注解里面都有⼀个注解 @Component ,说明它们本身就是属于 @Component 的 “子类”

@Component 是⼀个元注解,也就是说可以注解其他类注解,如 @Controller , @Service ,@Repository 等. 这些注解被称为 @Component 的衍生注解.

@Controller , @Service 和 @Repository 用于更具体的用例(分别在控制层, 业务逻辑层, 持久化层), 在开发过程中, 如果你要在业务逻辑层使用 @Component 或 @Service,显然@Service是更好的选择

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

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

相关文章

万兆以太网MAC设计(7)ARP协议报文格式详解以及ARP层模块设计

文章目录 前言&#xff1a;1、ARP协议详解2、ARP工作机制 二、ARP_RX模块设计三、ARP_TX模块设计四、ARP_table模块5、仿真5.1、发送端5.2、接收端5.3、缓存表 总结 前言&#xff1a; 1、ARP协议详解 ARP数据格式&#xff1a; 硬件类型:表示硬件地址的类型。它的值为1表示以太…

微信小程序使用echarts组件实现饼状统计图功能

微信小程序使用echarts组件实现饼状统计图功能 使用echarts实现在微信小程序中统计图的功能&#xff0c;具体的实现步骤思路可进我主页查看我的另一篇博文https://blog.csdn.net/weixin_45465881/article/details/138171153进行查看&#xff0c;本篇文章主要使用echarts组件实…

javaEE初阶——多线程(九)——JUC常见的类以及线程安全的集合类

T04BF &#x1f44b;专栏: 算法|JAVA|MySQL|C语言 &#x1faf5; 小比特 大梦想 此篇文章与大家分享多线程专题的最后一篇文章:关于JUC常见的类以及线程安全的集合类 如果有不足的或者错误的请您指出! 目录 3.JUC(java.util.concurrent)常见的类3.1Callable接口3.2 RentrantLoc…

Yolov5 v7.0目标检测——详细记录环境配置、自定义数据处理、模型训练与常用错误解决方法(数据集为河道漂浮物)

1. Yolov5 YOLOv5是是YOLO系列的一个延伸&#xff0c;其网络结构共分为&#xff1a;input、backbone、neck和head四个模块&#xff0c;yolov5对yolov4网络的四个部分都进行了修改&#xff0c;并取得了较大的提升&#xff0c;在input端使用了Mosaic数据增强、自适应锚框计算、自…

鸿蒙云函数调试坑点

如果你要本地调试请使用 const {payload, action} event.body/** 本地调试不需要序列化远程需要序列化 */ // const {payload, action} JSON.parse(event.body) const {payload, action} event.body 注意: 只要修改云函数&#xff0c;必须上传云函数 如果使用 const {pay…

牛客NC98 判断t1树中是否有与t2树完全相同的子树【simple 深度优先dfs C++/Java/Go/PHP】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/4eaccec5ee8f4fe8a4309463b807a542 思路 深度优先搜索暴力匹配 思路和算法这是一种最朴素的方法——深度优先搜索枚举 s 中的每一个节点&#xff0c;判断这个点的子树是否和 t 相等。如何判断一个节点的子树是否…

HTTP/1.1,HTTP/2.0和HTTP/3.0 各版本协议的详解(2024-04-24)

1、HTTP介绍 HTTP 协议有多个版本&#xff0c;目前广泛使用的是 HTTP/1.1 和 HTTP/2&#xff0c;以及正在逐步推广的 HTTP/3。 HTTP/1.1&#xff1a;支持持久连接&#xff0c;允许多个请求/响应通过同一个 TCP 连接传输&#xff0c;减少了建立和关闭连接的消耗。 HTTP/2&#…

基于STM32和阿里云的智能台灯(STM32+ESP8266+MQTT+阿里云+语音模块)

一、主要完成功能 1、冷光模式和暖光模式两种灯光 主要支持冷光和暖光模式两种&#xff0c;可以通过语音模块或手机app远程切换冷暖光 2、自动模式和手动模式 主要支持手动模式和自动两种模式&#xff08;app或语音助手切换&#xff09; (1)自动模式&#xff1a;根据环境光照…

vscode 使用文件模板功能来添加版权信息

vscode 新建文件的时候&#xff0c;自动填充作者及版权信息 无需使用插件&#xff0c;操作如下&#xff1a; 选择 “首选项(Preferences)”。在搜索框中输入 “file template” 或者 “文件模板”&#xff0c;然后选择相关的设置项。 {"C_Cpp.clang_format_fallbackSt…

[lesson58]类模板的概念和意义

类模板的概念和意义 类模板 一些类主要用于存储和组织数据元素 类中数据组织的方式和数据元素的具体类型无关 如&#xff1a;数组类、链表类、Stack类、Queue类等 C中将模板的思想应用于类,使得类的实现不关注数据元素的具体类型,而只关注类所需要实现的功能。 C中的类模板…

Docker 开启远程安全访问

说明 如果你的服务器是公网IP&#xff0c;并且开放了docker的远程访问&#xff0c;如果没有进行保护是非常危险的&#xff0c;任何人都可以向你的docker中推送镜像、运行实例。我曾开放过阿里云服务器中docker的远程访问权限&#xff0c;在没有开启保护的状态下&#xff0c;几…

企业微信hook接口协议,根据手机号搜索联系人

根据手机号搜索联系人 参数名必选类型说明uuid是String每个实例的唯一标识&#xff0c;根据uuid操作具体企业微信 请求示例 {"uuid":"3240fde0-45e2-48c0-90e8-cb098d0ebe43","phoneNumber":"1357xxxx" } 返回示例 {"data&q…

抖音 小程序 获取手机号 报错 getPhoneNumber:fail auth deny

这是因为 当前小程序没有获取 手机号的 权限 此能力仅支持小程序通过试运营期后可用&#xff0c;默认获取权限&#xff0c;无需申请&#xff1b; https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/guide/open-capabilities/acquire-phone-number-acqu…

用斐波那契数列感受算法的神奇(21亿耗时0.2毫秒)

目录 一、回顾斐波那契数列 二、简单递归方法 &#xff08;一&#xff09;解决思路 &#xff08;二&#xff09;代码展示 &#xff08;三&#xff09;性能分析 三、采用递归HashMap缓存 &#xff08;一&#xff09;解决思路 &#xff08;二&#xff09;代码展示 &…

深度学习系列65:数字人openHeygen详解

1. 主流程分析 从inference.py函数进入&#xff0c;主要流程包括&#xff1a; 1&#xff09; 使用cv2获取视频中所有帧的列表&#xff0c;如下&#xff1a; 2&#xff09;定义Croper。核心代码为69行&#xff1a;full_frames_RGB, crop, quad croper.crop(full_frames_RGB)。…

公开课学习——基于索引B+树精准建立高性能索引

文章目录 遇到慢查询怎么办&#xff1f;—— 创建索引联合索引的底层的数据存储结构长什么样&#xff1f; mysql脑图 阿里开发手册 遇到慢查询怎么办&#xff1f;—— 创建索引 不用索引的话一个一个找太慢了&#xff0c;用索引就快的多。 假如使用树这样的结构建立索引&#x…

Spring - 3 ( 12000 字 Spring 入门级教程 )

一&#xff1a;Spring Web MVC入门 1.1 响应 在我们前⾯的代码例子中&#xff0c;都已经设置了响应数据, Http 响应结果可以是数据, 也可以是静态页面&#xff0c;也可以针对响应设置状态码, Header 信息等. 1.2 返回静态页面 创建前端页面 index.html(注意路径) html代码 …

frp 实现 http / tcp 内网穿透(穿透 wordpress )

frp 实现 http / tcp 内网穿透&#xff08;穿透 wordpress &#xff09; 1. 背景简介与软件安装2. 服务端配置2.1 配置文件2.2 wordpress 配置文件2.3 frps 自启动 3.客户端配置3.1 配置文件3.2 frpc 自启动 同步发布在个人笔记frp 实现 http / tcp 内网穿透&#xff08;穿透 w…

HZNUCTF -- web

HZNUCTF第五届校赛实践赛初赛 Web方向 WriteUp-CSDN博客 ezssti 下载文件 访问 /login 可由源代码中看到 Eval 函数 &#xff0c;可以任意命令执行 按照格式&#xff0c;可执行命令 POST &#xff1a;name{{.Eval "env"}} 可以得到flag &#xff08;尝试ls 只能列出…

就业班 第三阶段(负载均衡) 2401--4.19 day3

二、企业 keepalived 高可用项目实战 1、Keepalived VRRP 介绍 keepalived是什么keepalived是集群管理中保证集群高可用的一个服务软件&#xff0c;用来防止单点故障。 ​ keepalived工作原理keepalived是以VRRP协议为实现基础的&#xff0c;VRRP全称Virtual Router Redundan…