一、SpringBoot简介
1.什么是SpringBoot
Spring Boot 是所有基于 Spring 开发的项目的起点。 Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。 简单来说就是SpringBoot其实不是什么新的框架,它默认配置了很多框架的使用方式
2.SpringBoot四个主要特性
SpringBoot Starter:他将常用的依赖分组进行了整合,将其合并到一个依赖中,这样就可以一次性添加到项目的Maven或Gradle构建中 自动配置:SpringBoot的自动配置特性利用了Spring4对条件化配置的支持,合理地推测应用所需的bean并自动化配置他们 命令行接口:(Command-line-interface, CLI):SpringBoot的CLI发挥了Groovy编程语言的优势,并结合自动配置进一步简化Spring应用的开发 Actuatir:它为SpringBoot应用的所有特性构建一个小型的应用程序。但首先,我们快速了解每项特性,更好的体验他们如何简化Spring编程模型
3.SpringBoot开发的具体好处
快速创建独立运行的Spring项目以及与主流框架集成 使用嵌入式的Servlet容器,应用无需打成WAR包 starters自动依赖与版本控制 大量的自动配置,无代码生产,开箱即用 准生产环境的运行时应用监控 与云计算的天然集成
二、Springboot的入门
1.创建maven项目,jar包即可
2.导入依赖
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< groupId> springboot.start</ groupId>
< artifactId> Spring-boot</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< name> Spring-boot</ name>
< url> http://www.example.com</ url>
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.1.7.RELEASE</ version>
< relativePath/>
</ parent>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
</ dependencies>
</ project>
3.创建启动项
package com. spring. boot. demo;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. EnableAutoConfiguration;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. ResponseBody;
import org. springframework. web. bind. annotation. RestController;
@RestController
@EnableAutoConfiguration
public class SpringBootStartDemo {
public static void main ( String[ ] args) {
SpringApplication. run ( SpringBootStartDemo. class , args) ;
}
@RequestMapping ( "/hello" )
@ResponseBody
public String hello ( ) {
return "welcome to springboot World!" ;
}
}
4.测试
SpringBoot第二种实现方式
package com. study. spring. boot;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
@SpringBootApplication
public class SpringbootQuickApplication {
public static void main ( String[ ] args) {
SpringApplication. run ( SpringbootQuickApplication. class , args) ;
}
}
package com. study. spring. boot;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping ( "/hello" )
public String hello ( ) {
return "hello World!" ;
}
}
三、SpringBoot启动详细介绍
1.starters
SpringBoot为我们提供简化企业级开发绝大多数场景的start pom(启动器),只要引入相应场景的starter pom,相关技术的绝大部分配置将会消除(自动配置),从而简化开发。 具体请参考:https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#using-boot-starter
SpringBoot对场景依赖的jar包进行严格的测试与版本控制,我们不必担心jar版本合适度
的问题,因为spring-boot-dependencies里面定义了jar包的版本号
。
2.入口类和@SpringBootApplication
程序从main方法
开始运行 使用SpringApplication.run()
加载主程序类 主程序类需要标注@SpringBootApplication
这个注解表明这个类是主配置类
2.@SpringBootConfiguration
SpringBoot的配置类,标注在某个类上,表示这是一个SpringBoot的配置类
3.@Configuration
配置类上标注这个注解 配置类–>配置文件,配置类也是容器中的一个组件@Component
4.EnableAutoConfiguration
5.AutoConfigurationPackage
6.Import
Import(EnableAutoConfigurationImportSelector.class
@AutoConfigurationPackage
@Import ( EnableAutoConfigurationImportSelector. class )
public @interfaceEnableAutoConfiguration {
Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class
将主配置类(@SpringBootApplication
标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器
;
四、使用SpringBoot的向导创建项目