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

Spring boot如何工作

越来越方便了

 java技术生态发展近25年,框架也越来越方便使用了,简直so easy!!!我就以Spring衍生出的Spring boot做演示,Spring boot会让你开发应用更快速。

快速启动spring boot 请参照官网 Spring | Quickstart

代码如下:

@SpringBootApplication
@RestController
public class SpringBootTestApplication {public static void main(String[] args) {SpringApplication.run(SpringBootTestApplication.class, args);}@GetMapping("/hello")public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {return String.format("Hello %s!", name);}
}

注maven的pom文件 (部分):

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope>
</dependency>

运行

通过浏览器方法http://localhost:8080/hello

至此一个简单的spring boot应用开发完毕,要是公司企业的展示性网站用这种方式极快。

我们重点关注一下这个@SpringBootApplication注解,就是因为它,程序运行主类,相当于跑了一个tomcat中间件,既然能通过web访问,说明是一个web应用程序。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
/*** Exclude specific auto-configuration classes such that they will never be applied.* @return the classes to exclude*/@AliasFor(annotation = EnableAutoConfiguration.class)Class<?>[] exclude() default {};/*** Exclude specific auto-configuration class names such that they will never be* applied.* @return the class names to exclude* @since 1.3.0*/@AliasFor(annotation = EnableAutoConfiguration.class)String[] excludeName() default {};/*** Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}* for a type-safe alternative to String-based package names.* <p>* <strong>Note:</strong> this setting is an alias for* {@link ComponentScan @ComponentScan} only. It has no effect on {@code @Entity}* scanning or Spring Data {@link Repository} scanning. For those you should add* {@link org.springframework.boot.autoconfigure.domain.EntityScan @EntityScan} and* {@code @Enable...Repositories} annotations.* @return base packages to scan* @since 1.3.0*/@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")String[] scanBasePackages() default {};/*** Type-safe alternative to {@link #scanBasePackages} for specifying the packages to* scan for annotated components. The package of each class specified will be scanned.* <p>* Consider creating a special no-op marker class or interface in each package that* serves no purpose other than being referenced by this attribute.* <p>* <strong>Note:</strong> this setting is an alias for* {@link ComponentScan @ComponentScan} only. It has no effect on {@code @Entity}* scanning or Spring Data {@link Repository} scanning. For those you should add* {@link org.springframework.boot.autoconfigure.domain.EntityScan @EntityScan} and* {@code @Enable...Repositories} annotations.* @return base packages to scan* @since 1.3.0*/@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")Class<?>[] scanBasePackageClasses() default {};......略了
}

发现@SpringBootApplication注解是一个组合型注解,有3个Spring annotations:@SpringBootConfiguration, @ComponentScan, and @EnableAutoConfiguration ,记住@EnableAutoConfiguration注解很重要,其次@ComponentScan注解。

@EnableAutoConfiguration注解,spring boot 会基于classpath路径的jar、注解和配置信息,会自动配置我们的应用程序,所有的这些注解会帮助spring boot 自动配置我们的应用程序,我们不必担心配置它们。我演示的代码,spring boot会检查classpath路径,由于有依赖spring-boot-starter-web,Spring boot会把项目配置成web应用项目,另外Spring Boot将把它的HelloController当作一个web控制器,而且由于我的应用程序依赖于Tomcat服务器(spring-boot-starter-tomcat),Spring Boot也将使用这个Tomcat服务器来运行我的应用程序。

特别注意,框架里是否装载Bean会配合条件表达式一起使用

 真正使用时是混合使用的

@EnableAutoConfiguration代码:

package org.springframework.boot.autoconfigure;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";/*** Exclude specific auto-configuration classes such that they will never be applied.* @return the classes to exclude*/Class<?>[] exclude() default {};/*** Exclude specific auto-configuration class names such that they will never be* applied.* @return the class names to exclude* @since 1.3.0*/String[] excludeName() default {};}

又有两个需要理解的元注解meta-annotation : @AutoConfigurationPackage 和@Import(AutoConfigurationImportSelector.class)

@AutoConfigurationPackage代码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {String[] basePackages() default {};Class<?>[] basePackageClasses() default {};
}

这个注解又用了一个@Import, 它的value值是:  AutoConfigurationPackages.Registrar.class.,而

