Spring Boot简介
Spring Boot是对Spring进行的高度封装,是对Spring应用开发的高度简化版,是Spring技术栈的综合整合,是J2EE的一站式解决方案。想要精通Spring Boot的前提是需要熟悉Spring整套技术栈原理与内容。
Spring Boot的优点:
1、可以快速创建独立运行的Spring项目且可以与主流框架无缝集成;
2、内置使用了嵌入式的Servlet容器,应用无需打成WAR包;
3、使用了starters自动依赖与版本控制;
4、基于大量的自动配置,简化开发,无需硬编码;
5、只需极少的XML配置文件,无代码生成,开箱即用;
6、提供准生产环境的运行时应用监控;
7、与云计算天然集成等。
微服务
微服务是一组独立的小型服务,但是又可以互相通信,本质上是一种软件的架构风格。微服务架构希望把每个功能独立出来作为一个服务,实现系统的解耦,不会出现单体应用中牵一发而动全身的情况。也便于每个组件的升级与维护,以及整个系统的弹性扩容与收缩。参考链接:Microservices Guide (martinfowler.com)。Spring Boot可以说是为微服务而诞生的。

快速新建一个Spring Boot项目(基于IDEA)
**1、**打开IDEA,新建一个项目,选择Spring Initializr,如下图所示:


**2、**依次填写相关信息,Dependencies中选择Spring Web,此处Spring Boot版本选择的是2.7.7,然后一步步点击Next,最后点击Finish,如下图所示:


**3、**最终生成的项目结构如下图所示:

其中**@SpringBootApplication**为Spring Boot项目的核心注解,标志着这是一个Spring Boot应用,被此注解标注的类是整个项目的启动类,其中main()方法中调用的run()方法是使整个应用启动起来的关键。
SpringApplication.run(HelloApplication.class, args);
最终代码如下:
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
**4、**编写简单的Controller层代码作为测试,代码如下:
java
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot !";
}
}
需要注意此处的项目结构中HelloApplication类应该在最外层,如下图所示:

**5、**启动主程序即上面所说的HelloApplication类,启动成功后Console中提示默认端口为8080,在浏览器地址栏中输入定义的接口地址:localhost:8080/hello,浏览器返回自定义的信息,如下图所示:


至此,一个入门级别的Spring Boot项目搭建完毕。