专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

阿里面试官:请对springboot底层实现原理进行分析

一、什么是SpringBoot?

SpringBoot是一个快速开发框架,快速的将一些常用的第三方依赖整合(原理:通过Maven子父工程的方式),简化XML配置,全部采用注解形式,内置Http服务器(Jetty和Tomcat),最终以java应用程序进行执行。

二、SpringBoot核心原理

1> 基于SpringMVC无配置文件(纯Java)完全注解化+内置tomcat-embed-core实现SpringBoot框架,Main函数启动。

2> SpringBoot核心快速整合第三方框架原理:Maven继承依赖关系。

三、SpringBoot重点

3、1:快速整合第三方依赖:maven子父依赖关系

springboot 通过引用spring-boot-starter-web依赖,整合SpingMVC框架。只需要引用一个jar包,就可以通过Maven继承的方式引用到Spring-aop,Spring-beans,Spring-core,Spring-web等相关依赖。

 <parent>   <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version></parent><dependencies>     <!-- SpringBoot 整合SpringMVC --> <!-- 为什么我们映入spring-boot-starter-web 能够帮我整合Spring环境 原理通过Maven子父         工程 -->   <dependency>        <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-web</artifactId>    </dependency></dependencies>

3、2:完全无配置文件(采用注解化)

springboot没有配置文件,如何进行初始化?

在没有web.xml配置文件的情况,通过java代码操作整个SpringMVC的初始化过程,java代码最终会生成class文件,内置Tomcat就会加载这些class文件,当所有程序加载完成后,项目就可以访问了。

以前的web项目,通过Web.xml配置文件加载整个项目流程。

136_1.png

没有web.xml文件,那么Tomcat是如何启动(注解在什么时候产生)?

在Spring3.0以上(提供注解,在这个版本以后,有了巨大改变,完全不需要任何配置文件加载项目)。

SpringMVC内置注解加载整个SpringMVC容器。相当于使用Java代码编写SpringMVC初始化。

package com.springboot.config; import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.InternalResourceViewResolver; /** * springmvc 配置信息 *  * @EnableWebMvc 开启springmvc 功能<br> * @作者说明 LongCode <br> */@Configuration@EnableWebMvc   //此注解就是开启SpringMVC容器@ComponentScan(basePackages = { "com.springboot.controller" })public class WebConfig extends WebMvcConfigurerAdapter {  // springboot 整合jsp 最好是war  // 需要配置视图转换器    // 创建SpringMVC视图解析器 @Bean   public ViewResolver viewResolver() {        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();     viewResolver.setPrefix("/WEB-INF/views/");      viewResolver.setSuffix(".jsp");     // 可以在JSP页面中通过${}访问beans        viewResolver.setExposeContextBeansAsAttributes(true);       return viewResolver;    } }

3、3:内置Http服务器

java代码创建Tomcat容器,加载class文件。

 package com.springboot; import java.io.File; import javax.servlet.ServletException; import org.apache.catalina.LifecycleException;import org.apache.catalina.WebResourceRoot;import org.apache.catalina.core.StandardContext;import org.apache.catalina.startup.Tomcat;import org.apache.catalina.webresources.DirResourceSet;import org.apache.catalina.webresources.StandardRoot; public class AppTomcat {   public static void main(String[] args) throws ServletException, LifecycleException {        // 使用Java内置Tomcat运行SpringMVC框架 原理:tomcat加载到     // springmvc注解启动方式,就会创建springmvc容器      start();    }   public static void start() throws ServletException, LifecycleException {        // 创建Tomcat容器       Tomcat tomcatServer = new Tomcat();     // 端口号设置        tomcatServer.setPort(9090);     // 读取项目路径 加载静态资源        StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main").getAbsolutePath());        // 禁止重新载入       ctx.setReloadable(false);       // class文件读取地址      File additionWebInfClasses = new File("target/classes");        // 创建WebRoot        WebResourceRoot resources = new StandardRoot(ctx);      // tomcat内部读取Class执行        resources.addPreResources(              new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/"));       tomcatServer.start();       // 异步等待请求执行     tomcatServer.getServer().await();   } }

springBoot框架流程:先创建Tomcat容器,然后加载class文件,加载过程中如果发现有java代码编写的SpringMVC初始化,就会创建SpringMVC容器。所有程序执行完毕后,项目就可以访问了。

文章永久链接:https://tech.souyunku.com/29373

未经允许不得转载:搜云库技术团队 » 阿里面试官:请对springboot底层实现原理进行分析

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们