栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot(三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot(三)

1.POM文件解析 1.1父项目

项目需要依赖的父项目【spring‐boot‐starter‐parent】

【spring‐boot‐starter‐parent】依赖于【 spring-boot-dependencies】


    5.16.4
    2.7.7
    1.9.95
    2.19.1
    1.9.7
    3.21.0
    ......





  
    
      org.apache.activemq
      activemq-amqp
      ${activemq.version}
    
    
      org.apache.activemq
      activemq-blueprint
      ${activemq.version}
    
    
      org.apache.activemq
      activemq-broker
      ${activemq.version}
    
.......

【spring‐boot‐dependencies】项目实际是用来真正管理Spring Boot应用里面的所有依赖包。有了【spring‐boot‐dependencies】项目以后我们新建的SpringBoot项目所需要的依赖包导入就不要在再配置版本和元素导入依赖,从【spring‐boot‐dependencies】项目中继承就可以了。

1.2SpringBoot启动器


    org.springframework.boot
    spring-boot-starter


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-test
    test

注意命名规则:spring-boot-starter-xxxx  [SpingBoot的启动器]

1)Spring-boot-starter:这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。

2)Spring-boot-starter-web: 帮助我们岛屿web模块正常的运行所依赖的包

3)Spring-boot-starter-test: 帮我们导入但愿测试模块正常运行依赖包。

基本的启动器

Spring Boot应用启动器基本的一共有44种,具体如下:
1)spring-boot-starter:这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。
2)spring-boot-starter-actuator:帮助监控和管理应用。
3)spring-boot-starter-amqp:通过spring-rabbit来支持AMQP协议(Advanced Message Queuing Protocol)。
4)spring-boot-starter-aop:支持面向方面的编程即AOP,包括spring-aop和AspectJ。
5)spring-boot-starter-artemis:通过Apache Artemis支持JMS的API(Java Message Service API)。
6)spring-boot-starter-batch:支持Spring Batch,包括HSQLDB数据库。
7)spring-boot-starter-cache:支持Spring的Cache抽象。
8)spring-boot-starter-cloud-connectors:支持Spring Cloud Connectors,简化了在像Cloud Foundry或Heroku这样的云平台上连接服务。
9)spring-boot-starter-data-elasticsearch:支持ElasticSearch搜索和分析引擎,包括spring-data-elasticsearch。
10)spring-boot-starter-data-gemfire:支持GemFire分布式数据存储,包括spring-data-gemfire。
11)spring-boot-starter-data-jpa:支持JPA(Java Persistence API),包括spring-data-jpa、spring-orm、Hibernate。
12)spring-boot-starter-data-mongodb:支持MongoDB数据,包括spring-data-mongodb。
13)spring-boot-starter-data-rest:通过spring-data-rest-webmvc,支持通过REST暴露Spring Data数据仓库。
14)spring-boot-starter-data-solr:支持Apache Solr搜索平台,包括spring-data-solr。
15)spring-boot-starter-freemarker:支持FreeMarker模板引擎。
16)spring-boot-starter-groovy-templates:支持Groovy模板引擎。
17)spring-boot-starter-hateoas:通过spring-hateoas支持基于HATEOAS的RESTful Web服务。
18)spring-boot-starter-hornetq:通过HornetQ支持JMS。
19)spring-boot-starter-integration:支持通用的spring-integration模块。
20)spring-boot-starter-jdbc:支持JDBC数据库。
21)spring-boot-starter-jersey:支持Jersey RESTful Web服务框架。
22)spring-boot-starter-jta-atomikos:通过Atomikos支持JTA分布式事务处理。
23)spring-boot-starter-jta-bitronix:通过Bitronix支持JTA分布式事务处理。
24)spring-boot-starter-mail:支持javax.mail模块。
25)spring-boot-starter-mobile:支持spring-mobile。
26)spring-boot-starter-mustache:支持Mustache模板引擎。
27)spring-boot-starter-redis:支持Redis键值存储数据库,包括spring-redis。
28)spring-boot-starter-security:支持spring-security。
29)spring-boot-starter-social-facebook:支持spring-social-facebook
30)spring-boot-starter-social-linkedin:支持pring-social-linkedin
31)spring-boot-starter-social-twitter:支持pring-social-twitter
32)spring-boot-starter-test:支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。
33)spring-boot-starter-thymeleaf:支持Thymeleaf模板引擎,包括与Spring的集成。
34)spring-boot-starter-velocity:支持Velocity模板引擎。
35)spring-boot-starter-web:支持全栈式Web开发,包括Tomcat和spring-webmvc。
36)spring-boot-starter-websocket:支持WebSocket开发。
37)spring-boot-starter-ws:支持Spring Web Services。
Spring Boot应用启动器面向生产环境的还有2种,具体如下:
1)spring-boot-starter-actuator:增加了面向产品上线相关的功能,比如测量和监控。
2)spring-boot-starter-remote-shell:增加了远程ssh shell的支持。
最后,Spring Boot应用启动器还有一些替换技术的启动器,具体如下:
1)spring-boot-starter-jetty:引入了Jetty HTTP引擎(用于替换Tomcat)。
2)spring-boot-starter-log4j:支持Log4J日志框架。
3)spring-boot-starter-logging:引入了Spring Boot默认的日志框架Logback。
4)spring-boot-starter-tomcat:引入了Spring Boot默认的HTTP引擎Tomcat。
5)spring-boot-starter-undertow:引入了Undertow HTTP引擎(用于替换Tomcat)。

2.SpringBoot的主类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = "com.wangxing.springboot")
public class Springbootdemo1Application {
    public static void main(String[] args) {
        SpringApplication.run(Springbootdemo1Application.class, args);
    }
}

