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);
相关推荐
张小洛8 小时前
Spring AOP 是如何生效的(入口源码级解析)?
java·后端·spring
追风少年浪子彦9 小时前
mapstruct与lombok冲突原因及解决方案
java·spring boot·spring·spring cloud
军军君019 小时前
基于Springboot+UniApp+Ai实现模拟面试小工具四:后端项目基础框架搭建下
spring boot·spring·面试·elementui·typescript·uni-app·mybatis
MZ_ZXD00111 小时前
flask校园学科竞赛管理系统-计算机毕业设计源码12876
java·spring boot·python·spring·django·flask·php
你我约定有三16 小时前
spring--xml注入时bean的property属性
xml·java·spring
喜欢敲代码的程序员17 小时前
Spring Boot中请求参数读取方式
java·spring boot·后端·spring
灰小猿18 小时前
多级@JsonTypeInfo和@JsonSubTypes注解使用详解及场景分析
java·后端·mysql·spring·spring cloud
NE_STOP18 小时前
SpringBoot--Profile你用对了吗
java·spring
小鱼人爱编程21 小时前
Java基石--无处不在的Java Class
java·后端·spring
JouJz1 天前
设计模式之单例模式:深入解析全局唯一对象的艺术
java·开发语言·spring·单例模式·设计模式·面试