AutoConfigurationPackages.Registrar 实现ImportBeanDefinitionRegistrar。还记得我以前写的博文Spring Bean注册的几种方式Spring Bean注册的几种方式_filterregistrationbean beandefinitionregistrypostp_董广明的博客-CSDN博客吗

@Import(AutoConfigurationImportSelector.class)

这个注解是自动配置机制,AutoConfigurationImportSelector实现了 DeferredImportSelector.

这个选择器的实现使用spring core功能方法:SpringFactoriesLoader.loadFactoryNames() ,该方法从META-INF/spring.factories加载配置类

而这个引导配置类从spring-boot-autoconfigure-2.3.0.RELEASE-sources.jar!/META-INF/spring.factories文件加载,该文件有键org.springframework.boot.autoconfigure.EnableAutoConfiguration

注意spring引导自动配置模块隐式包含在所有引导应用程序中。

参考:

  1. SpringBoot中@EnableAutoConfiguration注解的作用  SpringBoot中@EnableAutoConfiguration注解的作用_51CTO博客_@enableautoconfiguration注解报错

  2. Talking about how Spring Boot works  Talking about how Spring Boot works - Huong Dan Java

  3. How Spring Boot Works? Spring Boot Rock’n’Roll -王福强的个人博客:一个架构士的思考与沉淀
  4.  How Spring Boot auto-configuration works  How Spring Boot auto-configuration works | Java Development Journal
  5. Spring Boot - How auto configuration works? https://www.logicbig.com/tutorials/spring-framework/spring-boot/auto-config-mechanism.html
  6. SpringBoot 启动原理  SpringBoot 启动原理 | Server 运维论坛

  7. How SpringBoot AutoConfiguration magic works? https://www.sivalabs.in/how-springboot-autoconfiguration-magic/

  8. @EnableAutoConfiguration Annotation in Spring Boot @EnableAutoConfiguration Annotation in Spring Boot

相关文章:

Spring boot如何工作

越来越方便了 java技术生态发展近25年&#xff0c;框架也越来越方便使用了&#xff0c;简直so easy&#xff01;&#xff01;&#xff01;我就以Spring衍生出的Spring boot做演示&#xff0c;Spring boot会让你开发应用更快速。 快速启动spring boot 请参照官网 Spring | Quic…...

代码随想录打卡—day45—【DP】— 8.29 完全背包应用

1 70. 爬楼梯&#xff08;完全背包版&#xff09; 70. 爬楼梯 完全背包装满的选法排列的套路&#xff0c;AC代码&#xff1a; class Solution { public:/*完全背包的思路:1 2是两个物体 可以无限取*/int dp[50]; // 能爬到第i楼的选法的排列数/*dp[j] dp[j - i];dp[0] 1fo…...

2023.8.28日论文阅读

文章目录 NestFuse: An Infrared and Visible Image Fusion Architecture based on Nest Connection and Spatial/Channel Attention Models(2020的论文)本文方法 LRRNet: A Novel Representation Learning Guided Fusion Network for Infrared and Visible Images本文方法学习…...

HAproxy(四十七)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 前言 一、概述 1.1 简介 1.2 核心功能 1.3 关键特性 1.4 应用场景 二、安装 1.内核配置 2.编译安装 ​3. 建立配置文件 4. 添加为系统服务 5. 添加3和5运行级别下自启动…...

Java实战场景下的ElasticSearch

文章目录 前言一、环境准备二、RsetAPI操作索引库1.创建索引库2.判断索引库是否存在3.删除索引库 二、RsetAPI操作文档1.新增文档2.单条查询3.删除文档4.增量修改5.批量导入6.自定义响应解析方法 四、常用的查询方法1.MatchAll():查询所有2.matchQuery():单字段查询3.multiMatc…...

拓世科技集团 | “书剑人生”李步云学术思想研讨会暨李步云先生九十华诞志庆

2023年&#xff0c;中国改革开放迎来了45周年&#xff0c;改革春风浩荡&#xff0c;席卷神州大地&#xff0c;45年间&#xff0c;中国特色社会主义伟大事业大步迈入崭新境界&#xff0c;一路上结出了饶为丰硕的果实。中华民族在这45年间的砥砺前行&#xff0c;不仅使中国的经济…...

前端须知名词解释

