一、SpringBoot介绍
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Features
- Create stand-alone Spring applications
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
- Provide opinionated ‘starter’ POMs to simplify your Maven configuration
- Automatically configure Spring whenever possible
- Provide production-ready features such as metrics, health checks and externalized configuration
- Absolutely no code generation and no requirement for XML configuration
二、入门Demo(Helloworld)
(1)https://start.spring.io/ 从这上面下载demo(直接点击generate Project)
(2)eclipse –>inport–>Existing Maven Projects,导入(1)中下载的Maven项目(Demo)
如下图示:
(3)在pom.xml中配置以下依赖
<!-- 在生成的Demo里,parent已经存在,不需要重复配置 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<!--以下依赖需要配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
(4)创建SampleController.java类,创建完成后会报错,因为找不到framework下相关的包。
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
(5)在eclipse里,project–>maven–>update project,下载项目依赖(注:需要配置好eclipse的maven环境)
下载依赖的jar后,第(3)中的错误将消失。
(6)点击这个类,右键 run as–>java application
console中打印如下内容,则表示启动成功:
(7)在浏览器中访问:
三、遇到的错误
1、eclipse的Maven配置不正确,无法下载依赖的jar
解决方法:重新配置Maven环境
2、run as–>java application 运行报错
问题原因:8080端口被占用导致
解决方法:
1、在CMD里,输入netstat -nao,打印端口使用情况
2、找到8080端口,查看对应PID
3、打开任务管理器,切到详细信息界面,找到PID对应的进程,结束此进程。
4、重新run as–>java application