springBoot (一)

springBoot框架流程:

先创建Tomcat容器,然后加载class文件,加载过程中如果发现有java代码编写的SpringMVC初始化,就会创建SpringMVC容器。所有程序执行完毕后,项目就可以访问了。
在这里插入图片描述

  • 快速整合第三方依赖: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>
  • 完全无配置文件(采用注解化)
    在没有web.xml配置文件的情况,通过java代码操作整个SpringMVC的初始化过程,java代码最终会生成class文件,内置Tomcat就会加载这些class文件,当所有程序加载完成后,项目就可以访问了。

以前的web项目,通过Web.xml配置文件加载整个项目流程。
在这里插入图片描述
没有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;
	}
 
}
  • 内置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();
 
	}
 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章