SpringBoot化简Spring应用开发的框架,对Spring技术栈的整合,约定大于配置,去繁从简。
优点:
●快速创建独立运行的Spring项目以及主流框架继承
●使用嵌入式的Servlet容器,应用无需打成WAR包
●starters自动依赖与版本控制
●大量的自动配置,简化开发,也可修改默认值
●无需配置XML,无代码生成,开箱即用
●准生成环境的运行时应用监控
●与云计算的天然集成
缺点:springBoot入门容易,精通难。
微服务:
一个应用是一组小型服务,可以通过HTTP的方式进行互通。
每一个功能元素最终都是一个可独立替换和可独立软件升级的软件单元。
编写Hello,world
说明:依赖配置和Hello,wolrd代码可以参考SpringBoot官网
Spring | Spring Quickstart Guide
浏览器发送hello请求,服务器接受请求并处理,响应Hello,world字符串
1、创建一个MAVEN工程
2、导入SpringBoot相关依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.0
org.example
SpringBoot-HelloWorld
1.0-SNAPSHOT
8
8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
3、编写一个主程序,启动SpringBoot应用
package com.guigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//标注一个主程序类,说明这是一个SpringBoot应用
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//启动Spring应用
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
4、编写Service,Controller
package com.guigu.controller;
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";
}
}
运行
部署:
可以打成jar包后直接运行
右击package,运行
直接使用命令行运行jar包
仍然可以访问
HelloWorld探究
pom文件:
父项目:用于版本控制
org.springframework.boot
spring-boot-starter-parent
2.7.0
他的父项目是
org.springframework.boot
spring-boot-dependencies
2.7.0
进入spring-boot-dependecies可以看到有很多很多配置。
所以是由spring-boot-dependecies真正管理SpringBoot里面所有的依赖版本
spring-boot-dependecies又称为Springboot的版本仲裁中心。
所以以后导入依赖时不需要写版本;
5.16.5
2.7.7
1.9.96
2.19.1
1.9.7
3.22.0
4.0.6
4.2.0
3.3.0
1.12.10
2.6.1.Final
2.9.3
4.14.1
1.5.1
1.15
2.9.0
3.12.0
1.6
2.11.1
3.3.0
11.5.7.0
1.0.11.RELEASE
10.14.2.0
4.2.9
2.10.9.2
3.10.0
7.17.3
3.4.5
8.5.11
2.3.31
4.9.10
3.0.4
2.3.6
1.2.6
18.1
3.0.10
2.9.0
2.1.212
2.2
5.1.1
2.2.1
5.6.9.Final
6.2.3.Final
4.0.3
2.5.2
2.60.0
4.1.5
4.5.13
5.1.3
4.4.15
5.1.3
13.0.10.Final
2.22
2.13.3
1.2.2
1.3.5
2.0.3
1.1.6
1.0.2
1.6.7
1.1.4
2.2.3
4.0.4
1.2.7
1.3.3
2.0.2
1.1.2
2.1.6
2.3.3
1.4.2
2.3.3
3.1.7
1.2.0
1.3.2
1.1.1
2.3.1
2.3.1
2.0.1
1.1.4
1.0
1.6.2
1.1
2.2
1.3
2.0.1.Final
1.1
1.2.0
4.0.6.java8
3.4.3.Final
2.0.6.1
3.8.0
2.35
9.0.52
2.2.0.v201112011158
1.1.11
9.4.46.v20220331
1.15
1.2.18
1.7.1
3.14.15
2.7.0
2.4.8
1.5.0
1.2
1.3.1
4.13.2
5.8.2
3.1.1
1.6.21
1.6.1
6.1.8.RELEASE
4.9.1
2.17.2
1.2.11
1.18.24
3.0.4
3.0.0
3.3.0
3.2.0
3.10.1
3.3.0
2.8.2
3.0.0
2.22.2
3.2.0
2.5.2
3.2.2
3.2.2
3.4.0
3.2.0
3.3.0
3.2.1
2.22.2
3.3.2
1.9.0
1.10.0
4.5.1
4.6.0
10.2.1.jre8
8.0.29
1.9.22
4.4.5
4.1.77.Final
4.9.3
21.5.0.0
1.2.4
42.3.5
0.15.0
2.3.2
5.0.0
Borca-SR1
5.14.2
0.5.0
1.0.3
2020.0.19
4.5.1
1.1.2
1.3.8
1.2.1
2.2.21
1.5.3
4.1.4
3.61.0
4.9.2
4.0.1
1.7.36
1.30
8.11.1
2.4.5
4.3.6
2021.2.0
5.3.20
1.0.0
1.5.0
5.5.12
2.8.6
2.4.0
2.0.6.RELEASE
1.3.3
5.7.1
2021.2.0
3.1.3
3.36.0.3
1.6.7
3.0.15.RELEASE
2.0.1
3.0.4.RELEASE
3.0.4.RELEASE
3.0.0
9.0.63
6.0.5
2.2.17.Final
2.10.0
0.50
1.6.3
1.0.2
2.9.0
导入的依赖:
org.springframework.boot
spring-boot-starter-web
进入spring-boot-starter- web后,可以看到导入很多web模块需要的依赖,
org.springframework.boot
spring-boot-starter
2.7.0
compile
org.springframework.boot
spring-boot-starter-json
2.7.0
compile
org.springframework.boot
spring-boot-starter-tomcat
2.7.0
compile
org.springframework
spring-web
5.3.20
compile
org.springframework
spring-webmvc
5.3.20
compile
springboot将所有的功能场景都抽取处理,做成一个starters(启动器),只需要在项目里面引入starter,相关场景的所有依赖都会被导入
主程序类:
@SpringBootApplication:SpringBoot应用标注在某个类上,说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类来启动SpringBoot应用。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
@SpringBootConfiguration:SpringBoot的配置类
标注在某个类上,表示这是一个SpringBoot的配置类。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
@Configuaration:配置类
@EnableAutoConfiguration:开启自动配置功能
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class>[] exclude() default {};
String[] excludeName() default {};
}
@AutoConfigurationPackage:自动配置包
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
String[] basePackages() default {};
Class>[] basePackageClasses() default {};
}