springboot学习

参考视频 雷丰阳2021版SpringBoot2零基础入门springboot全套完整版(spring boot2)_哔哩哔哩_bilibili

spingboot其实自动配置了很多东西,参考spring-boot-starter-web-2.3.4RELEASE.pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- This module was also published with a richer model, Gradle metadata,  -->
  <!-- which should be used instead. Do not delete the following line which  -->
  <!-- is to indicate to Gradle or any Gradle module metadata file consumer  -->
  <!-- that they should prefer consuming it instead. -->
  <!-- do_not_remove: published-with-gradle-metadata -->
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.3.4.RELEASE</version>
  <name>spring-boot-starter-web</name>
  <description>Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container</description>
  <url>https://spring.io/projects/spring-boot</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>https://spring.io</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <issueManagement>
    <system>GitHub</system>
    <url>https://github.com/spring-projects/spring-boot/issues</url>
  </issueManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.3.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.3.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.3.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.9.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.9.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

可以看到其自动配置了tomcat和mvc,同时它还会在启动时自动帮你加载一些常用的Bean,用来应付web常用的场景,比如DispatcherServlet,viewResovler, charcterEncodingFilter等等

与此同时,springboot拥有默认的包结构,就是启动类所在的包内,类加载的时候会自动扫描该包及其子包

如果想要该表扫描路径,就要使用@SpringBootApplication(scanBasePackage = 'com.atguigu')

同时各种配置拥有默认值, 比如在application.properties里的spring.servlet.multipart.max-file-size,而且配置值最终会绑定到每个类上(对应类MultipartProperties),这个类会在容器中创建对象

而且spingboot所有的自动配置都在spring-boot-autoconfigure里面,查找路径是从spring-boot-starter-web-> spring-boot-starter -> spring-boot-autoconfigure,可以看到,有很多的自动配置类

源码学习

tomcat.exe 其实就是找到java main的方法,并运行

Tomcat tomcat = new Tomcat();
tomcat.setPort(9070);
tomcat.start();
tomcat.addServlet("/". "index");
Context c - tomcat

servelet如何注册到Tomcat:

1. 在web.xml 配置 <servlet>

2. @WebServlet("/index/xxx") servlet规范3.0以上

3. 调用tomcat的addServlet方法

 getServer().await

Springboot零配置原理

去掉web.xml(applicationContext.xml 和contextLoaderListener(完成spring容器初始化)),实现零配置

AnnotationConfigWebApplicationContext

MET-INF里面某个类的onStartup方法会被调用,这是servlet 3.0规范,可以去看Spring的官网,里面有很多范例代码

tomcat的docBase用于存储文件?

springboot导入的是beandefinition,

 new DispatcherServlet其实会对容器进行初始化,即调用ac.refresh方法

 也可以在后面加setServletContext,哎好乱

 啥,initializer规范

 Springboot的run方法,会一直调用到refresh,然后通过createServer,新建

@Override
	protected void onRefresh() {
		super.onRefresh();
		try {
			createWebServer();
		}
		catch (Throwable ex) {
			throw new ApplicationContextException("Unable to start web server", ex);
		}
	}

添加:

@Override
	public WebServer getWebServer(ServletContextInitializer... initializers) {
		if (this.disableMBeanRegistry) {
			Registry.disableRegistry();
		}
		Tomcat tomcat = new Tomcat();
		File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
		tomcat.setBaseDir(baseDir.getAbsolutePath());
		Connector connector = new Connector(this.protocol);
		connector.setThrowOnFailure(true);
		tomcat.getService().addConnector(connector);
		customizeConnector(connector);
		tomcat.setConnector(connector);
		tomcat.getHost().setAutoDeploy(false);
		configureEngine(tomcat.getEngine());
		for (Connector additionalConnector : this.additionalTomcatConnectors) {
			tomcat.getService().addConnector(additionalConnector);
		}
		prepareContext(tomcat.getHost(), initializers);
		return getTomcatWebServer(tomcat);
	}

看一看 DispatcherServletAutoConfiguration

 。。。。我头都麻了,这个依赖关系

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值