一、SpringBoot概述
(一)、Spring缺点
1. 配置繁琐
2. 依赖繁琐
(二)、SpringBoot功能
1. 自动配置
2. 起步依赖
3. 辅助功能
二、SpringBoot快速入门
(一)、需求
搭建SpringBoot工程,定义HelloController.hello()方法,返回“Hello SpringBoot”
(二)、步骤
1. 创建Maven项目
File – New – Module – Maven
2. 导入SpringBoot
打开pom.xml文件,加入<parent>
<!--springboot工程需要继承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
3. 起步依赖
打开pom.xml文件,加入<dependencies>
<dependencies>
<!--web开发的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
完整pom.xml
<?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>com.demo</groupId>
<artifactId>springboot_helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<!--springboot工程需要继承的父工程-->
<!--注释快捷键是 Ctrl + Shift + / -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<!--web开发的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
4. 定义Controller
src – main – java 目录下新建一个类:com.demo.controller.HelloController
HelloController.java
package com.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello SpringBoot";
}
}
5. 编写引导类
src – main – java 目录下新建一个类:com.demo.HelloApplication
HelloApplication.java
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 引导类,Spring Boot项目入口
*/
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}
}
6. 启动测试
点击HelloApplication.java文件中main函数左侧绿色三角,运行
浏览器输入http://localhost:8080/hello
小结
1.创建Maven工程,配置pom.xml,即添加<parent></parent>和<dependencies></dependencies>
2.写一个HelloController.java
3.写一个引导类HelloApplication.java