SpringBoot概要简介&SpringBoot入门,pom文件剖析,打jar包以及banner的修改(附banner生成网址)

本文介绍了 Spring Boot 的概要,包括其诞生背景、优缺点及与微服务的关系。还详细说明了 Spring Boot 入门的环境要求、项目创建方式、pom 文件配置、处理请求案例、项目打 Jar 包、banner 修改、项目列表显示优化及运行原理等内容,帮助开发者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot概要

SpringBoot介绍

随着动态语言的流行(Ruby、Scala、Node.js), Java的开发显得格外的笨重;繁多的配置、低下的开发效率、复杂的部署流程以及第三方技术整合难度大。

在上述环境下,Spring Boot由此诞生,它的设计是为了使您能够尽可能快地启动和运行。它使用 “习惯优于配置” (项目中存在大量的配置,而 Spring Boot 内置一个习惯性的配置,让你无须手动进行配置)的理念让你的项目快速运行起来。使用 Spring Boot 很容易创建一个独立运行(运行jar,内嵌Servlet 容器)、准生产强力的基于 Spring 框架的项目,使用 Spring Boot你可以不用或者只需要很少的 Spring 配置。提供了 J2EE 开发的一站式解决方案。

2014 年 4 月,Spring Boot 1.0.0 发布。Spring的顶级项目之一(https://spring.io)。

Spring Boot makes it easy to create stand-alone,production-grade Spring based Applications that you can “just run”

能快速创建出生产级别的Spring应用

SpringBoot优点

  1. Create stand-alone Spring applications

    创建独立Spring应用

  2. Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

    内嵌web服务器

  3. Provide opinionated ‘starter’ dependencies to simplify your build configuration

    自动starter依赖,简化构建配置

    起步依赖 ,起步依赖本质上是一个Maven项目对象模型(Project Object ModelPOM),定义了对其他库的传递依赖 ,这些东西加在一起即支持某项功能。 简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能

  4. Automatically configure Spring and 3rd party libraries whenever possible

    自动配置Spring以及第三方功能

  5. Provide production-ready features such as metrics, health checks, and externalized

    configuration

    提供生产级别的监控、健康检查及外部化配置

  6. Absolutely no code generation and no requirement for XML configuration

    无代码生成、无需编写XML

小结:

  1. SpringBoot是整合Spring技术栈的一站式框架
  2. SpringBoot是简化Spring技术栈的快速开发脚手架
  3. Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式

SpringBoot缺点

  • 人称版本帝,迭代快,需要时刻关注变化

  • 封装太深,内部原理复杂,不容易精通

时代背景----微服务

James Lewis and Martin Fowler (2014) 提出微服务完整概念。https://martinfowler.com/microservices/

In short, the microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.

James Lewis and Martin Fowler (2014)

  • 微服务是一种架构风格
  • 一个应用拆分为一组小型服务
  • 每个服务运行在自己的进程内,也就是可独立部署和升级
  • 服务之间使用轻量级HTTP交互
  • 服务围绕业务功能拆分
  • 可以由全自动部署机制独立部署
  • 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术

SpringBoot入门

熟练 Spring 框架的使用

熟练 Maven 依赖管理与项目构建熟练使用 Eclipse 或 IDEA

环境要求

  1. JDK
  2. Maven
  3. SpringBoot

创建SpringBoot项目

创建方式一:使用Spring Initializr 的Web页面创建项目

  1. 打开:https://start.spring.io/
  2. 填写项目信息
  3. 点击”Generate Project“按钮生成项目;下载此项目
  4. 解压项目包,并用IDEA以Maven项目导入,一路下一步即可,直到项目导入完毕
  5. 如果是第一次使用,可能速度会比较慢,包比较多、需要耐心等待一切就绪

在这里插入图片描述

创建方式二:使用IDEA直接创建项目

  1. 创建一个新项目
  2. 选择spring initalizr , 可以看到默认就是去官网的快速构建工具那里实现
  3. 填写项目信息
  4. 选择初始化的组件(初学勾选 Web 即可)
  5. 填写项目路径
  6. 等待项目构建成功

创建方式三:使用IDEA直接创建项目

  1. 与方式二相同,但是参考路径改为:https://start.aliyun.com/

创建方式四:使用IDEA创建Maven项目并改造为SpringBoot

项目结构分析:

  1. 程序的主启动类

  2. 一个 application.properties 配置文件

  3. 一个 测试类

  4. 一个 pom.xml

演示:用方式二创建一个SpringBoot项目
在这里插入图片描述

SpringBoot项目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>

    <!--父pom文件,SpringBoot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--本项目的基本信息-->
    <groupId>com.dong</groupId>
    <artifactId>springboot_init</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_init</name>
    <description>SrpingBoot_init_day01</description>

    <!--版本锁定-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--坐标-->
    <dependencies>
        <!--web启动器
        spring-boot-starter-xxxxx 由springboot提供的启动器
        xxxxxx-spring-boot-starter由xxx第三方提供的启动器
        spring+springMVC环境均添加
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--测试启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

处理请求案例

  1. 在主程序的同级目录下,新建一个controller包,一定要在同级目录下,否则识别不到

  2. 在包中新建一个Controller类

  3. 编写完毕后,从主程序启动项目,浏览器发起请求,看页面返回;控制台输出了 Tomcat 访问的端口号!

    eg:UserController

    @Controller
    public class UserController {
        @RequestMapping(path = "/init")
        @ResponseBody
        public String doinit(){
            System.out.println("处理了用户请求");
            return "Hello SpringBoot";
        }
    }
    

    @Controller:将控制器注入容器

    @RequestMapping:访问路径

    @ResponseBody:响应内容变为Json格式

  4. 测试:运行SpringBoot生成的SpringbootInitApplication,浏览器访问路径:localhost:8080/init

  5. 结果:

    页面显示:Hello SpringBoot

    控制台输出:处理了用户请求

项目打Jar包

在这里插入图片描述

运行jar包步骤:

​ 找到磁盘上项目路径,到target目录下,复制jar包全名,点击路径输入cmd打开终端,使用命令:java -jar 项目名.jar,终端输出启动成功即可

banner的修改

运行成功后控制台会输出banner(即控制台上的图案),可以修改此图案

  1. 项目下的 resources 目录下新建一个banner.txt 即可

  2. 图案可以生成,拷贝到文件中即可;生成网址:https://www.bootschool.net/ascii

  3. 设置图片作为banner,properties文件中需要配置下列信息(一般不用)

    spring.banner.image.location=logo.png
    

假如创建了一个banner.txt并粘贴了一下内容,每次启动时就会打印此内容


//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//            佛祖保佑       永不宕机     永无BUG                    //

项目列表显示优化

在这里插入图片描述

Setting–》Editor–》File Types–》红色框位置输入想要隐藏的文件类型

运行原理初步探求

  1. spring-boot-starter-parent
    • springBoot项目都要继承spring-boot-starter-parent
    • spring-boot-starter-parent中定义了若干个依赖管理
    • 继承parent模块可以避免多个依赖使用相同技术时出现依赖冲突
    • 继承parent的形式也可以采用引入依赖的形式实现效果例如alibaba
  2. start
    • 导入套餐形式的坐标从而简化配置,坐标实现依赖传递
  3. 引导类
    • 程序主入口,初始化spring容器,扫描所有类
    • 可以通过run方法返回值获取bean检验
  4. 内嵌tomcat
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeMonkey-D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值