SpringBoot入门之起步依赖

引子

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

       ;一句话来概括SpringBoot,那么官方的这句话就很简单明了。通过SpringBoot 使得创建独立、生产级的 Spring应用非常简单,开发者可以直接运行。
SpringBoot的主要功能特性有以下几个方面:

  • Create stand-alone Spring applications(创建独立的Spring应用)
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)(内嵌的Web服务器,如Tomcat,Undertow,Jetty等)
  • Provide opinionated ‘starter’ dependencies to simplify your build - configuration(起步依赖)
  • Automatically configure Spring and 3rd party libraries whenever possible(自动配置)
  • Provide production-ready features such as metrics, health checks, and externalized configuration(监控功能,通过一些度量指标、健康检查以及扩展配置可以很方便的做到生产级的监控)
  • Absolutely no code generation and no requirement for XML configuration(不需要代码生成以及XML配置)

本文作为SpringBoot学习的第一篇,主要从SpringBoot的起步依赖特性进行讲解,后边的章节会一一对其它特性进行说明。

在介绍SpringBoot的起步依赖之前,可能一些开发的老兵门会想到在没有SpringBoot的时候,开发一个Spring应用的艰辛。例如曾经的SSH/SSM/SSI开发组合,需要自己将工程中需要的Jar文件手动的放入工程中的lib目录中,随着功能的增多,不同jar的版本问题、循环依赖问题、后期由于安全或者jar bug升级版本而带来的一系列灾难,可能没有接触过的同志很难体会。而这些问题往往随着系统历史原因功能的堆积而变得牵一发而动全身,升级难、扩展难。

随着SpringBoot的出现,之前面临的问题不复存在,这里也给还在使用传统的Spring开发的朋友们提个醒,是时候拥抱变化了,如果系统需要重构或者恰逢其时,使用SpringBoot做个应用架构升级会让你以后的开发更加事半功倍。

开发一个SpringBoot版本的Hello World

使用Springboot开发一个web应用,你可能仅需要短短数行代码即可完成。我们可以通过 开发工具IDEA新建一个Spring Initializer工程(Eclipse同样支持)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
生成的工程结构如下所示:
在这里插入图片描述
再看我们的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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.study.springboot</groupId>
    <artifactId>helloword</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloword</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-web</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>

其中仅有两个dependency ,但是我们的 External Libiary(打包后再lib目录)中却有了这么多的依赖。新建一个HellowordController.java文件,见下图所示,然后点击运行,在浏览器中输入 http://localhost:8080
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
我们仅仅写了一个Controller,没有做过任何配置,也没有进行过任何服务器的搭建,怎么就通过8080 端口打印出来了Hello world了呢?

起步依赖

在看到上边的效果前,你一定会想,不可能凭空产生,那么一定是我们在Pom中加入的一个依赖产生了连锁反应,替我们将一些东西给做了。

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

对,没错!srping-boot-starter-web这个依赖,其实替我们引入了很多web开发所需要的jar,包括支持应用运行的web容器,例如tomcat。在上边的external library中我们所看到的众多jar都是通过这个starter引入的。怎么样,SpringBoot很神奇吧,你只需要告诉它,你想要的开发一个web应用,那么它就可以将web开发所需要的 很多东西包含了进来。
SpringBoot内置了很多可以让开发者简化应用配置和依赖管理的starter,例如:spring-boot-starter-data-x,spring-boot-starter-jpa,更详尽的官方starter可以在 https://github.com/spring-projects/spring-boot/tree/v2.3.0.RELEASE/spring-boot-project/spring-boot-starters 中找到。 除了官方的starter,也有一些第三方的starter,例如·mybatis-spring-boot-starter,mybatis-plus-boot-starter等,用户甚至可以将自己项目中的常用功能打包为一个starter进行引用(后边会介绍如何自定义一个starter)。

起步依赖本质上是一个Maven项目对象模型,定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。很多起步依赖的命名都暗示了他们提供的某种或某类功能。对于官方提供的starter而言,请保持所有依赖的starter版本的一致性,无须另行制定,而应该由spring-boot版本来决定。

例如spring-boot-starter-web的pom模型为:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.3.0.RELEASE</version>
  <name>spring-boot-starter-web</name>
  <description>Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container</description>
  <url>https://spring.io/projects/spring-boot</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>https://spring.io</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <issueManagement>
    <system>GitHub</system>
    <url>https://github.com/spring-projects/spring-boot/issues</url>
  </issueManagement>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.3.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.3.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.3.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.3.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

拿来主义

记得大学刚开始学习JAVA的时候,很多人都觉得代码就是Ctrl + C/V的组合,JAVA编程是拿来主义,引入第三方JAR,调用API完事了,大家的着力点都在研究API的调用层面上了。现在有了SpringBoot,我觉得可以叫做想要什么,都拿去。你想要的一个Web应用,不要研究什么JAR的引入、不要把主要精力放在一些繁琐的配置上,想要什么你告诉SpringBoot就好,你只管用。

参考资料:

  1. https://www.jianshu.com/p/0e72409ef182
  2. https://www.jianshu.com/p/a010fb4007c0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它提供了一种简化的方法来配置和部署应用程序,使开发人员能够更快地开发和运行应用程序。 Spring Boot Actuator是Spring Boot的一个组件,它可以帮助我们监控和管理Spring Boot应用程序,包括健康检查、审计、统计和HTTP追踪等功能。要使用Spring Boot Actuator,只需引入相应的起步依赖,并在应用程序的入口点类上添加@SpringBootApplication注解即可。在该类中,使用@SpringBootApplication注解相当于同时添加了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,它标识了当前应用程序是一个Spring Boot应用程序。要启动Spring Boot应用程序,只需在主启动类中编写main函数,通过调用SpringApplication.run(Application.class, args)方法来启动应用程序。在开发过程中,<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot入门](https://blog.csdn.net/weixin_45905210/article/details/121712027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [spring boot 入门](https://blog.csdn.net/zhshx19900318/article/details/129476812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值