SpringBoot基础
SpringBoot概念
简化Spring,快速搭建项目,快速开发 , 使用注解进行配置,无需xml
SpringBoot-HelloWord入门
搭建简单的web应用
一、导包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web </artifactId>
<!--没有版本号-->
</dependency>
</dependencies>
二、创建配置类
@SpringBootApplication
public class ApplicationConfig {
public static void main(String[] args){
SpringApplication.run(ApplicationConfig.class, args);
}
}
三、编写Controller
@RestController
public class HelloController {
@RequestMapping("/")
public String home(){
return "Hello Word";
}
}
到此为止一个简单的HelloWord入门程序即完成了!
============================================================
SpringBoot-HelloWord程序分析(详解)
1、parent - spring-boot-starter-parent 的作用?SpringBoot的父工程,管理了很多很多的jar包
< dependencies> : 导入依赖,如果一个父工程使用 dependencies导入依赖 ,
那么这个dependencies里面的所有依赖都会被子模块直接继承使用 -> 放所有子模块公共的jar包 ,test包 。
< dependencyManagement> : 如果一个父工程使用 dependencyManagement 导入依赖 ,那么这个dependencyManagement里面的所有依赖,是不能直接被只模块使用的。
如果子项目要使用 dependencyManagement里面的jar包 ,就需要在子模块的 < dependencies> 去导入jar包
但是,版本号不用写,使用户工程的版本号 -> 管理版本号 , 除了所有子模块都能用到的包以外的其他包放到 dependencyManagement
2、spring-boot-starter-web 包的作用? 用来整合web层的一个依赖,包括了(SpringMvc,日志,tomcat,json,自动配置包)
3、@RestController的作用? 是一个组合标签 @Controller + @ResponseBody
4、@EnableAutoConfiguration的作用? 启用自动配置,自动配置前段控制器,视图解析器等等
原理:
@EnableAutoConfiguration --> AutoConfigurationImportSelector(选择器) --> classpath的jar包中加载 META-INF/spring.factories --> 加载自动配置类(
在程序启动的过程中,加载 EnableAutoConfiguration 节点下的自动配置的类,完成相关的自动配置)
5、SpringApplication.run的作用?
启动SpringBoot应用 ,准备应用数据和环境 ,创建加载容器 , 加载注册Bean(初始化bean…),解析配置类 ,做自动配置 , 项目打包到内嵌的tomcat ,运行项目
6、Tomcat 是哪儿来的? SpringBoot内嵌tomcat , 就是通过 spring-boot-starter-web 包导入了tomcat
7、项目结构(jar) ? SpringBoot默认的项目打包方式就是 jar
8、前段控制器,视图解析器等等相关配置为什么没有手动配?自动配置已经帮我们配置了
9、@SpringBootApplication 包含了如下标签:
@Configuration:Spring的配置标签:Spring的配置标签,标记该类是Spring的配置类
@EnableAutoConfiguration //开启自动配置:自动配置前段控制器,视图解析器等等
@ComponentScan //组件自动扫描:扫描当前类所在的包及其子包