目录 一、多维转一维 二、一维转多维 一维转多维——使用场景&#xff1a;分页 三、判断当前元素是否为数组 四、判断当前元素是否是空对象 五、数字分割符&#xff1a;提高数字可读性 六、模糊盒子&#xff08;怪异盒子&#xff09;与标准盒模型 七、css的filter属性 …...

React性能优化之memo缓存函数

React是一个非常流行的前端框架&#xff0c;但是在处理大型应用程序时&#xff0c;性能可能会成为一个问题。为了解决这个问题&#xff0c;React提供了一个称为memo的功能&#xff0c;它可以缓存函数并避免不必要的重新渲染。 memo是React中的一个高阶组件&#xff08;HOC&…...

2023年高教社杯 国赛数学建模思路 - 案例:ID3-决策树分类算法

文章目录 0 赛题思路1 算法介绍2 FP树表示法3 构建FP树4 实现代码 建模资料 0 赛题思路 &#xff08;赛题出来以后第一时间在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 1 算法介绍 FP-Tree算法全称是FrequentPattern Tree算法&#xff0c;就是频繁模…...

C# Emgu.CV 条码检测

效果 项目 代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.Util; using static Emgu.C…...

VueRouter的基本使用

路由的基本使用 文章目录 路由的基本使用01-VueRouterVueRouter的使用 &#xff08; 5 2&#xff09;综合代码 拓展&#xff1a;组件存放问题 什么是路由呢&#xff1f; 在生活中的路由&#xff1a;设备和IP的映射关系 在Vue中&#xff1a;路径 和 组件 的 映射 关系。 01-Vu…...

网工笔记:快速认识7类逻辑接口

逻辑接口是指能够实现数据交换功能但物理上不存在、需要通过配置建立的接口。逻辑接口需要承担业务传输。 下面是我整理了7款常见的逻辑接口。 接口类型 描述 Eth-Trunk接口 具有二层特性和三层特性的逻辑接口&#xff0c;把多个以太网接口在逻辑上等同于一个逻辑接口&…...

MySQL中的free链表,flush链表,LRU链表

一、free链表 1、概述 free链表是一个双向链表数据结构&#xff0c;这个free链表里&#xff0c;每个节点就是一个空闲的缓存页的描述数据块的地址&#xff0c;也就是说&#xff0c;只要你一个缓存页是空闲的&#xff0c;那么他的描述数据块就会被放入这个free链表中。 刚开始数…...

mac使用VsCode远程连接服务器总是自动断开并要求输入密码的解决办法

在mac中使用vscode远程连接服务器&#xff0c;时常会出现自动断开并要求重新输入服务器密码的问题&#xff0c;接下来让我们来解决它&#xff1a; 1、首先&#xff0c;在本地创建公钥&#xff1a; ssh-keygen 这条命令执行之后&#xff0c;出现提示直接回车即可&#xff1b;直…...

Python爬虫分布式架构 - Redis/RabbitMQ工作流程介绍

在大规模数据采集和处理任务中&#xff0c;使用分布式架构可以提高效率和可扩展性。本文将介绍Python爬虫分布式架构中常用的消息队列工具Redis和RabbitMQ的工作流程&#xff0c;帮助你理解分布式爬虫的原理和应用。 为什么需要分布式架构&#xff1f; 在数据采集任务中&#…...

【ES】笔记-集合介绍与API

集合是一种不允许值重复的顺序数据结构。 通过集合我们可以进行并集、交集、差集等数学运算&#xff0c; 还会更深入的理解如何使用 ECMAScript 2015(ES2015)原生的 Set 类。 构建数据集合 集合是由一组无序且唯一(即不能重复)的项组成的。该数据结构使用了与有限集合相同的数…...

Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分离)【五】

&#x1f600;前言 本篇博文是关于Spring Boot(Vue3ElementPlusAxiosMyBatisPlusSpring Boot 前后端分离)【五】&#xff0c;希望你能够喜欢 &#x1f3e0;个人主页&#xff1a;晨犀主页 &#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是晨犀&#xff0c;希望我的文章…...

二、Tomcat 安装集

一、Tomcat—Docker 1. 拉取镜像 # 1、拉取镜像&#xff08;tomcat版本8&#xff0c;jre版本8&#xff09;。 docker pull tomcat:8-jre82. 启动容器 # 2、启动一个tomcat容器。 docker run -id --name tomcat -p 8080:8080 镜像ID # 3、宿主机里新建/root/tomcat目录&#x…...

CentOS 上通过 NFS 挂载远程服务器硬盘

