不废话Spring Boot(一)快速构建第一个Spring Boot应用

Spring Boot简介

Spring Boot是一个能让你通过快速、简单的方式创建出单独的、生产级别的基于Spring的应用程序。它本身并不是一套什么新的技术,有点类似于Maven可以管理的你所有的jar包,同样,Spring Boot可以作为你项目的一个起点,帮助你迅速整合你项目中需要的技术(框架),并且使用起来非常简单,告别原来SSM、SSH框架的复杂及繁琐。总结起来,使用Spring Boot就是简单、快速和方便。

Spring Boot的特性

  • 简化应用配置,告别SSM、SSH框架的繁琐配置
  • 提供“Starter”的依赖方式,简化相关依赖配置
  • 提供健康检查、监控、审计、统计、HTTP追踪等特性
  • 如果何时需要,都可以自动装配置Spring及第三方库
  • 内置Tomcat、jetty、undertow等容器(无需单独部署War文件)
  • ……

如果对Spring、Spring Boot相关历史感兴趣,建议看一下这篇文章:https://www.cnblogs.com/ityouknow/p/10213304.html

Spring Boot官方文档地址:https://spring.io/projects/spring-boot

快速开始

环境:Spring Boot 2.2.0 Release ,JDK1.8,IDEA

由于IDEA帮我们做了跟Spring Boot的集成,所以在IDEA里新建项目:

选择新建项目的配置选项,由于各种原因,可能访问不了https://start.spring.io这个地址(参考博文:https://blog.csdn.net/Dothwinds/article/details/105534008,这里可以换自定义的地址,使用阿里云提供的地址https://start.aliyun.com)

下一步,定义好Maven坐标属性等等

选定Spring Boot版本,这里使用2.2.0版本,选择下一步,建立工程,结构如下图所示:

默认生成的POM文件如下:

<?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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.dothwinds</groupId>
    <artifactId>spring-boot-study</artifactId>
    <version>1.0.0</version>
    <name>spring-boot-study</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>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

 加入web依赖模块,在POM里加入

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

在SpringBootStudyApplication类中有一个main方法,运行这个类的启动方法,web服务就启动了。我们写一个web接口测试一下

package org.dothwinds.springbootstudy;

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

@SpringBootApplication
@RestController
public class SpringBootStudyApplication {

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

    @GetMapping("/")
    public String sayHello(){
        return "hello world";
    }

}

启动服务,默认端口是8080,访问接口地址,如下图,成功返回

没有一大堆配置文件,没有配置什么Tomcat之类,简简单单几部轻松搞定,这感觉 

 参考资料:

https://spring.io/projects/spring-boot

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值