从0开始搭建一个属于自己的技术架构-1-新建一个Springboot项目

开篇导读

大家好,这是我在csdn上的第一篇文章,内容并不是特别新颖,只是用一种非常基础而又相对麻烦的方法来创建一个Springboot项目。这个系列主要是为了回顾一下大学四年所学,缅怀一下逝去的时光。

目标

搭建一个能跑得动的Springboot的项目,修改端口号,访问相应路径能输出一个Hello world

准备

jdk1.8,能编译Java的IDE(Eclipse、MyEclipse、IntelliJ IDEA),maven环境

相关操作

新建一个空的maven项目

这个就不说了。

引入依赖

在项目对应的pom文件里加入一下依赖

<!--    引入父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>
<!--        引入springboot单元测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
    </dependencies>

建好你要的目录结构

在这里插入图片描述

新建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.impossible.system")
@SpringBootApplication
public class SystemApplication {

    public static void main(String[] args){
        SpringApplication.run(SystemApplication.class, args);
    }
}

新建application.yml或application.propertie

在资源文件夹下新建application.yml或application.propertie文件,不过我建议先建议创建一个config文件夹,最终路径为资源文件夹/config/application.yml或资源文件夹/config/application.propertie,这里我演示的是
application.yml

server:
  port: 8000
  servlet:
    context-path: /system
spring:
  application:
    name:system

写个HelloController看看效果

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String Hello(){
        System.out.println("Hello!");
        return "Hello world";
    }
}

效果测试

在这里插入图片描述Success

补充说明

@ComponentScan:不让启动类的位置影响目录结构,默认会扫描该类所在的包下所有的配置类

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值