NFS&#xff08;Network File System&#xff09;是一种用于在不同的计算机系统之间共享文件和目录的协议。它允许一个计算机系统将其文件系统的一部分或全部内容暴露给其他计算机系统&#xff0c;使其能够像访问本地文件一样访问这些内容。在这篇博客中&#xff0c;我们将介绍…...

微信小程序中的 广播监听事件

定义 WxNotificationCenter.js 文件&#xff1b; /*** author: Di (微信小程序开发工程师)* organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)* 垂直微信小程序开发交流社区* * github地址: https://github.com/icindy/WxNotificationCenter…...

Quickstart: MinIO for Linux

单节点部署教程 1.安装Minio服务端 //wget下载二进制文件 wget https://dl.min.io/server/minio/release/linux-amd64/minio //赋予权限 chmod x minio //将minio可执行文件移入usr/local/bin目录下&#xff0c;使得minio可以全局执行 sudo mv minio /usr/local/bin/ 2.启动Mi…...

Java中word转Pdf工具类

背景&#xff1a; 最近做的一个项目中&#xff0c;对于word转Pdf用的地方很多&#xff0c;特此记录 搭建总图&#xff1a; 代码部分&#xff1a; 1.需要的jar包&#xff1a; aspose-words-15.8.0-jdk16.jar 注&#xff1a;下载好这个jar包后&#xff0c;在项目的根目录新建一…...

【conda install】网络慢导致报错CondaHTTPError: HTTP 000 CONNECTION FAILED for url

⭐⭐问题&#xff1a; 部署安装环境经常会出现由于网络慢问题&#xff0c;导致conda安装不了库&#xff0c;报错如下&#xff1a; Solving environment: failedCondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/…...

2023-8-28 图中点的层次(树与图的广度优先遍历)

题目链接&#xff1a;图中点的层次 #include <iostream> #include <cstring> #include <algorithm>using namespace std;const int N 100010;int h[N], e[N], ne[N], idx; int n, m; int q[N], d[N];void add(int a, int b) {e[idx] b, ne[idx] h[a], h…...

设计模式(一)

1、适配器模式 &#xff08;1&#xff09;概述 适配器中有一个适配器包装类Adapter&#xff0c;其包装的对象为适配者Adaptee&#xff0c;适配器作用就是将客户端请求转化为调用适配者中的接口&#xff1b;当调用适配器中的方法时&#xff0c;适配器内部会调用适配者类的方法…...

Prometheus关于微服务的监控

在微服务架构下随着服务越来越多,定位问题也变得越来越复杂,因此监控服务的运行状态以及针对异常状态及时的发出告警也成为微服务治理不可或缺的一环。服务的监控主要有日志监控、调用链路监控、指标监控等几种类型方式,其中指标监控在整个微服务监控中比重最高,也是实际生…...

CSS实现白天/夜晚模式切换

目录 功能介绍 示例 原理 代码 优化 总结 功能介绍 在网页设计和用户体验中&#xff0c;模式切换功能是一种常见的需求。模式切换可以为用户提供不同的界面外观和布局方案&#xff0c;以适应其个人偏好或特定环境。在这篇博客中&#xff0c;我们将探索如何使用纯CSS实现一…...

selenium实现输入数字字母验证码

思路 1. 登录url 2. 获取验证码坐标 3. 根据桌标截图验证码 4. 对验证码进行识别 5. 自动输入验证码 测试代码 import os import time from io import BytesIO from PIL import Image from selenium import webdriver from selenium.webdriver.common.by import By impo…...

Docker的运用

文章目录 一、 Docker介绍二、Docker常用命令三、Docker 部署微服务项目四、Docker 使用场景五、Docker模拟场景5.1 模拟部署Nacos5.2 模拟部署Mongodb5.3 模拟部署RabbitMQ 一、 Docker介绍 Docker是一种开源软件平台&#xff0c;用于在不同的操作系统&#xff08;如Windows、…...

在项目中快速搭建机器学习的流程

在软件开发领域&#xff0c;机器学习框架发挥着关键作用&#xff0c;为开发人员提供强大的人工智能工具、库和算法&#xff0c;以有效地利用机器学习的潜力。从本质上讲&#xff0c;机器学习使计算机能够从数据中学习并做出预测或决策&#xff0c;而无需明确编程。 机器学习框…...