当前位置: 首页 > news >正文

Spring Boot自定义配置项

Spring Boot自定义配置项

配置文件

application.properties文件添加需要的配置
比如:

file.path=D:\\flies\\springboot\\

@ConfigurationProperties 注解

使用注解@ConfigurationProperties将配置项和实体Bean关联起来,实现配置项和实体类字段的关联,读取配置文件数据。

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "file")
public class FileConfig {private String path;
}

使用

获取配置信息

FileConfig fileConfig = new FileConfig();
// 文件保存目录
String filePath = fileConfig.getPath();
    @PostMapping("/upload/")@ResponseBodypublic  Response upload(MultipartFile file) {// 验证是否有文件if(file == null || file.isEmpty()){return Response.newFail("Upload failed, please select file",400);}FileConfig fileConfig = new FileConfig();// 文件保存目录String filePath = fileConfig.getPath();// 验证文件夹File folder = new File(filePath);if (!folder.exists()) {folder.mkdirs();}// 文件名String fileName = UUID.randomUUID() + file.getOriginalFilename();filePath = filePath  + fileName;File saveFile = new File(filePath);try {file.transferTo(saveFile);return  Response.newSuccess("Upload successful");} catch (IOException e) {e.printStackTrace();return  Response.newFail("Upload failed",50001);}}
http://www.lryc.cn/news/444729.html

相关文章:

  • 【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
  • 什么时候用synchronized,什么时候用Reentrantlock
  • [ffmpeg]音频格式转换
  • SSRF工具类-SsrfTool
  • python集合运算介绍及示例代码
  • 『功能项目』按钮的打开关闭功能【73】
  • Linux 常用命令 - more 【分页显示文件内容】
  • Kotlin Android 环境搭建
  • 常见协议及其默认使用的端口号
  • 04-Docker常用命令
  • 数字化转型中的供应链管理优化
  • 【Python报错已解决】SyntaxError: invalid syntax
  • 树上差分+lca 黑暗的锁链
  • opencv4.5.5 GPU版本编译
  • 线性跟踪微分器TD详细测试(Simulink 算法框图+SCL完整源代码)
  • LabVIEW闪退
  • 【WPF】03 动态生成控件
  • 调试LTE模块碰到的4字节对齐问题
  • 一篇讲完HTML核心内容
  • 2024icpc(Ⅱ)网络赛补题 G
  • AIGC时代!AI的“iPhone时刻”与投资机遇
  • Kubernetes调度单位Pod
  • C语言指针篇
  • Unity 使用Editor工具查找 Prefab 中的指定脚本
  • Frida-JSAPI:Interceptor使用
  • 【深度学习】(3)--损失函数
  • git学习报告
  • Spring MVC的应用
  • JavaEE: 深入探索TCP网络编程的奇妙世界(六)
  • 探秘 Web Bluetooth API:连接蓝牙设备的新利器