什么是SpringBoot?
我们以前开发的项目是基于单体架构的。单体架构使用的技术
(1) jsp/sevlet
(2) spring + struts + hibernate
(3) springmvc + mybatis
后面我们会慢慢拆分:
基于分布式架构我们可以使用
(1) Http:Springmvc + mybatis
(2) RPC: Dubbo + Spring
(3) 微服务框架:SpringBoot:构建微服务项目。可以快速构建一个web应用,本质上是对Spring的封装,可以简化我们的配置。
上面是微服务架构,就是把一组小的服务互相协调、互相配合。并且完成功能。每个服务运行在其独立的进程中,服务与服务之间采用轻量级的通信机制互相协作(通常是基于HTTP协议的RESTfulAPI)。每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境中。
SpringBoot快速开始
第一种:创建一个空的maven项目,然后导入SpringBoot相关的jar包
① 父工程依赖:
org.springframework.bootspring-boot-starter-parent2.0.8.RELEASE
② Spring mvc-web依赖
org.springframework.bootspring-boot-starter-web
③ 引入Spring Boot插件,可以支持我们将web应用程序打包可运行jar
org.springframework.bootspring-boot-maven-plugin
④ 编写程序入口:
@SpringBootApplicationpublic class HelloWorldApplication { public static void main( String[] args ) { SpringApplication.run(HelloWorldApplication.class,args); }}
⑤ 编写Controller
@RestControllerpublic class HelloWorldController { @RequestMapping("/hello") public String hello(){ return "Hello world"; }}
注意:自己写的组件必须放在主启动的 所在的包或者其子包下,如果不遵守这个规则,则可以自定义加载:使用@ComponentScan指定。
⑥运行main函数运行程序,访问http://localhost:8080/hello,或者执行mvc package将项目打成jar包,用java -jar xxx.jar直接运行。
第二种:通过idea创建一个SpringBoot项目
下面就是编写自己的 业务,和上面一样。
为什么我只需要引用Spring-boot-starter-parent 和 spring-boot-starter-web就可以快速开发mvc的项目
我们看一下我们引用的 Spring-boot-starter-parent
真正起作用的是:
org.springframework.boot spring-boot-dependencies 2.0.8.RELEASE../../spring-boot-dependencies
再点击进去,我们就会看到版本依赖:
我们常用的依赖都在上面。
我们在分析spring-boot-starter-web引入了什么jar包
org.springframework.boot spring-boot-starter 2.0.8.RELEASEcompileorg.springframework.boot spring-boot-starter-json 2.0.8.RELEASEcompileorg.springframework.boot spring-boot-starter-tomcat 2.0.8.RELEASEcompileorg.hibernate.validator hibernate-validator 6.0.14.Finalcompileorg.springframework spring-web 5.0.12.RELEASEcompileorg.springframework spring-webmvc 5.0.12.RELEASEcompile
从上面可以看到,spring-boot-starter-web 自动为 我们引入了Spring-web,Spring-webmvc和相关的校验,还有tomcat。