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

如何创建一个SpringBoot项目?(详细的图文教程)SpringBoot简介SpringBoot官网地址:https://www.spring.io官网介绍:SpringBootmakesiteasytocreatestand-alone,production-gradeSpring-basedApplicationsthatyoucanrun.WetakeanopinionatedviewoftheSpringplatformandthird-partylibraries,sothaty

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

如何创建一个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 模板)

在这里插入图片描述

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

在这里插入图片描述

  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="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>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:如有错误,欢迎批评指正,谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/183872.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号