127.【SpringBoot 源码刨析D】(五)

简介: 127.【SpringBoot 源码刨析D】

service包下: HelloProperties.java

并无引入组件

package com.jsxs.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 * @Author Jsxs
 * @Date 2023/7/29 13:29
 * @PackageName:com.jsxs
 * @ClassName: HelloProperties
 * @Description: TODO
 * @Version 1.0
 */
@ConfigurationProperties("atguigu.hello")
public class HelloProperties {
    private String prefix;
    private String suffix;
    public String getPrefix() {
        return prefix;
    }
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
    public String getSuffix() {
        return suffix;
    }
    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

bean包下: HelloService.jsva

并无引入组件

package com.jsxs.service;
import com.jsxs.bean.HelloProperties;
import javax.annotation.Resource;
/**
 * @Author Jsxs
 * @Date 2023/7/29 13:27
 * @PackageName:com.jsxs.service
 * @ClassName: HelloService
 * @Description: TODO
 * @Version 1.0
 */
public class HelloService {
    @Resource
    HelloProperties helloProperties;
    public String sayHello(String username) {
        return helloProperties.getPrefix() + ": " + username + ">" + helloProperties.getSuffix();
    }
}

auto: HelloServiceAutoConfig.java

package com.jsxs.auto;
import com.jsxs.bean.HelloProperties;
import com.jsxs.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @Author Jsxs
 * @Date 2023/7/29 13:37
 * @PackageName:com.jsxs.auto
 * @ClassName: HelloServiceAutoConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
@ConditionalOnMissingBean(HelloService.class)  // 1.假如没存在这个组件,就自动引入
@EnableConfigurationProperties(HelloProperties.class)  // 1.将属性放入容器 2.且绑定属性
public class HelloServiceAutoConfig {
    @Bean
    public HelloService helloService(){
        return new HelloService();
    }
}

5.配置自动引入

autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jsxs.auto.HelloServiceAutoConfig

4. 先放到本地Maven仓库

(3.2).使用我们自定义的Statr

1.引入我们的xml文件

<dependency>
            <groupId>com.jsxs</groupId>
            <artifactId>atguigu-hello-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

2.测试

package com.jsxs.Controller;
import com.jsxs.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
 * @Author Jsxs
 * @Date 2023/7/29 14:15
 * @PackageName:com.jsxs.Controller
 * @ClassName: HelloController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
public class HelloController {
    @Resource
    HelloService helloService;
    @GetMapping("/")
    public String sayHello(){
        return helloService.sayHello("李明");
    }
}
atguigu.hello.prefix:你好
atguigu.hello.suffix:!

(3.3).starter启动原理
  • starter-pom引入 autoconfigurer 包
  • autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类
  • 编写自动配置类 xxxAutoConfiguration -> xxxxProperties
  • @Configuration
  • @Conditional
  • @EnableConfigurationProperties
  • @Bean

引入starter — xxxAutoConfiguration — 容器中放入组件 ---- 绑定xxxProperties ---- 配置项

7.SpringBoot 原理

Spring原理【Spring注解】、SpringMVC原理、自动配置原理、SpringBoot原理

(1).SpringBoot启动过程

