快捷搜索: 王者荣耀 脱发

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

你的项目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.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>

    <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”。

经验分享 程序员 微信小程序 职场和发展