Spring boot如何工作

news2024/9/27 19:19:44

越来越方便了

 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

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

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

相关文章

开源与云计算:新的合作模式

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…

用QT实现MVP模式

近些天用qt 作项目,遇到参数界面.偷闲写个mvp模式示例. mvp模式重要的有两点 1 低耦合: 界面与后端数据类,不直接引用,可方便替换. 2 形成界面驱动-界面更新的闭环.:通过函数指针类技术,让数据自动回流. MVP (Model-View-Presenter) 视图&#xff08;View&#xff09;: 接…

本地私有仓库、harbor私有仓库部署与管理

本地私有仓库、harbor私有仓库部署与管理 一、本地私有仓库1.本地私有仓库简介2.搭建本地私有仓库3.容器重启策略介绍 二、harbor私有仓库部署与管理1.什么是harbor2.Harbor的特性3.Harbor的构成4.harbor部署及配置5.客户端测试 三、Harbor维护1.创建2.普通用户操作私有仓库3.日…

python进行数据分析:数据预处理

六大数据类型 见python基本功 import numpy as np import pandas as pd数据预处理 缺失值处理 float_data pd.Series([1.2, -3.5, np.nan, 0]) float_data0 1.2 1 -3.5 2 NaN 3 0.0 dtype: float64查看缺失值 float_data.isna()0 False 1 …

mysql57、mysql80 目录结构 之 Windows

查看mysql 数据存储的位置 /bin&#xff1a;存储可执行文件&#xff0c;主要包含客户端和服务端启动程序&#xff0c;如mysql.exe、mysqld.exe等 /docs&#xff1a;存放一些文档 /include&#xff1a;用于放置一些头文件&#xff0c;如&#xff1a;mysql.h、mysqld_error.h 等 …

Android SDK 上手指南||第七章 Java应用程序编程

第七章 Java应用程序编程 如果大家已经对Java非常熟悉&#xff0c;那么不妨直接忽略这部分内容。如果大家的技巧还存在局限或者对Java这种语言只闻其名&#xff0c;那么本文将为各位解答很多在Android开发当中经常遇到的问题。需要注意的是&#xff0c;这篇文章并不能作为Java…

容器技术,1. Docker,2. Kubernetes(K8s):

目录 容器技术 1. Docker&#xff1a; 2. Kubernetes&#xff08;K8s&#xff09;&#xff1a; Docker和Kubernetes 容器的主要应用场景有哪些&#xff1f; 容器技术 有效的将单个操作系统的资源划分到孤立的组中&#xff0c;以便更好的在孤立的组之间平衡有冲突的资源使…

【云原生】Docker的数据管理(数据卷、容器互联)

目录 一、数据卷&#xff08;容器与宿主机之间数据共享&#xff09; 二、数据卷容器&#xff08;容器与容器之间数据共享&#xff09; 三、 容器互联&#xff08;使用centos镜像&#xff09; 总结 用户在使用Docker的过程中&#xff0c;往往需要能查看容器内应用产生的数据…

Spring(aop介绍,底层实现,jdk代理,cglib代理)

02-aop简介-aop的作用及其优势_哔哩哔哩_bilibili 122 1、Spring的aop介绍 1.1aop是一种技术&#xff0c;aop是在运行之间执行的&#xff0c;他可以完成程序功能之间的松耦合&#xff0c;动态代理的作用也等同于Aop的作用&#xff1a;他提供了相应的封装&#xff0c;Aop是面向…

UG\NX二次开发 使用BlockUI设计对话框时,如何设置默认的开发语言?

文章作者:里海 来源网站:王牌飞行员_里海_里海NX二次开发3000例,C\C++,Qt-CSDN博客 简介: NX二次开发使用BlockUI设计对话框时,如何设置默认的代码语言? 效果: 方法: 依次打开“文件”->“实用工具”->“用户默认设置”->“用户界面”->“操作记录”->“…

如何进行微服务的集成测试

