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();
}
}