zoukankan      html  css  js  c++  java
  • SpringBoot 使用maven创建springboot项目

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

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

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

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>

    <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

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

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

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>1.5.9.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    2.2 在子模块中 添加依赖

     <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>

    <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </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函数)

    import org.springframework.boot.Banner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
    
            SpringApplication app = new SpringApplication(App.class);
            //关闭banner
            app.setBannerMode(Banner.Mode.OFF);
            app.run(args);
        }
    
    }

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

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

    @ComponentScan({"jar包名"})

    4.设置src/main/resources/application.properties

    # 当server.port=0时 是随机端口
    server.port=8080
  • 相关阅读:
    关于Allele(等位基因)的理解
    beta 分布的详细介绍(转载)
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/9629756.html
Copyright © 2011-2022 走看看