之所以说Springbootdemo1Application 是主类是因为它里面包含了主方法

 public static void main(String[] args) {}

在这个主方法中有

SpringApplication.run(Springbootdemo1Application.class, args);

SpringApplication 将一个典型的 Spring 应用启动的流程“模板化”(这里是动词),在没有特殊需求的情况下,默认模板化后的执行流程就可以满足需求了但有特殊需求也没关系,SpringApplication 在合适的流程结点开放了一系列不同类型的扩展点,我们可以通过这些扩展点对 SpringBoot 程序的启动和关闭过程进行扩展。

2.1通过SpringApplication设置修改启动图标

1)在resources文件夹中创建banner.txt

banner.txt中的内容
'##::::'##:'########:'##:::::::'##::::::::'#######::
 ##:::: ##: ##.....:: ##::::::: ##:::::::'##.... ##:
 ##:::: ##: ##::::::: ##::::::: ##::::::: ##:::: ##:
 #########: ######::: ##::::::: ##::::::: ##:::: ##:
 ##.... ##: ##...:::: ##::::::: ##::::::: ##:::: ##:
 ##:::: ##: ##::::::: ##::::::: ##::::::: ##:::: ##:
 ##:::: ##: ########: ########: ########:. #######::
..:::::..::........::........::........:::.......:::

2)在主类中通过SpringApplication来设置启动图标 

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;

@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class Demo2Application {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Demo2Application.class);
        springApplication.setBannerMode(Banner.Mode.CONSOLE);
        springApplication.run(args);
    }
}

运行结果图: 

 

 3.SpringApplication的run方法的执行流程

        SringApplicaiton的run方法的实现是我们本次运行的主要路线图,大致流程可以归纳为:

1)如果我们是用的是SpringApplication的静态run方法,那么,这个方法里面首先需要创建一个SpringApplication对象实例,然后调用这个创建好的SpringApplication的实例run方法。在SpringApplication实例初始化的时候,它会提前做几件事:

        (1)根据classpath里面是否存在某个特征类(org.springframework.web.context.ConfigurableWebApplicationContext)来决定是否应该创建一个Web应用的ApplicationContext类型,还是创建一个标准Standalone应用使用的ApplicationContext类型

         (2)使用SpringFactoriesLoader在应用的classpath中查找并加载所有可用的ApplicationContextInitializer。

        (3)使用SpringFactoriesLoader应用的classpath中查找并加载可用的ApplicationListener。

        推断并设置main方法定义类

2)SpringApplication实例初始化完成设置后,就开始执行run方法的逻辑,方法执行开始,首先遍历执行所有通过SpringFactoriesLoader可以查找到并加载的SpringApplicationRunLister,调用他们的stared()方法,告诉这些SpringApplicationRunListener,"SpringBoot应用开始执行~" 。

3)创建并配置当前SpringBoot应用将要使用的Environment(包括配置要是用的PropertySource以及Profile)

4)调用所有SpringApplicationRunListener的environmentPrepared()方法,告诉他们;“当前SpringApplication应用使用的environment准备好咯!”

5)如果SpringApplication的showBanner属性被设置为true,则打印banner(Spring1.3x版本,这里应该是基于Banner.Mode决定打印行为)。这一步的逻辑其实可以不关心。

6)根据用户是否明确设置了applicationContextClass类型以为初始化阶段的推断结果,决定为当前的SpringBoot应用创建什么类型的ApplicationContext并创建完成,然后根据条件决定是否添加ShutdownHook,决定是否使用自定义的BeanNameGenerator,决定是否使用自定义的ResourceLoader,当然最重要的,将之前准备好的Environment设置给创建好的ApplicationContext使用。

7)ApplicationContext创建好之后,SpringApplication会再次借助Spring-FactoriesLoader,查询并加载,classpath中所有可用的ApplicationContext-Initializer,然后遍历调用这些ApplicationContextInitializer的initialize(applicationContext)方法来对已经创建的好的ApplicationContext进行进一步的处理。

8)遍历调用所有SpringApplicationRunListener的contextPrepared()方法,通告他们:“SpringBoot应用使用的ApplicationContext准备好了”。

9)最核心的一步,将之前通过@EnableAutoConfiguration获取的所有配置以及其他形式的IoC容器配置架子啊到已经准备完成的ApplicationContext。

10)遍历调用所有的SpringApplicationRunListener的contextLoaded()方法,告知所有的SpringApplicationRunListener,ApplicationContext“装填完毕”

11)调用ApplicationContext的refresh()方法,完成IoC容器可用的最后一道工序。

12)查找当前ApplicationContext中是否注册有CommandLineRunner,如果有,则遍历执行他们。

13)正常情况下,遍历执行SpringApplicationRunListener的finished()方法,告知它们“搞定~”.(如果整个过程出现异常,则依然调用所有的SpringApplicationRunListener的finished()方法,只不过一个完整的SpringBoot应用启动完毕~)

整个过程看起来冗长无比,但其实很多都是一些事件通知的扩展点,如果我们将这些逻辑暂时忽略,那么,其实整个 SpringBoot 应用启动的逻辑就可以压缩到极其精简的几步,如图 1 所示。

 前后对比我们就可以发现,其实 SpringApplication 提供的这些各类扩展点近乎“喧宾夺主”,占据了一个 Spring 应用启动逻辑的大部分“江山”,除了初始化并准备好ApplicationContext,剩下的大部分工作都是通过这些扩展点完成的,所以,我们接下来对各类扩展点进行逐一剖析。

无奈源于不够强大

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号