Spring Boot依赖spring-boot-starter-parent是干啥的?

编程知识 更新时间:2023-04-05 04:14:10

你的项目pom.xml文件中,应该存在如下代码:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

你是否知道这是干啥的?
这是Spring Boot的父级依赖,这样当前的项目就是Spring Boot项目了。
spring-boot-starter-parent 是一个特殊的starter,它用来提供相关的Maven默认依赖。使用它之后,常用的包依赖可以省去version标签。
当我们搭建web应用的时候,可以像下面这样添加spring-boot-starter-web依赖:

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

简单的演示下 Spring Boot 项目:

maven文件看起来是这个样子的:

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache/POM/4.0.0"
       xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache/POM/4.0.0 http://maven.apache/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springBootLearn</groupId>
    <artifactId>springBootLearn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

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

HelloworldController.java

@RestController
public class HelloworldController {
    @RequestMapping("/")
    public String say(){
        return "Hello Spring Boot";
    }
}

Application.java

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Ch1Application.class, args);
    }
}

运行main方法,启动项目,然后在浏览器访问http://localhost:8080,就能看到 “Hello Spring Boot”。


作者:打铁大师
链接:https://www.jianshu/p/54cdfe4e0cac
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

更多推荐

Spring Boot依赖spring-boot-starter-parent是干啥的?

本文发布于:2023-04-05 04:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/291f06805fc3948e8c657108b140b3b8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:干啥   spring   Boot   Spring   parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 44711文章数
  • 14阅读数
  • 0评论数