如何创建一个SpringBoot项目?(详细的图文教程)

编程入门 行业动态 更新时间:2024-10-28 12:27:00

如何<a href=https://www.elefans.com/category/jswz/34/1771345.html style=创建一个SpringBoot项目?(详细的图文教程)"/>

如何创建一个SpringBoot项目?(详细的图文教程)

如何创建一个SpringBoot项目?

SpringBoot 官网

官网介绍:

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
You can use Spring Boot to create Java applications that can be started by using java -jar or more traditional war deployments. We also provide a command line tool that runs “spring scripts”.Our primary goals are:

  • Provide a radically faster and widely accessible getting-started experience for all Spring development.
  • Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.
  • Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics,
    health checks, and externalized configuration).
  • Absolutely no code generation and no requirement for XML configuration.

简单理解为:

  • 更广泛的、迅速的 Spring 开发体验
  • 一系列开箱即用的 starter
  • 一系列非功能性的自动化配置(无法实现业务配置)
  • 没有xml配置文件生成
SpringBoot 工程创建:(三种方式)

1. 在线创建

  • 官方推荐方式: spring initializr
    • 选择开发语言,版本号,填写项目名,打包方式,指定 Java 版本等,点击 GENERATE,网站自动生成并下载 SpringBoot 项目
    • 解压下载的文件,用开发工具打开即可。

2. 通过 IDE 创建(IntelliJ IDEA)

  • 点击 Spring Initializr , 选择 JDK 版本 ,选择 Default ,点击 Next;

  • 填写Group 和 Artifact 信息,选择对应的开发语言,打包方式,Java 版本等 ,点击 Next;

  • 选择 Web 依赖 和 Spring Boot 版本号,点击 Next;

  • 选择项目的保存位置,点击 FINISH

  • 点击 Enable Auto-Import 导入依赖

  • 至此创建完成

3. 通过改造 Maven 工程创建

  • 在IDEA 界面,依次点击 File -> New -> Project;

  • 选择 Maven 和 JDK 版本,点击 Next; (此处不选择 Maven 模板)

  • 修改项目名和指定项目地址

  • 此时创建一个普通的 Maven 项目,此时需要在 pom.xml 中引入 SpringBoot 需要的 jar 包;
    详细可查看SpringBoot 官方文档: Developing Your First Spring Boot Application
  1. 创建POM文件: Creating the POM
    在pom.xml 文件中引入:
	<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.7.RELEASE</version></parent>
  1. 添加 SpringBoot 依赖: Adding Classpath Dependencies
<dependencies><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=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>SpringBootDemo</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.7.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>
  1. 编写启动类:Writing the Code
  • 新建包(包名最好为项目名),并创建启动类;

  • 编写启动类:
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 SpringBootApplication {@RequestMapping("/")String home() {return "Hello World!";}public static void main(String[] args) {SpringApplication.run(SpringBootApplication.class, args);}
}

检测是否创建成功?

  • 启动 main() 方法

  • 访问 localhost:8080

    至此 SpringBoot 创建成功!

ps:如有错误,欢迎批评指正,谢谢

更多推荐

如何创建一个SpringBoot项目?(详细的图文教程)

本文发布于:2023-07-28 21:51:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   图文   项目   教程   详细

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!