spring boot使用

spring boot使用

spring boot介绍及原理

1.介绍:Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。
2. 原理:Spring Boot 就是一些库的集合,它能够被任意项目的构建系统所使用。它使用 “习惯优于配置” (项目中存在大量的配置,此外还内置一个习惯性的配置)的理念让你的项目快速运行起来。

Spring Boot 在应用中的角色

①Spring Boot 是基于 Spring Framework 来构建的,Spring Framework 是一种 J2EE 的框架
②Spring Boot 是一种快速构建 Spring 应用
③Spring Cloud 是构建 Spring Boot 分布式环境,也就是常说的云应用
④Spring Boot 中流砥柱,承上启下

环境准备

  1. JDK 环境必须是 1.8 及以上
  2. 要使用到 Maven 管理工具 3.2.5 及以上版本
  3. 开发工具建议使用 IDEA

1.在 idea 配置 maven:

点击【File】>【Settings】>搜索【Maven】,按截图配置;

在这里插入图片描述

2.使用 idea 快速搭建 Spring Boot

第一步:新建 Spring Initializr 项目:
(1)选择 Spring Initializr
(2)选择 SDK,点击new选择自己的JDK版本

在这里插入图片描述
(3)填写项目的信息:
在这里插入图片描述
(4)勾选上 Web 依赖:
在这里插入图片描述
(5)选择好项目的位置,点击【Finish】,如果是第一次配置 Spring Boot 的话可能需要等待一会儿 IDEA 下载相应的 依赖包

在这里插入图片描述
第二步:编写控制类HelloController
在这里插入图片描述

package com.example.springboot;

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

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
}

第三步:利用 IDEA 启动 Spring Boot
(1)在SpringbootApplication 这个类中,然后右键点击运行:
在这里插入图片描述
(2)等待一会儿就会看到下方的成功运行的提示信息:
在这里插入图片描述

SpringBoot快速开始指导翻译

Spring Quickstart Guide
弹簧快速启动指南

What you’ll build
You will build a classic “Hello World!” endpoint which any browser can connect to. You can even tell it your name, and it will respond in a more friendly way.
你将要建造的是一个经典的“你好世界!”任何浏览器都可以连接到的端点。你甚至可以告诉它你的名字,它会以一种更友好的方式作出反应。

What you’ll need
An Integrated Developer Environment (IDE)
Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.
A Java™ Development Kit (JDK)
We recommend AdoptOpenJDK version 8 or version 11.
您需要集成开发人员环境(IntegratedDeveloperEnvironment,IDE)的流行选择包括IntelliJIDEA、Spring工具、VisualStudio代码或Eclipse等等。JavaOpenJDKDevelopmentKit(JDK)我们推荐AdoptOpenJDKVersion 8或Version 11。

1. Step 1: Start a new Spring Boot project: 步骤1:启动一个新的SpringBoot项目

①Use start.spring.io to create a “web” project. In the “Dependencies”dialog search for and add the “web”dependency as shown in thescreenshot. Hit the “Generate” button, download the zip, and unpack itinto a folder on your computer.
使用start.Spring.io创建一个“web”项目。在“依赖项”对话框中搜索并添加“web”依赖项,如屏幕快照所示。点击“Generate”按钮,下载zip,然后将其解压到计算机上的一个文件夹中。

②Projects created by start.spring.io contain Spring Boot, a framework that makes Spring ready to work in side your app, but without much code or configuration required. Spring Boot is the quickest and most popular way to start Spring projects.
Spring.io创建的项目包含SpringBoot,这是一个框架,使Spring可以在应用程序中工作,但不需要太多代码或配置。SpringBoot是启动Spring项目最快也是最受欢迎的方式。

2. Step 2: Add your code 步骤2:添加代码

①Open up the project in your IDE and locate the DemoApplication.java file in the src/main/java/com/example/demo folder. Now change thecontents of the file by adding the extra method and annotations shown n the code below. You can copy and paste the code or just type it.
打开IDE中的项目,并在src/main/java/com/示例/演示文件夹中找到DemoApplication.java文件。现在,通过添加下面代码中显示的额外方法和注释来更改文件的内容。您可以复制和粘贴代码,或者只需键入它。

在这里插入图片描述`package com.example.demo;

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

@SpringBootApplication
@RestController
public class DemoApplication {

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

@GetMapping("/hello")
public String hello(@RequestParam(value = “name”, defaultValue = “World”) String name) {
return String.format(“Hello %s!”, name);
}
}`

②The hello() method we’ve added is designed to take a String parameter called name, and then combine this parameter with the word “Hello” in the code. This means that if you set your name to “Amy” in the request, the response would be “Hello Amy”.

我们添加的hello()方法旨在接受一个名为name的字符串参数,然后将该参数与代码中的单词“Hello”结合起来。这意味着如果您在请求中将名称设置为“Amy”,则响应将是“HelloAmy”。

The @RestController annotation tells Spring that this code describes an endpoint that should be made available over the web. The @GetMapping(“/hello”) tells Spring to use our hello() method to answer requests that get sent to the http://localhost:8080/hello address. Finally, the @RequestParam is telling Spring to expect a name value in the request, but if it’s not there, it will use the word “World” by default.
@RestController注释告诉Spring,这段代码描述了应该通过Web提供的端点。@Getmap(“/hello”)告诉Spring使用hello()方法来响应发送到http://localhost:8080/hello地址的请求。最后,@RequestParam告诉Spring在请求中期望一个名称值,但是如果它不在那里,它将默认使用“World”一词。

3. Step 3: Try it

Let’s build and run the program. Open a command line (or terminal) and navigate to the folder where you have the project files. We can build and run the application by issuing the following command:
让我们构建并运行这个程序。打开命令行(或终端),导航到项目文件所在的文件夹。我们可以通过发出以下命令来构建和运行应用程序:

MacOS/Linux:

./mvnw spring-boot:run

Windows:

mvnw spring-boot:run

You should see some output that looks very similar to this:
您应该会看到一些非常类似于此的输出:

在这里插入图片描述

The last couple of lines here tell us that Spring has started. Spring Boot’s embedded Apache Tomcat server is acting as a webserver and is listening for requests on localhost port 8080. Open your browser and in the address bar at the top enter http://localhost:8080/hello. You should get a nice friendly response like this:
最后几句话告诉我们春天已经开始了。Spring Boot的嵌入式ApacheTomcat服务器充当Web服务器,并侦听localhost港8080。打开您的浏览器,并在地址栏的顶部回车。Http://localhost:8080/hello。你应该得到这样的友好回应:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值