前些天搭建服务器的时候,学习了一下spring boot框架,spring boot的特点如下:
- 化繁为简,简化配置
- 备受关注,是下一代框架
- 微服务的入门级微框架
总之优点大大的。下面记录一下我在eclipse上安装spring tool suite插件(STS)的过程
查看eclipse版本号
在你的eclipse目录下找到readme文件夹,里面的html文件双击打开,在左上角看到自己的eclipse版本号信息。比如我的是4.5。
获取插件安装地址
打开网址:https://spring.io/tools/sts/all
一定要往下翻翻,找到自己的版本,4.5的,然后将update sites复制下来。
打开eclipse,help->install new software…,点击add。name填spring,location填刚才复制的地址,如下:
下面勾选带IDE的,并点击next,如下:
后面如下,最终finish,于是开始安装:
安装结束后要重启一下eclipse。重启后,window->preferences,左侧看到spring,说明装好了。
创建project
点击file->new->project…在spring中找到spring starter project,填写一下名字如下,然后next
下面有一堆选项,只需要勾选web里面的web。最终maven项目开始构建。如果是第一次使用的,这个过程非常之慢,耐心等待吧……
添加依赖
打开pom.xml,选到写代码的模式下。
添加两个依赖。第一个是作为web项目必须引入的一个依赖,第二个是作为单元测试时要用到的。然后再添加一个插件,是使用maven构建项目的时候的一个插件。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
第一个spring boot应用
我这里包名是com.example,看一下在这个包里已经有一个类了,是DemoApplication.java。我们再新建一个类,叫做HelloController。
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String say(){
return "Hello Spring Boot!";
}
}
然后在DemoApplication.java下右键->run as->spring boot app。在这里我们可以看到tomcat已经启动在了8080端口。
以后再运行时候可以点图标。注意:左边停止运行,右边重新运行。要点击右边。如果用原来方式运行的话,记得先点左边停止一下之前的,否则端口占用,报错。
打开浏览器,输入网址,成功~