  • 创建 SpringApplication
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return new SpringApplication(primarySources).run(args);
  }
  • 保存一些信息 (应用引导器,初始化器,监听器)。
  • 判定当前应用的类型。ClassUtils。Servlet
  • bootstrappers:初始启动引导器(List):去spring.factories文件中找 org.springframework.boot.Bootstrapper
  • ApplicationContextInitializer启动初始化器spring.factories找 ApplicationContextInitializerList<ApplicationContextInitializer<?>> initializers
  • 找 ApplicationListener ;应用监听器。去spring.factories找 ApplicationListener
  • List<ApplicationListener<?>> listeners
  • 运行 SpringApplication
  • StopWatch (监听项目的起停的)
  • 记录应用的启动时间
  • 创建引导上下文(Context环境)createBootstrapContext()
  • 获取到所有之前的 bootstrappers 挨个执行 intitialize() 来完成对引导启动器上下文环境设置
  • 让当前应用进入headless模式。java.awt.headless
  • 获取所有RunListener(运行监听器)【为了方便所有Listener进行事件感知】
  • getSpringFactoriesInstancesspring.factories找 SpringApplicationRunListener.
  • 遍历SpringApplicationRunListener调用starting方法;
  • 相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。
  • 保存命令行参数;ApplicationArguments
  • 准备环境prepareEnvironment();
  • 返回或者创建基础环境信息对象。StandardServletEnvironment
  • 配置环境信息对象
  • 读取所有的配置源的配置属性值。
  • 绑定环境信息
  • 监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成
  • 创建IOC容器(createApplicationContext())
  • 根据项目类型(Servlet)创建容器

  • 当前会创建 AnnotationConfigServletWebServerApplicationContext
  • 准备ApplicationContext IOC容器的基本信息 prepareContext()
  • 保存环境信息
  • IOC容器的后置处理流程
  • 应用初始化器;applyInitializers
  • 遍历所有的 ApplicationContextInitializer 。调用 initialize.。来对ioc容器进行初始化扩展功能
  • 遍历所有的 listener 调用 contextPrepared。EventPublishRunListenr;通知所有的监听器contextPrepared
  • 所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded;
  • 刷新IOC容器。refreshContext
  • 创建容器中的所有组件(Spring注解)
  • 容器刷新完成后工作?afterRefresh
  • 所有监听 器 调用 listeners.started(context); 通知所有的监听器 started
  • 调用所有runners;callRunners()
  • 获取容器中的 ApplicationRunner
  • 获取容器中的 CommandLineRunner
  • 合并所有runner并且按照@Order进行排序
  • 遍历所有的runner。调用 run 方法
  • 如果以上有异常
  • 调用Listener 的 failed
  • 调用所有监听器的 running 方法 listeners.running(context); 通知所有的监听器 running
    - running如果有问题。继续通知 failed 。调用所有 Listener 的 failed;通知所有的监听器 failed
/*
 * Copyright 2012-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.boot;
/**
 * Callback interface that can be used to initialize a {@link BootstrapRegistry} before it
 * is used.
 *
 * @author Phillip Webb
 * @since 2.4.0
 * @see SpringApplication#addBootstrapper(Bootstrapper)
 * @see BootstrapRegistry
 */
public interface Bootstrapper {
  /**
   * Initialize the given {@link BootstrapRegistry} with any required registrations.
   * @param registry the registry to initialize
   */
  void intitialize(BootstrapRegistry registry);
}


