首页 > 其他分享 >SpringBoot官方教程1入门

SpringBoot官方教程1入门

时间:2023-07-11 13:01:51浏览次数:41  
标签:INFO __ 教程 SpringBoot jar boot spring ___ 入门

Spring Boot helps you to create stand-alone, production-grade Spring-based applications that you can run. Most Spring Boot applications need very little Spring configuration.

Build Tool:Maven

Servlet Container:Tomcat

快速创建:start.spring.io

手动创建:

Maven 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>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.1</version>
    </parent>

    <!-- Additional lines to be added here... -->

</project>

Spring Boot provides a number of “Starters” that let you add jars to your classpath. “Starters” provide dependencies that you are likely to need when developing a specific type of application.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

编写代码:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

@Controller handling incoming web requests

@RequestMapping provides “routing” information

@SpringBootApplication combines @SpringBootConfiguration@EnableAutoConfiguration and @ComponentScan

We need to pass MyApplication.class as an argument to the run method to tell SpringApplication which is the primary Spring component.

运行:

$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v3.1.1)
....... . . .
....... . . . (log output here)
....... . . .
........ Started MyApplication in 0.906 seconds (process running for 6.514)

打jar包:

SpringBoot打jar包,用Maven打包就行了,SpringBoot提供了maven-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
$ mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:3.1.1:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

运行jar:

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v3.1.1)
....... . . .
....... . . . (log output here)
....... . . .
........ Started MyApplication in 0.999 seconds (process running for 1.253)

参考资料:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#getting-started

标签:INFO,__,教程,SpringBoot,jar,boot,spring,___,入门
From: https://www.cnblogs.com/df888/p/17544346.html

相关文章

  • SpringBoot开启Gzip接口报文压缩
    背景当我们一个接口响应报文比较大的时候,超过几兆甚至几十兆的情况下,减少响应体的报文大小是能有效减少响应时间的。springboot配置server:compression:##开启服务端的报文压缩enabled:true##只压缩响应类型为application/json的报文mime-types:a......
  • Streamlit 入门介绍
    Streamlit入门介绍Streamlit是一个PythonWeb应用框架。但和常规Web框架,如Flask/Django的不同之处在于,它不需要你去编写任何客户端代码(HTML/CSS/JS),只需要编写普通的Python模块,就可以在很短的时间内创建美观并具备高度交互性的界面,从而快速生成数据分析或者机器学习的结......
  • Windows版python安装教程
    Windows版python安装教程第一步:下载Python最新版本Python官网:http://www.python.org进入官网之后,根据电脑系统选择合适的版本下载安装。截止到目前为止,python-3.10.2是Python最新版本(此版本Windows7系统及更早系统无法使用,Windows7系统的小伙伴可以选择下载python-3.8.10)。第......
  • SpringBoot整合Caffeine本地缓存
    1、@Cacheable相关注解1.1相关依赖如果要使用@Cacheable注解,需要引入相关依赖,并在任一配置类文件上添加@EnableCaching注解<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>1.2常......
  • SkyWalking入门与实战
    SkyWalking部署一、基础介绍1.1概念SkyWalking是一个国产的开源框架,2015年由吴晟个人开源,2017年加入Apache孵化器,国人开源的产品,主要开发人员来自于华为,2019年4月17日Apache董事会批准SkyWalking成为顶级项目,支持Java、.Net、NodeJs等探针,数据存储支持Mysql、Elasticsearch等,......
  • 新版Springboot3.0打造能落地的高并发仿12306售票系统
    第1章课程介绍与学习指南3节|22分钟本章主要对课程做整体介绍,其中包括:课程要解决的问题、课程特色和亮点、课程内容安排、学完大家的收获,以及在学习方法上提出的建议与指导。 第2章12306这个系统架构到底有多牛?8节|71分钟本章主要对课程为什么选择12306课程作为实战......
  • Camtasia Studio 2023.0.2 Build 45178中文版功能介绍及免费下载安装教程
    TechSmithCamtasia2023Mac版软件由兔八哥爱分享的Macos系统上一款屏幕录制软件中文版,它可以帮助用户录制电脑屏幕、添加音频、视频和图片,进行剪辑和编辑,并输出高质量的视频文件。CamtasiaStudio2023.0.2Build45178软件介绍Camtasia2023是一款简便的屏幕录制程序,该软件帮助......
  • Docker教程
    目录Docker教程1.Docker是什么2.Docker的用途3.Docker的安装4.运行Docker4.1DockerHelloWorld4.2运行交互式的容器4.3后台模式启动容器4.4停止容器5.Docker容器使用5.1Docker客户端5.2容器使用5.2.1获取镜像5.2.2启动镜像5.2.3启动已经停止的容器5.2.4后台运行5.......
  • Git教程
    Git教程目录Git教程1.安装Git1.1在Linux上安装Git1.2在windows上安装Git2.创建版本库2.1创建一个版本库:2.2添加文件到版本库3.版本管理3.1查看代码仓库的状态3.2版本穿梭3.3工作区和暂存区3.4管理修改3.5撤销修改3.6删除文件4.远程仓库4.1创建和添加SSH秘钥4.2添......
  • 腾讯T3-3级资深架构师呕心整理:10万字节springboot教程全解!
    前言:可以毫不夸张地说,这篇文章介绍的SpringBoot教程基本已经涵盖你工作中遇到的大部分常用的场景。对于每一个springboot的用法,文中都会都会有详细的教程解释。本文总共从29个章节,248页PDF,阐述springboot在各种环境下的各种用法,文中给大家列举出部分的springboot教程,需要这份10......