0
点赞
收藏
分享

微信扫一扫

目标检测算法

在这里插入图片描述

文章目录

前言

一. SpringBoot3介绍

1.1 SpringBoot项目创建

1. 创建Maven工程

在这里插入图片描述

2. 添加依赖(springboot父工程依赖 , web启动器依赖)
  • springboot父工程依赖

    <parent>
            <!-- spring-boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>3.0.5</version>
    </parent>
    
  • web启动器依赖

    <dependencies>
            <!-- 导入对应启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
3. 编写启动引导类(springboot项目运行的入口)
package com.hky;
	
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	
	/**
	 * @author hky
	 * @date ${DATE}
	 * @Description
	 */
	
	// 1. 包含配置类 @SpringBootConfiguration
	// 2. 自动加载配置 @EnableAutoConfiguration
	// 3. @ComponentScan 默认扫描当前类所在包,子包的注解
	@SpringBootApplication //启动类
	public class Main {
	    public static void main(String[] args) {
	        SpringApplication.run(Main.class,args);//自动创建ioc容器,启动tomcat服务器软件
	    }
	}
4. 编写处理器Controller
package com.hky.controller;
	
	import org.springframework.web.bind.annotation.GetMapping;
	import org.springframework.web.bind.annotation.RequestMapping;
	import org.springframework.web.bind.annotation.RestController;
	
	/**
	 * @author hky
	 * @date 2024/6/28
	 * @Description
	 */
	@RestController
	@RequestMapping("hello")
	public class HelloController {
	
	    @GetMapping("boot")
	    public String hello(){
	        return "hello springboot3!!";
	    }
	}
5. 启动项目

点击启动类,启动项目
在这里插入图片描述

1.2 项目理解

1. 依赖不需要写版本原因
  • 每个boot项目都有一个父项目spring-boot-starter-parent
  • parent的父项目是spring-boot-dependencies,父项目把所有常见的jar的依赖版本都声明好了。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
2. 启动器(Starter)
  • 使用Starter:在 pom.xml 中添加所需的Starter依赖,Spring Boot会自动处理依赖管理和配置。
  • spring boot提供的全部启动器地址
3. @SpringBootApplication注解
  • @SpringBootApplication注解是Spring Boot框架中的核心注解,它的主要作用是简化和加速Spring Boot应用程序的配置和启动过程
  • 其中包含:

二. SpringBoot配置文件

2.1 统一配置管理

  • 配置文件应该放置在Spring Boot工程的src/main/resources目录下
  • 命名:application 后缀 .properties / .yaml / .yml
  • 若同时存在application.properties | application.yml(.yaml) , properties的优先级更高。

2.2 properties配置文件使用

  • 在resources文件夹下创建一个application.properties 配置文件
    在这里插入图片描述
    配置文件内用 key=值 的形式书写配置文件\

    server.port=8081
    server.servlet.context-path=/huahua
    
    # 自定义
    hky.name=hekaiyan
    hky.age=18
    
  • 读取配置文件
    使用@Value(“${hky.name}”)

    @RestController
    @RequestMapping("hello")
    public class HelloController {
    
        @Value("${hky.name}")
        private String name;
    
        @GetMapping("boot")
        public String hello(){
            System.out.println(name);
            return "hello springboot3!!";
        }
    }
    
    

2.3 yaml配置文件使用

  • YAML配置文件的扩展名是yaml 或 yml

    
    server:
      port: 8081
      servlet:
        context-path: /huahua
    
    hky:
      info:
        name: hekaiyan  #此时,冒号属性后面必须有一个空格
        age: 18
        anims:          #数组
          - dog
          - cat
    

2.4 批量配置文件注入

  • 创建实体类,在实体类中添加属性和注解(属性名称和配置文件中的key必须要保持一致才可以注入成功)
    package com.hky.pojo;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    
    import java.util.List;
    
    /**
     * @author hky
     * @Description
     *
     *  读取配置文件信息:
     *      1. @Value读取
     *          只能读取一个值,不能读取数组
     *      2. 批量读取 @ConfigurationProperties(prefix = "")
     */
    @Data
    @Component
    @ConfigurationProperties(prefix = "hky.info")
    public class User {
    
        private String name;
    
        private String age;
    
        private List<String> anims;
    
    }
    
    

2.5 多环境配置和使用

  • 通过yaml方式实现多环境配置
    application-dev.yaml

    hky:
      info:
        name: hekaili
    

    application-test.yaml

    hky:
      info:
        age: 20
    

    application.yaml

    
    server:
      port: 8081
      servlet:
        context-path: /huahua
    
    hky:
      info:
        name: hekaiyan  #此时,冒号属性后面必须有一个空格
        age: 18
        anims:          #数组
          - dog
          - cat
    
    
    spring:
      profiles:
        active: test,dev  # 激活外部配置
                          # 外部配置的 key 与 application 的 key 重复,外部配置覆盖
    
  • controller类

    @RestController
    @RequestMapping("user")
    public class HelloController {
    
        @Autowired
        private User user;
    
        @GetMapping("show")
        public User show(){
            return user;
        }
    
    }
    
  • 测试显示
    在这里插入图片描述

  • 也可使用 properties 以及 命令行参数 实现多环境配置

举报

相关推荐

0 条评论