一个90后女孩的第一个 Spring Boot 应用

一个90后女孩的第一个 Spring Boot 应用

本文转载自微信公众号「小明菜市场  」,作者小明菜市场。转载本文请联系小明菜市场公众号。

前言

Spring Boot 出现的原因

Spring Boot的出现,主要是用来解决 Spring 过去的一些问题,提出了约定优于配置的思想,默认对很多方法进行了设置,使得开发者可以快速的构建项目,集成第三方的内容。使得开发效率大大提升。

基本概念

Spring Boot 不单单是一套框架,是一套体系,目的是简化 Spring 的开发。

特点

基于 Spring 的开发提供更快的入门 直接上手,冗余代码没有 内嵌式容器 简化 Spring

核心功能极度依赖构建工具 能够进行自动化的配置

Hello World

Maven创建

创建一个新的空工程,分别创建 module,如下图所示

一个90后女孩的第一个 Spring Boot 应用

创建 Maven Module

创建一个 Module,选择 Maven 工程,勾选以前用的 web 骨架

一个90后女孩的第一个 Spring Boot 应用

填写好 GroupID,ArtifactID

一个90后女孩的第一个 Spring Boot 应用


选择好以后,按住回车

一个90后女孩的第一个 Spring Boot 应用


这样就完成了一个基本的 maven 项目的创建

添加起步依赖

根据 Spring Boot 的要求,进行简单的测试,以及添加相应的起步依赖 项目需要继承 Spring Boot 的起步依赖 Spring boot starter parent 为了集成 Spring MVC 进行 Controller 开发,需要导入 Spring boot starter web

<?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 http://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.7.RELEASE</version> 
    </parent> 
 
    <groupId>cn.ideal</groupId> 
    <artifactId>springboot_01_start</artifactId> 
    <version>1.0-SNAPSHOT</version> 
 
    <dependencies> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
    </dependencies> 
</project> 

编写 Spring Boot 启动类

这里编写 Spring Boot 启动类

package cn.ideal; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
@SpringBootApplication 
public class MySpringBootApplication { 
    public static void main(String[] args) { 
        SpringApplication.run(MySpringBootApplication.class); 
    } 
} 

创建控制层

package cn.ideal.controller; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
@Controller 
public class QuickStartController { 
    @RequestMapping("/test") 
    @ResponseBody 
    public String test(){ 
        return "springboot 访问测试,起飞,飞飞飞飞 ~ ~ ~"; 
    } 
} 

测试 Spring Boot

项目启动,控制台会输出如下内容

一个90后女孩的第一个 Spring Boot 应用

 .   ____          _            __ _ _ 
 /\ / ___'_ __ _ _(_)_ __  __ _     
( ( )___ | '_ | '_| | '_ / _` |     
 \/  ___)| |_)| | | | | || (_| |  ) ) ) ) 
  '  |____| .__|_| |_|_| |___, | / / / / 
 =========|_|==============|___/=/_/_/_/ 
 :: Spring Boot ::        (v2.2.7.RELEASE) 
 
2020-05-10 22:11:34.973  INFO 30580 --- [           main] cn.ideal.MySpringBootApplication         : Starting MySpringBootApplication on LAPTOP-5T03DV1G with PID 30580 (F:developIdeaProjectsframework-codespringboot_01_demospringboot_01_starttargetclasses started by abc in F:developIdeaProjectsframework-codespringboot_01_demo) 
2020-05-10 22:11:34.976  INFO 30580 --- [           main] cn.ideal.MySpringBootApplication         : No active profile set, falling back to default profiles: default 
2020-05-10 22:11:35.686  INFO 30580 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http) 
2020-05-10 22:11:35.693  INFO 30580 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat] 
2020-05-10 22:11:35.693  INFO 30580 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.34] 
2020-05-10 22:11:35.765  INFO 30580 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 
2020-05-10 22:11:35.766  INFO 30580 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 747 ms 
2020-05-10 22:11:35.884  INFO 30580 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor' 
2020-05-10 22:11:35.990  INFO 30580 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '' 
2020-05-10 22:11:35.993  INFO 30580 --- [           main] cn.ideal.MySpringBootApplication     

图片显示如下

一个90后女孩的第一个 Spring Boot 应用


输入创建的 controller 项目直接打印出来

一个90后女孩的第一个 Spring Boot 应用

项目打包成为 jar 包

添加依赖

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

单击右边,进行package 进行打包,选择package选项

一个90后女孩的第一个 Spring Boot 应用

可以看到target下产生了新的jar包,这里直接在cmd中运行

一个90后女孩的第一个 Spring Boot 应用

相关推荐