集成测试的概念 说到集成测试&#xff0c;相信每个测试工程师并不陌生&#xff0c;它不是一个崭新的概念&#xff0c;通过维基百科定义可以知道它在传统软件测试中的含义。 Integration testing (sometimes called integration and testing, abbreviated I&T) is the pha…

【C++精华铺】9.STL string

目录 1. string类的优势 2. string类的常用接口 2.1 常用构造 1. 空串构造&#xff1a;string(); 2. C串构造&#xff1a;string(const char* s); 3. 拷贝构造&#xff1a;string(const string& str); 4. 字符填充构造&#xff1a;string(size_t n, char c); 5. 迭代…

GDFN模块(restormer)

为了对特征进行变换&#xff0c;常规的前馈神经网络独立地在每个像素位置进行相同的操作。它使用两个1x1卷积层&#xff0c;一个用来扩展特征通道&#xff08;通常4倍&#xff09;&#xff0c;第二个用来将特征通道减少到原来的输入维度。在隐藏层中加入非线性。 GDFN做了两个…

嵌入式实时操作系统的设计与开发

时钟管理 在RTOS中&#xff0c;时钟具有非常重要的作用&#xff0c;通过时钟可实现延时任务、周期性触发任务执行、任务有限等待的计时。 大多数嵌入式系统有两种时钟源&#xff0c;分别为实时时钟RTC&#xff08;Real-Time Clock&#xff09;和定时器/计数器。 实时时钟一般…

jvm——内存模型

1.java内存模型 1.1 原子性 1.2 问题分析 这里与局部变量自增不同&#xff0c;局部变量调用iinc是在局部变量表槽位上进行自增。 静态变量是在操作数栈自增。 这里的主内存和工作内存时再JMM里的说法。 因为操作系统是时间片切换的多个线程轮流使用CPU. 1.3解决方法 JMM中…

2023京东酒类市场数据分析(京东数据开放平台)

根据鲸参谋平台的数据统计&#xff0c;今年7月份京东平台酒类环比集体下滑&#xff0c;接下来我们一起来看白酒、啤酒、葡萄酒的详情数据。 首先来看白酒市场。 鲸参谋数据显示&#xff0c;7月份京东平台白酒的销量为210万&#xff0c;环比下滑约49%&#xff1b;销售额将近19…

前端需要理解的数据结构与算法知识

1 数组 1.1 集合、列表、数组的联系与区别 集合&#xff1a;由一个或多个确定的元素所构成的整体。类型不一定相同、确定、无序、互异。 列表&#xff08;又称线性列表&#xff09;&#xff1a;按照一定的线性顺序&#xff0c;排列而成的数据项的集合。类型不一定相同、有序…

opencv实现全景图像拼接

目录 部分代码展示&#xff1a; 效果演示 查看处理过程 历史记录 完整演示视频&#xff1a; 完整代码链接 部分代码展示&#xff1a; 效果演示 查看处理过程 历史记录 完整演示视频&#xff1a; 无法粘贴视频........ 完整代码链接 视频和代码都已上传百度网盘&#x…

Kafka 简介 + 学习笔记

消息队列 先说明消息队列是什么&#xff1a; 亚马逊&#xff1a; 消息队列是一种异步的服务间通信方式&#xff0c;适用于微服务架构。消息在被处理和删除之前一直存储在队列上。每条消息仅可被一位用户处理一次。消息队列可被用于分离重量级处理、缓冲或批处理工作以及缓解高…

机器学习基础之《分类算法(5)—朴素贝叶斯算法原理》

一、朴素贝叶斯算法 1、什么是朴素贝叶斯分类方法 之前用KNN算法&#xff0c;分类完直接有个结果&#xff0c;但是朴素贝叶斯分完之后会出现一些概率值&#xff0c;比如&#xff1a; 这六个类别&#xff0c;它都有一定的可能性 再比如&#xff0c;对文章进行分类&#xff1a;…