SpringBoot-三种方式搭建
本章主要讲解三种方式对Spring的搭建,开始入门SpringBoot。
- 使用官网搭建SpringBoot
- 手动搭建SpringBoot
- 使用IDEA搭建SpringBoot
目录
环境准备
java:1.8
Maven:3.3.9
SpringBoot:2.X
开发工具
IDEA:2020
1、使用官网搭建SpringBoot
(1)打开官网:https://start.spring.io/
(2)填写要创建项目的基本信息
(3) 点击GENERATE进行下载。
(4) 解压到指定目录。
(5) 用IDEA进行导入
(6) 编写测试Controller
注意:务必要在主程序SpringBootTestApplication同级目录下进行创建。
package com.yixin.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
点击运行主程序SpringBootTestApplication。
浏览器测试:
第一种方式搭建成功。
2、手动搭建SpringBoot
(1)创建一个Maven项目
(2)在pom.xm导入以下依赖及配置。
parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
(3)编写主程序(启动Spring Boot应用)
package com.yixin.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
(4)编写相关的Controller测试
注意:务必跟主程序在同级目录下。
package com.yixin.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
(5)运行主程序测试
第二种方式到此就大功告成了!
补充
如果希望将应用打成jar包,直接使用java -jar的命令进行执行,那么应在pom.xml进行如下配置。
<!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3、使用IDEA搭建SpringBoot(推荐)
(1)创建一个新项目
(2)选择spring initalizr , 其默认就是去官网的快速构建工具那里实现的
(3)填写项目基本信息
(4)选择初始化的组件(Web)
(5)创建成功。
(6)编写Controller进行测试。
注意:务必在主启动程序的同级目录下进行创建。
package com.yixin.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello(){
return "Hello World!--IDEA";
}
}
(7)运行主程序。
大功告成!
结语
对SpringBoot的搭建,一心同学主要整理出了以上三种,在我们平时的开发当中,建议使用IDEA进行SpringBoot的搭建,因为这样更快,更方便。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
二维码