spring常用方法

1. 读取配置文件信息

方式一:

java 复制代码
// 获取文件路径
String fileName = "application.yaml";
String filePath = this.getClass().getClassLoader().getResource(fileName).getPath();

BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
Stringline = null;
StringBuilder fileData = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
    fileData.append(line).append(System.lineSeparator());
}
bufferedReader.close();

//或
StringBuilder fileData = new StringBuilder();
int value;
while ((value = bufferedReader.read()) != -1) {
    char c = (char) value;
    fileData.append(c);
}
bufferedReader.close();

//或使用hutool的IoUtil
String fileData = IoUtil.read(new FileInputStream(path), Charset.forName("UTF-8"));
System.out.println(fileData);

方式二:

java 复制代码
String fileName = "application.yaml";
ClassPathResource classPathResource = new ClassPathResource(fileName);
String fileData = IoUtil.read(classPathResource.getStream(), Charset.forName("utf-8"));

方式三:

java 复制代码
String fileData = IoUtil.read(JDBCUtils.class.getClassLoader().getResourceAsStream("application.yaml"), Charset.forName("UTF-8"));
System.out.println(fileData);

方式四:

java 复制代码
String fileData = IoUtil.read(ResourceUtil.getStream("application.yaml"), Charset.forName("UTF-8"));
System.out.println(fileData);
相关推荐
云烟成雨TD21 小时前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
Java成神之路-1 天前
SpringMVC 响应实战指南:页面、文本、JSON 返回全流程(Spring系列13)
java·spring·json
砍材农夫1 天前
spring-ai 第六模型介绍-聊天模型
java·人工智能·spring
云烟成雨TD1 天前
Spring AI Alibaba 1.x 系列【5】ReactAgent 构建器深度源码解析
java·人工智能·spring
Flittly1 天前
【SpringAIAlibaba新手村系列】(15)MCP Client 调用本地服务
java·笔记·spring·ai·springboot
Flittly1 天前
【SpringAIAlibaba新手村系列】(14)MCP 本地服务与工具集成
java·spring boot·笔记·spring·ai
mfxcyh1 天前
基于xml、注解、JavaConfig实现spring的ioc
xml·java·spring
Flittly1 天前
【SpringAIAlibaba新手村系列】(13)Tool Calling 函数工具调用技术
java·spring boot·spring·ai
xdscode1 天前
Spring 依赖注入方式全景解析
java·后端·spring