当前位置:网站首页>PathMatchingResourcePatternResolver解析配置文件 资源文件
PathMatchingResourcePatternResolver解析配置文件 资源文件
2022-07-26 04:04:00 【抓手】
使用方法:
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource resource = resourcePatternResolver.getResource("youResourcePath");如果不是根路径,需要指定路径,例如:

从类路径的rules目录下获取demo.drl文件
Resource resource = resourcePatternResolver .getResource("rules/demo.drl");可以明确的指定classpath前缀:
Resource resource = resourcePatternResolver .getResource("classpath:rules/demo.drl");使用通配符获取满足某种格式的文件
通过classpath*前缀指定从所有的类路径下获取指定的文件
Resource[] resource = resourcePatternResolver .getResource("classpath*:rules/demo.drl");获取rules目录下前缀为demo的文件
Resource[] resource = resourcePatternResolver .getResource("classpath*:rules/demo*.drl");获取com.a.rules、com.b.rules、com.c.rules目录下的demo的文件
Resource[] resource = resourcePatternResolver .getResource("classpath*:com/*/rules/demo.drl");两个*表示任意多层的目录
Resource[] resource = resourcePatternResolver .getResource("classpath*:com/**/rules/demo.drl");将读取的资源文件转换成字符串:
import org.apache.poi.util.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException;
/**
* @author 向振华
* @date 2022/07/25 10:59
*/
public class ResourceUtils {
/**
* 读取配置文件并转换成字符串
*
* @param path 路径,eg: classpath:META-INF/xxx/spring.factories
* @return
*/
public static String read(String path) {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource resource = resourcePatternResolver.getResource(path);
try {
byte[] bytes = IOUtils.toByteArray(resource.getInputStream());
return new String(bytes);
} catch (IOException e) {
return null;
}
}
}边栏推荐
- php 查找 session 存储文件位置的方法
- Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop
- booking.com缤客上海面经
- Apple removed the last Intel chip from its products
- 【读书笔记->数据分析】BDA教材《数据分析》书籍介绍
- operator new、operator delete补充讲义
- General test case writing specification
- 通用测试用例写作规范
- [Reading Notes - > data analysis] 01 introduction to data analysis
- Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package
猜你喜欢

【Unity3d Shader】角色投影与倒影

Inventory the concept, classification and characteristics of cloud computing

If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network

Find My技术|物联网资产跟踪市场规模达66亿美元,Find My助力市场发展

Kbpc1510-asemi large chip 15A rectifier bridge kbpc1510

Six years of automated testing from scratch, I don't regret turning development to testing

容器跑不动?网络可不背锅

工程师如何对待开源 --- 一个老工程师的肺腑之言
![[programmers must] Tanabata confession strategy:](/img/55/0b43dd18c8682250db13ad94cd2c2c.png)
[programmers must] Tanabata confession strategy: "the moon meets the cloud, the flowers meet the wind, and the night sky is beautiful at night". (with source code Collection)
随机推荐
Testing is not valued? Senior: you should think in another position
Data elements
[programmers must] Tanabata confession strategy: "the moon meets the cloud, the flowers meet the wind, and the night sky is beautiful at night". (with source code Collection)
PHP installation spool supports dtls protocol
Pits encountered by sdl2 OpenGL
2022 Hangzhou Electric Multi school bowcraft
redux
按键消抖的Verilog实现
微信小程序实现音乐播放器(5)
Analysis on the infectious problem of open source license
Oracle 11g "password delayed verification" feature
booking.com缤客上海面经
Chinese database oceanbase was selected into the Forrester translational data platform report
Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop
安装VMware报错failed to install the hcmon driver
《opencv学习笔记》-- 霍夫变换
STM32状态机编程实例——全自动洗衣机(下)
【单片机仿真项目】外部中断0和1控制两位数码管进行计数
Tactile intelligent sharing-rk3568 application in scenic spot navigation robot
Wechat applet realizes music player (5)