SpringBoot 使用maven创建springboot项目

2021-07-02 17:04

阅读:519

标签:manage   div   创建   return   依赖   补充   star   one   world   

 有两种方式可以创建  1是使用spring-boot-start-parent ,2是使用spring-boot-dependencies (即父项目dependencyManagement)

(同理springcloud 项目也可以使用两种方式创建,推荐使用dependencyManagement,后续笔记中补充)

1.使用 spring-boot-start-parent创建

    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starterartifactId>
        version>1.5.9.RELEASEversion>
    parent>

    dependencies>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
            version>4.11version>
            scope>testscope>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starterartifactId>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

build> plugins> plugin> groupId>org.springframework.bootgroupId> artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build>

2.使用 spring-boot-dependencies创建  (推荐使用此种方式)

2.1 创建一个父级maven项目,并在父pom中添加依赖

dependencyManagement>
        dependencies>
            dependency>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-dependenciesartifactId>
                version>1.5.9.RELEASEversion>
                type>pomtype>
                scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

2.2 在子模块中 添加依赖

 dependencies>
        dependency>
            groupId>junitgroupId>
            artifactId>junitartifactId>
            version>4.11version>
            scope>testscope>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starterartifactId>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

build> plugins> plugin> groupId>org.springframework.bootgroupId> artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build>

3. 编写java代码

3.1新增一个controller

package com.itstudy.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @GetMapping("/index")
    public String getIndex()
    {
        return "hello world";
    }
}

3.2 修改主函数(App.java中的main函数)

package com.itstudy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

说明@SpringBootApplication会扫描 自己所在的包以及子包下面所有的类 。

如果需要扫描不在同一包下的类,需要增加

@ComponentScan({"jar包名"})

SpringBoot 使用maven创建springboot项目

标签:manage   div   创建   return   依赖   补充   star   one   world   

原文地址:https://www.cnblogs.com/liuxm2017/p/9629756.html


评论


亲,登录后才可以留言!