Spring Boot概念
Spring Boot 是 Spring 框架的优化版本,Spring Boot 大大优化了 Spring的 复杂配置 和 依赖配置版本 问题
特点
- 独立运行 Spring项目
- 内嵌 tomcat 和 jetty 容器
- 简化 maven配置
- 无代码生成 和 xml配置
- …
首次应用
-
创建 Maven无骨架项目 IDEA创建Maven项目 (无骨架Java项目)
-
pom.xml
引入 SpringBoot相关依赖<project> ... <!--父工程坐标 web相关应用无需配置再配置版本号(解决版本冲突问题) --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <!--当中包含 tomcat、SpringMVC、...(有关web应用,自动配置)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ... </project>
-
创建
Application
启动器类 全限定名 com.Applicationpackage com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // @SpringBootApplication启动类 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class , args); } }
-
创建
HelloController
控制器类 全限定名 com.controller.Application (为了方便看出构架结构)package com.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.sql.DataSource; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "hello , Spring Boot..."; } }
-
启动类启用Main方法,再浏览器访问:http://localhost:8080/hello (页面返回有信息表示成功!)
项目结构
.
|
├── src
| ├── main
| | ├── java
| | | └── com
| | | ├── controller
| | | | └── HelloController.java
| | | └── Application.java
| | └── resources
| test
└── pom.xml