# 获取类路径资源 ResourcePatternResolver&PathMatchingResourcePatternResolver
最编程
2024-03-30 13:48:43
...
【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
ResourcePatternResolver介绍
用于解析资源文件的策略接口,其特殊的地方在于,它应该提供带有*号这种通配符的资源路径。此接口是ResourceLoader接口的拓展接口。
源码:
public interface ResourcePatternResolver extends ResourceLoader {
/**
* 在所有根目录下搜索文件的伪URL的前缀
* 与ResourceLoader中classpath不同的地方在于,此前缀会在所有的JAR包的根目录下搜索指定文件。
* @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX
*/
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
/**
* 返回指定路径下所有的资源对象。
* 返回的对象集合应该有Set的语义,也就是说,对于同一个资源,只应该返回一个资源对象
* @param locationPattern the location pattern to resolve
* @return the corresponding Resource objects
* @throws IOException in case of I/O errors
*/
Resource[] getResources(String locationPattern) throws IOException;
}
1. 读取resource下的文件
@Autowired
private ResourcePatternResolver resourcePatResolver;
private List<String> forestsName = new ArrayList<>();
@PostConstruct
private void postConstructInit() throws Exception {
//加载自定义词典
for(Resource resource: resourcePatResolver.getResources("classpath:dics/userLibrary/*.dic")) {
//TODO
Forest forest = Library.makeForest(resource.getInputStream());
String key = "dic_" + resource.getFilename().replace(".dic", "");
DicLibrary.put(key, key, forest);
forestsName.add(key);
}
}
1. 读取resource下的文件
@Autowired
private ResourcePatternResolver resourcePatResolver;
private List<String> forestsName = new ArrayList<>();
@PostConstruct
private void postConstructInit() throws Exception {
//加载自定义词典
for(Resource resource: resourcePatResolver.getResources("classpath:dics/userLibrary/*.dic")) {
//TODO
Forest forest = Library.makeForest(resource.getInputStream());
String key = "dic_" + resource.getFilename().replace(".dic", "");
DicLibrary.put(key, key, forest);
forestsName.add(key);
}
}
PathMatchingResourcePatternResolver介绍
PathMatchingResourcePatternResolver是一个Ant模式通配符的Resource查找器,可以用来查找类路径下或者文件系统中的资源。
1. 加载当前类路径中所有匹配的资源
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//classpath: 表示加载当前类路径中所有匹配的资源
Resource[] resources = resourcePatternResolver.getResources("classpath:dics/userLibrary/*.dic");
for(Resource r : resources){
// 文件名
System.out.println(r.getFilename());
// 文件绝对路径
System.out.println(r.getURL().getPath());
// File对象
System.out.println(r.getFile());
// InputStream对象
System.out.println(r.getInputStream());
}
2. 加载类路径中所有匹配的资源
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
// classpath*: 表示加载类路径中所有匹配的资源
resources = resourcePatternResolver.getResources("classpath*:com/haodf/**/*.class");
for(Resource r : resources){
// 文件绝对路径
System.out.println("文件绝对路径:" + r.getURL().getPath());
}
3. 加载文件系统中的资源
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
// 加载文件系统中的资源
Resource r = resourcePatternResolver.getResource("file:/Users/lihuan/Documents/projects/git/me/default.dic");
// 读取文件内容
System.out.println("读取文件内容:" + r.getInputStream());
推荐阅读
-
# 获取类路径资源 ResourcePatternResolver&PathMatchingResourcePatternResolver
-
java 获取资源目录路径
-
java 获取项目资源的相对路径。
-
linux 获取 Java 资源路径
-
java 学习路径: 4.字符串类 连接字符串 获取字符串信息
-
如何在Logback中获取和显示完整的类包路径名称
-
玩转Java底层:JMX详解 - jconsole与自定义MBean监控工具的实际应用与区别" 在日常JVM调优中,我们熟知的jconsole工具通过JMX包装的bean以图形化形式展示管理数据,而像jstat和jmap这类内建监控工具则由JVM直接支持。本文将以jconsole为例,深入讲解其实质——基于JMX的MBean功能,包括可视化界面上的bean属性查看和操作调用。 MBeans在jconsole中的体现是那些可观察的组件属性和方法,如上图所示,通过名为"Verbose"的属性能看到其值为false,同时还能直接操作该bean的方法,例如"closeJerryMBean"。 尽管jconsole给我们提供了直观的可视化界面,但请注意,这里的MBean并非固定不变,开发者可根据JMX提供的接口将自己的自定义bean展示到jconsole。以下步骤展示了如何创建并注册一个名为"StudyJavaMBean"的自定义MBean: 1. 首先定义接口`StudyJavaMBean`,接口需遵循MBean规范,即后缀为"MBean"且包含getter方法代表属性,如`getApplicationName`,和无返回值的setter方法代表操作,如`closeJerryMBean`。 ```java public interface StudyJavaMBean { String getApplicationName(); void closeJerryMBean(); } ``` 2. 编写接口的实现类`StudyJavaMBeanImpl`,实现接口中的方法: ```java public class StudyJavaMBeanImpl implements StudyJavaMBean { @Override public String getApplicationName() { return "每天学Java"; } @Override public void closeJerryMBean() { System.out.println("关闭Jerry应用"); } } ``` 3. 在代码中注册自定义MBean,涉及的关键步骤包括: - 获取平台MBeanServer - 定义ObjectName,指定唯一的MBean标识符 - 注册MBean到服务器 - 启动RMI连接器服务,以便jconsole能够访问 ```java public void registerMBean() throws Exception { // ... 具体实现省略 ... } ``` 实际运行注册后的MBean,您将在jconsole中发现并查看自定义bean的属性和调用相关方法。然而,这种方式相较于传统的属性/日志查看和HTTP接口,实用性相对有限,可能存在潜在的安全风险。但不可否认的是,JMX及其MBean机制对于获取操作系统信息、内存状态等关键性能指标仍然具有重要价值。例如: 1. **获取操作系统信息**:通过JMX MBean,可以直接获取到诸如CPU使用率、操作系统版本等系统级信息,这对于资源管理和优化工作具有显著帮助。