Spring Boot学习笔记——02第一个Spring Boot程序

1. 创建Spring Boot项目

  1. 选择创建方式
    在这里插入图片描述
  2. 配置
    在这里插入图片描述
  3. 现在版本
    在这里插入图片描述
  4. 然后Next就行了

2. pom文件

  1. 父项目配置

    <!-- 有一个父项目 -->
    <parent>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
    	<version>2.3.4.RELEASE</version>
    	<relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  2. 引入必要的maven依赖

    <dependencies>
    	<!--
    		启动器
    		说白了, 就是Spring Boot的启动场景
    		Spring Boot 会将所有的功能场景, 都变成一个个的启动器
    		spring-boot-starter-web: 自动导入Web环境所有的依赖
    		spring-boot-starter-test: 自动导入测试环境所有的依赖
    	-->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter</artifactId>
    	</dependency>
    	<!-- web依赖: tomcat, dispatcherServlet, xml -->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-web</artifactId>
    	</dependency>
    
    	<!-- spring boot的依赖都是以spring-boot-starter开头的 -->
    	<!-- 单元测试 -->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-test</artifactId>
    		<scope>test</scope>
    		<exclusions>
    			<exclusion>
    				<groupId>org.junit.vintage</groupId>
    				<artifactId>junit-vintage-engine</artifactId>
    			</exclusion>
    		</exclusions>
    	</dependency>
    </dependencies>
    

3. 程序代码

  1. 创建一个控制器(HelloController)

    package com.hjf.controlller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author Jiang锋时刻
     * @create 2020-09-20 20:26
     */
    
    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String hello(){
            return "hello world";
        }
    }
    
  2. 主程序

    package com.hjf;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    /**
     * SpringApplication类的作用:
     * 	1. 推断应用的类型是普通的项目还是Web项目
     * 	2. 查找并加载所有可用初始化器, 设置到initializers属性中
     * 	3. 找出所有的应用程序监听器, 设置到listeners属性中
     * 	4. 推送并设置main方法的定义类, 找到运行的主类
     */
    @SpringBootApplication
    public class Demo01Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Demo01Application.class, args);
        }
    }
    
    
  3. 启动主程序, 运行结果
    在这里插入图片描述

4. 说明
  1. Spring Boot 默认端口号是8080, 但是我的8080端口被占用了的, 所以就改为了8081, 在application.properties文件中设置
    在这里插入图片描述
  2. 可以通过创建banner.txt文件, 自定义设置启动的banner图像
    在这里插入图片描述
    启动
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值