Spring boot底层实现原理

Spring boot简介

1.Spring Boot是什么?

Spring Boot是由Pivotal团队提供的框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。

2.为什么要使用Spring Boot?

Spring Boot是为简化Spring项目配置而生,使用它使得jar依赖管理以及应用编译和部署更为简单。Spring Boot提供自动化配置,使用Spring Boot,你只需编写必要的代码和配置必须的属性。

3.SpringBoot重点

1.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>

2.完全无配置文件(采用注解化)
springboot没有配置文件,如何进行初始化?
在没有web.xml配置文件的情况,通过java代码操作整个SpringMVC的初始化过程,java代码最终会生成class文件,内置Tomcat就会加载这些class文件,当所有程序加载完成后,项目就可以访问了。

以前的web项目,通过Web.xml配置文件加载整个项目流程。
在这里插入图片描述
3.没有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> * 
*  */
@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;	} }	

4.内置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(); 	} }

完成

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值