SpringBoot:spring-boot-starter-parent

版权声明:本博客为记录本人学习过程而开,内容大多从网上学习与整理所得,若侵权请告知! https://blog.csdn.net/Fly_as_tadpole/article/details/84957783

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

猜你喜欢

转载自blog.csdn.net/Fly_as_tadpole/article/details/84957783