SpringBoot《第二课》(一)

简介: SpringBoot《第二课》(一)

2022年9月5号 SpringBoot自动配置原理初步了解SpringBoot原理。

第一点解析Pom.xml文件信息下面是配置文件的具体信息。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <version>2.7.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>

第一张图:带你了解Pom.xml文件:

图一的流程来解析pom.xml文件的信息

让我们来看一下上面流程图的过程吧!这里涉及到源码

1 认真观察你会发现pom.xml文件中有两次的父依赖的关系

2  第一次父依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

3 第二次父依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.3</version>
  </parent>

4 spring-boot-dependencies 这个包在Maven的学习中见过吧

<groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.7.3</version>
  <packaging>pom</packaging>
  <name>spring-boot-dependencies</name>

5 进一步的深入了解一下

 <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>

6 第一次小的总结:spring-boot-dependencies 帮我们管理了SpringBoot开发环境中应用中所有应用中所有的依赖版本,解决了第三方库的冲突问题。

因此:spring-boot-dependencies称为 SpringBoot版本仲裁中心。

7 启动器的概念:   spring-boot-starter-xxx  简称为启动器

启动器:说白了又是SpringBoot的启动场景

比如 spring-boot-starter-web,他又会导入所有的web环境所有的依赖

8 springboot官网介绍的应用场景的地址 在下面

https://docs.spring.io/spring-boot/docs/current/reference/html/dependency-versions.html#appendix.dependency-versions

9 下面是一些Springboot应用场景在官网中找到的

image.png

第二张图:关于SpringApplication和Run方法的介绍:请看流程图.

/**
 * @ SpringBootApplication 标注的是一个SpringBoot的应用
 */
@SpringBootApplication //标记成SpringBoot启动类 启动类下面所有的包
public class Application {
    public static void main(String[] args) {
        //将SpringBoot应用启动
        //SpringApplication 类
        //run方法
      SpringApplication.run(Application.class,args);
    }
}
package org.springframework.boot;
public class SpringApplication {
    public static final String BANNER_LOCATION_PROPERTY_VALUE = "banner.txt";
    public static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";
    private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
    private static final Log logger = LogFactory.getLog(SpringApplication.class);
    static final SpringApplicationShutdownHook shutdownHook = new SpringApplicationShutdownHook();

上面是有关:SpringApplication类的介绍

package org.springframework.boot;
public class SpringApplication {
    public static final String BANNER_LOCATION_PROPERTY_VALUE = "banner.txt";
    public static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";
    private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
    private static final Log logger = LogFactory.getLog(SpringApplication.class);
    static final SpringApplicationShutdownHook shutdownHook = new SpringApplicationShutdownHook();
    private Set<Class<?>> primarySources;
    private Set<String> sources;
    private Class<?> mainApplicationClass;
    private Mode bannerMode;
    private boolean logStartupInfo;

上面是关于源码Run方法的介绍:关键在下面 new SpringApplication

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return (new SpringApplication(primarySources)).run(args);
    }

第二部分的总结

SpringApplication类的作用

这个类主要做了以下四件事情

  • 1.推断应用的类型是普通的项目还是Web项目
  • 2.查找并加载所有可用初始化器,设 置到initializers属性中.
  • 3 找出所有的应用程序监听器,设置到listeners属性中
  • 4.推断并设置main方法的定义类,找到运行的主类

Run方法

  • 1 判断项目类型
  • 2 推断当前主类
  • 3 设置监听器 获取上下文 全面接管SpringMvc配置
  • 4 找到运行主类


第三部分 @SpringBootApplication 一个注解才是SpringBoot的自动配置原理的核心。

SpringBoot自动配置原理核心@SpringBoot程序:

第一模块的学习  

package com.java.controller.com.java;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// @SpringBootApplication  SpringBoot启动类(入口)
//@Configuration Spring.xml 页是配置类
//ComponentScan  =<context:ComponentScan ></context-ComponentScan > 扫描包名
//Spring底层在解析配置类:回去解析@ComponentScan,读取basePackage
//如果没有读取到,会将当前配置类所在的包当成扫描包  package com.java.controller.com.java;
//位置:最好放在需要扫描的包的根目录下面,或者说放在所有Bean的项目中
/**
 * @ SpringBootApplication 标注的是一个SpringBoot的应用
 */
@SpringBootApplication //标记成SpringBoot启动类 启动类下面所有的包
public class Application {
    public static void main(String[] args) {
        //将SpringBoot应用启动
        //SpringApplication 类
        //run方法
      SpringApplication.run(Application.class,args);
    }
}

第一层  Application

package org.springframework.boot.autoconfigure;
@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 {}

注解:说明

@SpringBootConfiguration springboot的配置

@Configuration   spring配置类

@Component   说明这也是一个Spring组件

@EnableAutoConfiguration  自动配置

@AutoConfigurationPackage 自动配置包 导入了选择器

@Import({AutoConfigurationImportSelector.class}) 自动配置 包的注册

第二模块:SpringBootConfiguration 让程序进入

package org.springframework.boot;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

然后进入@Configuration

package org.springframework.context.annotation;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
    boolean proxyBeanMethods() default true;
}

最后进入到@Component中去你会发现下面内容

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.context.annotation;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
    boolean proxyBeanMethods() default true;
}
相关文章
|
3天前
|
缓存 Java 数据库
|
8月前
|
Prometheus 监控 安全
|
8月前
|
Java 数据库
SpringBoot笔记
SpringBoot笔记
39 0
|
10月前
|
Java 容器 Spring
SpringBoot《第二课》(二)
SpringBoot《第二课》(二)
61 0
|
10月前
|
前端开发 JavaScript Java
SpringBoot《第一课》
SpringBoot《第一课》
49 0
|
11月前
|
Java 数据库 Spring
基于SpringBoot的SSM整合案例 -- SpringBoot快速入门保姆级教程(四)
基于SpringBoot的SSM整合案例 -- SpringBoot快速入门保姆级教程(四)
QGS
|
XML 监控 Java
手把手初认Springboot2
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
QGS
55 0
|
Java 开发工具 Spring
05.SpringBoot开发技巧
05.SpringBoot开发技巧
|
前端开发 JavaScript Java
|
Java 测试技术 Maven
第十篇:SpringBoot 测试
第十篇:SpringBoot 测试
119 0
第十篇:SpringBoot 测试