相关文章
|
21天前
|
设计模式 Java 关系型数据库
【Java笔记+踩坑汇总】Java基础+JavaWeb+SSM+SpringBoot+SpringCloud+瑞吉外卖/谷粒商城/学成在线+设计模式+面试题汇总+性能调优/架构设计+源码解析
本文是“Java学习路线”专栏的导航文章,目标是为Java初学者和初中高级工程师提供一套完整的Java学习路线。
175 37
|
9天前
|
JavaScript Java 关系型数据库
毕设项目&课程设计&毕设项目:基于springboot+vue实现的在线考试系统(含教程&源码&数据库数据)
本文介绍了一个基于Spring Boot和Vue.js实现的在线考试系统。随着在线教育的发展,在线考试系统的重要性日益凸显。该系统不仅能提高教学效率,减轻教师负担,还为学生提供了灵活便捷的考试方式。技术栈包括Spring Boot、Vue.js、Element-UI等,支持多种角色登录,具备考试管理、题库管理、成绩查询等功能。系统采用前后端分离架构,具备高性能和扩展性,未来可进一步优化并引入AI技术提升智能化水平。
毕设项目&课程设计&毕设项目:基于springboot+vue实现的在线考试系统(含教程&源码&数据库数据)
|
11天前
|
Java 关系型数据库 MySQL
毕设项目&课程设计&毕设项目:springboot+jsp实现的房屋租租赁系统(含教程&源码&数据库数据)
本文介绍了一款基于Spring Boot和JSP技术的房屋租赁系统,旨在通过自动化和信息化手段提升房屋管理效率,优化租户体验。系统采用JDK 1.8、Maven 3.6、MySQL 8.0、JSP、Layui和Spring Boot 2.0等技术栈,实现了高效的房源管理和便捷的租户服务。通过该系统,房东可以轻松管理房源,租户可以快速找到合适的住所,双方都能享受数字化带来的便利。未来,系统将持续优化升级,提供更多完善的服务。
毕设项目&课程设计&毕设项目:springboot+jsp实现的房屋租租赁系统(含教程&源码&数据库数据)
|
16天前
|
机器学习/深度学习 数据采集 JavaScript
ADR智能监测系统源码,系统采用Java开发,基于SpringBoot框架,前端使用Vue,可自动预警药品不良反应
ADR药品不良反应监测系统是一款智能化工具,用于监测和分析药品不良反应。该系统通过收集和分析病历、处方及实验室数据,快速识别潜在不良反应,提升用药安全性。系统采用Java开发,基于SpringBoot框架,前端使用Vue,具备数据采集、清洗、分析等功能模块,并能生成监测报告辅助医务人员决策。通过集成多种数据源并运用机器学习算法,系统可自动预警药品不良反应,有效减少药害事故,保障公众健康。
ADR智能监测系统源码,系统采用Java开发,基于SpringBoot框架,前端使用Vue,可自动预警药品不良反应
|
2月前
|
JavaScript Java 关系型数据库
美妆商城系统 SpringBoot + Vue 【毕业设计 资料 + 源码】
这篇文章介绍了一个使用SpringBoot + Vue + Mybatis + Mysql技术栈开发的美妆商城系统,包括系统功能划分、部分页面截图和前后端源码示例,并提供了GitHub上的源码链接。
美妆商城系统 SpringBoot + Vue 【毕业设计 资料 + 源码】
|
2月前
|
JavaScript Java 关系型数据库
毕设项目&课程设计&毕设项目:基于springboot+vue实现的前后端分离的选课管理系统(含教程&源码&数据库数据)
本文介绍了一个基于Spring Boot和Vue.js技术栈的高校选课管理系统的设计与实现。该系统采用前后端分离架构,旨在提高选课效率、优化资源分配及提升用户体验。技术栈包括:后端Spring Boot 2.0、前端Vue 2.0、数据库MySQL 8.0、开发环境JDK 1.8和Maven 3.6等。系统功能覆盖登录、学生信息管理、选课管理、成绩查询等多个方面,并针对学生、教师和管理员提供了不同的操作界面。系统采用了响应式设计,支持多设备访问,并通过Element UI增强了界面的友好性和交互性。
毕设项目&课程设计&毕设项目:基于springboot+vue实现的前后端分离的选课管理系统(含教程&源码&数据库数据)
|
2月前
|
安全 Java 关系型数据库
毕设项目&课程设计&毕设项目:基于springboot+jsp实现的健身房管理系统(含教程&源码&数据库数据)
本文介绍了一款基于Spring Boot和JSP技术实现的健身房管理系统。随着健康生活观念的普及,健身房成为日常锻炼的重要场所,高效管理会员信息、课程安排等变得尤为重要。该系统旨在通过简洁的操作界面帮助管理者轻松处理日常运营挑战。技术栈包括:JDK 1.8、Maven 3.6、MySQL 8.0、JSP、Shiro、Spring Boot 2.0等。系统功能覆盖登录、会员管理(如会员列表、充值管理)、教练管理、课程管理、器材管理、物品遗失管理、商品管理及信息统计等多方面。
|
2月前
|
JavaScript Java 关系型数据库
毕设项目&课程设计&毕设项目:基于springboot+vue实现的前后端分离的考试管理系统(含教程&源码&数据库数据)
在数字化时代背景下,本文详细介绍了如何使用Spring Boot框架结合Vue.js技术栈,实现一个前后端分离的考试管理系统。该系统旨在提升考试管理效率,优化用户体验,确保数据安全及可维护性。技术选型包括:Spring Boot 2.0、Vue.js 2.0、Node.js 12.14.0、MySQL 8.0、Element-UI等。系统功能涵盖登录注册、学员考试(包括查看试卷、答题、成绩查询等)、管理员功能(题库管理、试题管理、试卷管理、系统设置等)。
毕设项目&课程设计&毕设项目:基于springboot+vue实现的前后端分离的考试管理系统(含教程&源码&数据库数据)
|
2月前
|
Web App开发 前端开发 关系型数据库
基于SpringBoot+Vue+Redis+Mybatis的商城购物系统 【系统实现+系统源码+答辩PPT】
这篇文章介绍了一个基于SpringBoot+Vue+Redis+Mybatis技术栈开发的商城购物系统,包括系统功能、页面展示、前后端项目结构和核心代码,以及如何获取系统源码和答辩PPT的方法。
下一篇
无影云桌面