第一个SpringBoot程序

本文指导如何在IDEA中创建SpringBoot项目,介绍了项目结构、HelloController示例、配置文件和依赖管理,帮助读者理解基本流程和关键设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一个SpringBoot程序的创建

  • BootSchool这个网站里面有好多有趣的页面和注释大家有时间了可以去玩一玩

  • 第一种呢就是去官网直接下载后导入idea开发(这种呢一般不常用)
    在这里插入图片描述

  • 也可以直接去idea中去创建一个SpringBoot项目(这中方式呢更加的方便)

项目结构(虽然你自己创建一个SpringBoot会生成但是我还是给大家拿过来)

在这里插入图片描述

  • 项目创建步骤
    在这里插入图片描述
    在这里插入图片描述
  • 上面的一些选项呢是你项目的名字和JDK的一个版本最后一个呢留意一下是里面包的一个结构所以一般都要去改动这个结构去选则你自己的结构自动生成的结构可能会有点长。
  • controller这个包呢是我们创建的还创建了HelloController类
package com.cloud.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hello")
public class HelloController {
    //接口:http://localhost:8080/hello/hello
    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }
}
//这个代码呢就不做过多的解释了这个呢就想当于一个重定向就是一个接口会跳转页面并返回一个hello

  • application.properties这个呢是人家生成的用来写一些SpringBoot的核心配置文件
#SpringBoot的核心配置文件
#更改项目的端口号
server.port=8081

  • banner.txt这个也是我们自己写的也给大家拿过来这个其实是个小趣味就是把人家的一些东西干掉了留给大家去发现

       $$$$$$$$
      $$$$$$$$$$
     $$$$$$$$$$$$
     $$$$$$$$$$$$
     $$$$$$$$$$$$
    $$$$$$$$$$$$$
   $$$$$$$$$$$$$
   $$$$$$$$$$$$$
   $$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$$$$$$$$
   $$$$$$  $$$$$$$$$$$$$$
     $$$$    $$$$$$$$$$$
     $$$$     $$$$$$$$$$
     $$$$     $$$$$$$$$$
      $$$$    $$$$$$$$$$
       $$$    $$$$$$$$$
        $$$  $$$$$$$$$$
         $$$$$$$$$$$$$$
          $$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$
          $$$$$$$$$$    $$$$$$$$$$$$$
           $$$$$$$$       $$$$$$$$$$$
           $$$$$$$$     $$$$$$$$$$$
           $$$$$$$    $$$$$$$$$$
           $$$$$$$ $$$$$$$$$$
          $$$$$$$$$$$$$$$
          $$$$$$$$$$$$
          $$$$$$$$$$
         $$$$$$$$
         $$$$$$
        $$$$$$
        $$$$$$
        $$$$$$
        $$$$$
        $$$$$
         $$$$
         $$$$
         $$$$
        $$$$$
        $$$$$
         $$$$
          $$$$$
          $$$$
  • Springboot01HelloworldApplicationTests这是一个自动生成的测试类
package com.cloud;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

//单元测试
@SpringBootTest
class Springboot01HelloworldApplicationTests {

    @Test
    void contextLoads() {
    }

}

  • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--它呢是有一个父项目的-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cloud</groupId>
    <artifactId>springboot-01-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-01-helloworld</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--相当于一个启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--web依赖:tomcat,dispatcherServlet,xml....-->
        <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>

    <!--spring-boot-starter所有的springboot依赖都是以这个开头的-->

    <build>
        <!--打jar包插件-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这个呢给大家重点讲一下这几个依赖第一个呢就相当于一个依赖启动器,第二个依赖呢就是web依赖它做了一个上面操作呢它把一些tomcat还有web.xml里面的配置都做了比ssm更加的简化了大大减少了大家书写代码的行数最后呢一个是jar包创建和单元测试依赖,还有呢项目结构呢一定要根据我的项目结构创建因为虽然它虽然是更加方便了但自由度却降低了里面存在一些规则。

第一个Spring Boot是通过引入`spring-boot-starter-web`来实现的。这是一个Spring Boot场景启动器,它帮助我们导入了web模块正常运行所依赖的组件。[1] 引用: spring-boot-starter-web spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件 。 引用: <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.13.RELEASE</version> </parent> 。 引用: package com.wu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootApplication public class HelloWorldMainJavaApplication { public static void main(String[] args) { //spring应用启动起来 SpringApplication.run(HelloWorldMainJavaApplication.class,args); } } 。 问题:请告诉我,第一个Spring Boot是如何实现的?<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [springboot入门--第一个springboot程序](https://blog.youkuaiyun.com/ChaoticNg/article/details/114651903)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值