资讯专栏INFORMATION COLUMN

SpringBoot学习笔记:Getting Started构建第一个Spring Boot工程

Drummor / 2225人阅读

摘要:本文参考官方文档部分特定版本如版本官方文档地址注本文基于构建话说在上已经有多颗星了,足见火爆程度简介以下介绍引自创建独立的应用程序直接嵌入,或无需部署文件提供自己的入门来简化你的配置尽可能自动配置提供生产就绪功能,如指标,运行

本文参考 Spring Boot官方文档 Part II. Getting Started部分
特定版本如1.5.10.RELEASE版本官方文档地址:
https://docs.spring.io/spring...
注:本文基于Spring Boot 1.5.10.RELEASE构建

The Spring Boot repository has also a bunch of samples you can run.(话说spring-boot在github上已经有23000多颗星了,足见spring-boot火爆程度)

Spring Boot简介

以下介绍引自 https://projects.spring.io/sp...

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features

Create stand-alone Spring applications
创建独立的Spring应用程序

Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)

Provide opinionated "starter" POMs to simplify your Maven
configuration
提供自己的"入门"POM来简化你的Maven配置

Automatically configure Spring whenever possible
尽可能自动配置Spring

Provide production-ready features such as metrics, health checks and
externalized configuration
提供生产就绪功能,如指标,运行状况检查和外部配置

Absolutely no code generation and no requirement for XML
configuration
绝对不会生成代码,并且不需要XML配置

系统要求

Spring Boot 1.5.10.RELEASE版本需要

Java 7 and Spring Framework 4.3.14.RELEASE or above 最好java8及以上
构建工具:Maven (3.2+), and Gradle 2 (2.9 or later) and 3.

Servlet容器:

构建工程

这里笔者采用构建Empty Project作为Spring Boot学习过程的总工程,以添加Module的方式构建不同学习过程如springboot-first-app、springboot-redis等

步骤演示

在Idea欢迎页点击Create New Project或File --> New Project,弹出以下界面并选择Empty Project

点击Next,填写Project name如SpringBootLearning

点击Finish完成创建,进入工程Idea会弹出Project Structure窗口,可以点击绿色的+按钮来New Module

也可以通过File --> New --> Module

使用Maven构建第一个Spring Boot应用

以上构建空工程的步骤只是笔者为了自身的系统学习,读者可以直接创建一个Spring Boot工程
方法一:在 Spring Boot官方Initializer页面 在线构建工程再导入到Ide中

方法二:直接在Idea中Create New Project --> Spring Initializr --> 填写group、artifact -->钩上web --> 点下一步就行了,具体步骤参照笔者New Module步骤即可

工程目录结构

pom.xml


    4.0.0

    cn.fulgens.springbootlearning
    springboot-first-app-maven
    0.0.1-SNAPSHOT
    jar

    springboot-first-app-maven
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


编写HelloController
package cn.fulgens.springbootlearning.springbootfirstappmaven.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

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

}
启动第一个Spring Boot应用

方法一:直接运行SpringbootFirstAppMavenApplication中的main方法

package cn.fulgens.springbootlearning.springbootfirstappmaven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootFirstAppMavenApplication {

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

启动后在浏览器输入localhost:8080

方法二:maven命令启动
cd到项目主目录后执行(其中-Dtest.skip=true表示跳过单元测试)

mvn clean package 
mvn spring-boot:run -Dtest.skip=true

如果是Gradle构建可执行
gradle build
gradle bootRun

方法三:以java -jar的方式启动
maven打包完成后cd到target目录下执行,jar包名称视自己情况而定

java -jar springboot-first-app-maven-0.0.1-SNAPSHOT.jar
单元测试
package cn.fulgens.springbootlearning.springbootfirstappmaven;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void helloControllerTest() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET, "/"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("Hello Spring Boot!"));
    }

}

spring boot 1.4.0 版本之前使用以下三个注解,参考Spring Boot 系列(二)单元测试&网络请求
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class) //在spring boot 1.4.0 版本之后取消了 //classes需要指定spring boot 的启动类如:DemoApplication.class 不然WebApplicationContext不被实例化
@WebAppConfiguration

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76365.html

相关文章

  • SpringBoot非官方教程 | 二十篇: 处理表单提交

    摘要:创建工程涉及了,加上和的起步依赖。创建实体代码清单如下创建页面展示层启动工程,访问点击参考资料源码下载 这篇文件主要介绍通过springboot 去创建和提交一个表单。 创建工程 涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖。 org.springf...

    impig33 评论0 收藏0
  • SpringBoot 1024行代码 - Getting Started一个简单的web应用)

    摘要:前言本文是行代码系列的第一篇。本文介绍了如何用搭建一个简单的应用。准备工作安装安装具备和的基础知识具体步骤在中加入的引用创建一个程序入口类添加一个的验证程序调用接口返回结果源码 前言 本文是SpringBoot 1024行代码系列的第一篇。本文介绍了如何用SpringBoot搭建一个简单的web应用。 准备工作 1 安装jdk1.82 安装maven3 具备Spring和SpringM...

    Soarkey 评论0 收藏0
  • 一起来学SpringBoot | 一篇:构建一个SpringBoot工程

    摘要:由于本人更习惯使用所以后续案例都是基于与,同时这里是基于最新的编写的哦创建项目初次接触,我们先来看看如何创建一个项目,这里以为例,其他的工具小伙伴们自行搜索创建方式。创建完项目后,各位小伙伴请认真细心的对比下与传统的工程有何区别如目录结构。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身...

    jayce 评论0 收藏0
  • SpringBoot学习笔记Spring Boot配置文件详解

    摘要:全局配置文件默认为下的,另外它还可以重命名为格式即对着两种格式均支持。其中每个环境的数据库地址服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。 SpringBoot全局配置文件默认为src/main/resources下的application.properties,另外它还可以重命名为.yml格式(即SpringBoo...

    sunny5541 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<