Eclipse搭建简单的Spring boot项目
Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。
简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题---习惯大于约定。Spring Boot提供了一系列的依赖包,所以需要构建工具的支持:maven 或 gradle。个人仅熟悉maven,所以下面的内容都是maven相关的。
如果不熟悉maven,请先了解一下。
话不多说,搞起来。
1、首先你的电脑是安装过jdk的(基本常识不多讲,不会的去问百度),就像maven整理了jar包,Spring boot整理了框架,所以Spring boot项目需要maven作支持。
需要配置maven,在网上下载maven后解压。打开高级系统设置配置maven的环境。
我们需要创建MAVEN_HOME变量,值为maven的解压路径。
检验maven环境配置,配置好后打开控制台查看是否配置成功。在控制台输入mvn -v 命令。
2、下载STS插件,更快速的开发
基本的运行环境就搭建成功了,现在就可以搭建spring boot的项目。由于eclipse提供了Spring Tool Suite(STS)插件,使用插件可以更快速的开发。
打开eclipse,help-》Eclipse Marketplace-》选择Popular-》选择STS-》Installed,下载插件需要等待一段时间。
下载完成后需要重启Eclipse才能生效,会提示你重启的,确认就好了。重启后new -》Project,你会发现是酱婶的:
多出来spring目录,选中Spring Starter Project-》next
然后
可根据你需要勾选,我们勾选web,然后就可以Finish了,项目就创建成功了!
3、创建spring boot。
我们只需要编写pom.xml和一个java类。
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.midgetontoes</groupId>
-
<artifactId>spring-boot-simple</artifactId>
-
<version>1.0-SNAPSHOT</version>
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<spring.boot.version>1.1.4.RELEASE</spring.boot.version>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>3.8.1</version>
-
<scope>test</scope>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
<version>${spring.boot.version}</version>
-
</dependency>
-
</dependencies>
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
<version>${spring.boot.version}</version>
-
<executions>
-
<execution>
-
<goals>
-
<goal>repackage</goal>
-
</goals>
-
</execution>
-
</executions>
-
</plugin>
-
</plugins>
-
</build>
-
-
- </project>
java类
- package com.example.demo;
-
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
@RestController
-
@EnableAutoConfiguration
-
public class DemoApplication {
-
-
@RequestMapping("/test")
-
String home() {
-
return "Hello World!";
-
}
-
public static void main(String[] args) {
-
SpringApplication.run(DemoApplication.class, args);
-
}
-
}
4、运行Spring boot
在Application类中run as -》java Aplication,
若出现一个打印的spring图标,则一个简单的spring boot 项目就创建成功了!
也可以打开网页输入localhost:8080/test查看