[JavaWeb]【十】web后端开发-SpringBootWeb案例(配置文件)

目录

一、参数配置化

1.1 问题分析

1.2 问题解决(application.properties)

1.2.1 application.properties

 1.2.2 AliOSSUtils 

1.2.3 启动服务-测试

二、yml配置文件

2.1 配置格式

2.1.1 新增 application.yml 

2.1.2 启动服务

2.2 XML与properties与yml对比

2.3 yml基本语法 

 2.4 yml数据格式

三、 实现application.yml 

3.1 application.yml 

3.2 删除application.properties

3.3 启动服务-测试 

四、ConfigurationProperties

4.1 问题分析

 4.2 问题解决

 4.2.1 新增实体类AliOSSProperties

 4.2.2 优化AliOSSUtils

 4.2.3 启动服务-测试

 4.2.4 处理上面的警告-添加依赖

 4.3 @ConfigurationProperties与@Value区别


前言:

SpringBoot的配置文件是在应用程序启动时读取的外部配置文件。在SpringBoot中,配置文件可以使用多种格式来编写,包括properties、yaml、xml等。通过配置文件,我们可以对应用程序进行各种配置,如数据库配置、日志配置、缓存配置等。

以下是SpringBoot配置文件的说明:

  1. 配置文件的命名规则:application.properties或application.yml,位于/src/main/resources目录下。

  2. 配置文件的优先级:SpringBoot会从以下位置读取配置文件,并按照优先级顺序覆盖配置:

    1)命令行参数:通过--spring.config.name和--spring.config.location参数指定配置文件路径。

    2)jar包外部的application.properties或application.yml文件。

    3)jar包内部的application.properties或application.yml文件。

    4)jar包内部的application-{profile}.properties或application-{profile}.yml文件。

    5)jar包外部的application-{profile}.properties或application-{profile}.yml文件。

    6)jar包内部的config目录下的application.properties或application.yml文件。

    7)jar包外部的config目录下的application.properties或application.yml文件。

  3. 配置文件的基本格式:

    1)properties格式:

    key=value

    2)yaml格式:

    key: value

  4. 配置文件的常用属性:

    1)server.port:应用程序的端口号。

    2)spring.datasource.url:数据库连接URL。

    3)spring.datasource.username:数据库用户名。

    4)spring.datasource.password:数据库密码。

    5)spring.datasource.driver-class-name:数据库驱动类名。

    6)spring.jpa.show-sql:是否显示SQL语句。

    7)logging.level.root:日志级别。

    8)spring.profiles.active:激活的配置文件。

  5. 配置文件的注释:在properties格式中使用#注释,在yaml格式中使用#或者先加上“#!”表示注释。

 

一、参数配置化

1.1 问题分析

1.2 问题解决(application.properties)

1.2.1 application.properties

 1.2.2 AliOSSUtils 

package com.runa.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;

/**
 * 阿里云 OSS 工具类
 */
@Component
public class AliOSSUtils {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    /**
     * 实现上传图片到OSS
     */
    public String upload(MultipartFile file) throws IOException {
        // 获取上传的文件的输入流
        InputStream inputStream = file.getInputStream();

        // 避免文件覆盖
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

        //上传文件到 OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);

        //文件访问路径
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        // 关闭ossClient
        ossClient.shutdown();
        return url;// 把上传到oss的路径返回
    }

}

1.2.3 启动服务-测试

 

 

二、yml配置文件

2.1 配置格式

2.1.1 新增 application.yml 

 

server:
  port: 9000

2.1.2 启动服务

2.2 XML与properties与yml对比

2.3 yml基本语法 

 

 2.4 yml数据格式

三、 实现application.yml 

3.1 application.yml 

spring:
  # 数据库的连接信息
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springbootproject
    username: root
    password: VVVV
  # 文件上传配置
  servlet:
    multipart:
      # 配置单个文件最大上传大小
      max-file-size: 10MB
      # 配置单个请求最大大小的限制(一次请求中是可以上传多个文件)
      max-request-size: 100MB
#Mybatis配置
mybatis:
  configuration:
    #配置mybatis的日志, 指定输出到控制台
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #开启mybatis的驼峰命名自动映射开关 a_column ------> aCloumn
    map-underscore-to-camel-case: true

# 阿里云OSS配置
aliyun:
  oss:
    endpoint: https://oss-cn-beijing.aliyuncs.com
    accessKeyId: LTAVVsfdsfsdfsfsd
    accessKeySecret: Piy4Tzdc1lfsfsZoLum9ALHTHm6sR
    bucketName: web-sprifdfdfocai


3.2 删除application.properties

3.3 启动服务-测试 

四、ConfigurationProperties

4.1 问题分析

 4.2 问题解决

 4.2.1 新增实体类AliOSSProperties

 

package com.runa.utils;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
}

 出现下图警告后面处理

 

 4.2.2 优化AliOSSUtils

package com.runa.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;

/**
 * 阿里云 OSS 工具类
 */
@Component
public class AliOSSUtils {

//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
//    @Value("${aliyun.oss.accessKeyId}")
//    private String accessKeyId;
//    @Value("${aliyun.oss.accessKeySecret}")
//    private String accessKeySecret;
//    @Value("${aliyun.oss.bucketName}")
//    private String bucketName;
    @Autowired
    private AliOSSProperties aliOSSProperties;

    /**
     * 实现上传图片到OSS
     */
    public String upload(MultipartFile file) throws IOException {
        // 获取阿里云OSS参数
        String endpoint = aliOSSProperties.getEndpoint();
        String accessKeyId = aliOSSProperties.getAccessKeyId();
        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
        String bucketName = aliOSSProperties.getBucketName();
        // 获取上传的文件的输入流
        InputStream inputStream = file.getInputStream();

        // 避免文件覆盖
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

        //上传文件到 OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);

        //文件访问路径
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        // 关闭ossClient
        ossClient.shutdown();
        return url;// 把上传到oss的路径返回
    }

}

 4.2.3 启动服务-测试

 4.2.4 处理上面的警告-添加依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>3.1.1</version>
</dependency>

 4.3 @ConfigurationProperties与@Value区别

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值