1、用以下代码可以获取項目classpath的路径
ServerTimeGetTest.class.getResource("/")
ServerTimeGetTest.class.getClassLoader().getResource("")
例如:file:/D:\Users\hp\Desktop\tomcat\apache-tomcat-8.5.37\webapps\sps-sysfw-std\WEB-INF\classes
src/main/resources目录下的配置文件都会生成到该路径下
2、classpath*:和classpath:区别
3、classpath解析
classpath解析主要用到的类:PathMatchingResourcePatternResolver 类,位于包 org.springframework.core.io.support 下,该类中的 getResources 函数是逻辑的核心.
4、测试代码
ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
//classpath:applicationContextTest_*.xml
//classpath:applicationContextTest_common.xml
//classpath*:applicationContextTest_common.xml
Resource[] source = resourceLoader.getResources("classpath*:applicationContextTest_*.xml");
System.out.println("source.size: " + source.length);
for (int i =0; i < source.length; i++) {
Resource resource = source[i];
System.out.println(resource);
}
参考博客地址:https://blog.csdn.net/zhuqiuhui/article/details/65936889
网友评论