环境

  1. jboss 5.2

原因

加载资源的协议错误。一般在加载文件的时候,URL 都是以 file: 开头,但是在 jboss 上时,由于其虚拟化了路径,导致协议不一致,并且找不到外部的配置文件。

分析

通过将项目部署到 jboss 服务器上,打印日志查看其获得的 URL 如下:

vfsmemory://a653x1c-xfikka-k3i9k2ku-1-k3i9kk9n-2s/

该目录结构已被虚拟化,不能够获得服务器中的目录结构。

外部配置

优化后的外部配置,使用 tomcat 服务器是可以了,由于 jboss 比较特殊,不能够获取服务器真实的相对路径。可以使用 getClass().getResource("") ,即使用当前类的相对路径来解决。

// classpath 目录,上级目录的个数和当前类的类名层级相对应。
String path = PathDemo.class.getResource("../../").getPath();
Resource resource = new FileSystemResource(path + EXTERNAL_CONFIG_FILE); try { logger.info("外部配置目录为:{}", resource.getFile().getCanonicalPath()); if (!resource.exists()) { logger.info("外部配置不存在。");
return;
} ResourcePropertySource source = new ResourcePropertySource(new EncodedResource(resource, "UTF-8"));
// 外部化配置的优先级最高
beanFactory.getBean(StandardEnvironment.class).getPropertySources().addFirst(source); } catch (IOException e) { logger.error("加载外部化配置出错。", e);
}

拓展

如果使用该种方法,发现目录结构完全正确,但始终不能找到配置文件,

则可以参考这里

附录

获取 jboss 目录

logger.info("class / :{}", getClass().getResource("/"));
logger.info("class :{}", getClass().getResource(""));
logger.info("file class / :{}", getClass().getResource("/").getFile());
logger.info("file class :{}", getClass().getResource("").getFile());
logger.info("path class / :{}", getClass().getResource("/").getPath());
logger.info("path class :{}", getClass().getResource("").getPath());
logger.info("loader / :{}", getClass().getClassLoader().getResource("/"));
logger.info("loader:{}", getClass().getClassLoader().getResource(""));
logger.info("file loader / :{}", getClass().getClassLoader().getResource("/").getFile());
logger.info("file loader:{}", getClass().getClassLoader().getResource("").getFile());
logger.info("path loader / :{}", getClass().getClassLoader().getResource("/").getPath());
logger.info("path loader:{}", getClass().getClassLoader().getResource("").getPath());
logger.info("thread /: {}", threadLoader.getResource("/"));
logger.info("thread : {}", threadLoader.getResource(""));
logger.info("file thread /: {}", threadLoader.getResource("/").getFile());
logger.info("file thread : {}", threadLoader.getResource("").getFile());
logger.info("path thread /: {}", threadLoader.getResource("/").getPath());
logger.info("path thread : {}", threadLoader.getResource("").getPath());

对应输出结果

 class / :vfsmemory://a653x1c-xfikka-k3i9k2ku-1-k3i9kk9n-2s/
class :vfszip:/DATA/app/jboss/appdeploy/demo.war/WEB-INF/classes/jiangbo/demo/
file class / :/
file class :/DATA/app/jboss/appdeploy/demo.war/WEB-INF/classes/jiangbo/demo/
path class / :/
path class :/DATA/app/jboss/appdeploy/demo.war/WEB-INF/classes/jiangbo/demo/
loader / :vfsmemory://a653x1c-xfikka-k3i9k2ku-1-k3i9kk9n-2s/
loader:vfsmemory://a653x1c-xfikka-k3i9k2ku-1-k3i9kk9n-2s/
file loader / :/
file loader:/
path loader / :/
path loader:/
thread /: vfsmemory://a653x1c-xfikka-k3i9k2ku-1-k3i9kk9n-2s/
thread : vfsmemory://a653x1c-xfikka-k3i9k2ku-1-k3i9kk9n-2s/
file thread /: /
file thread : /
path thread /: /
path thread : /

最新文章

  1. C#源码500份
  2. 黄聪:发送键盘指令System.Windows.Forms.SendKeys.Send
  3. Spring Batch的事务– Part 3: 略过和重试
  4. 手机web开发Repeater四层嵌套
  5. CSS模块化
  6. MVC学习系列——参考
  7. GCD多线程
  8. Android Studio中如何创建AIDL
  9. FormView用法
  10. OpenGL: Rotation vector sensor of Android and Device motion of iOS
  11. [例子]Ubuntu虚拟机设置固定IP上网
  12. 2153 ACM 仙人球的残影 输出格式
  13. BZOJ4771七彩树——可持久化线段树+set+树链的并+LCA
  14. 功率谱密度(PDS)的MATLAB分析
  15. Week4-作业1:阅读与博客
  16. 43:n个骰子的点数
  17. eclipes设置默认注释作者
  18. 使用minikube在本机测试kubernetes
  19. mysql hql异常
  20. request Dispatch

热门文章

  1. bbs--文章后台
  2. 对AM信号FFT的matlab仿真
  3. asmlinkage的用法
  4. Mybatis源码学习之DataSource(七)_2
  5. Qt控制台输出QString
  6. 博客&git收藏
  7. ubuntu下如何使用apt-get安装arm64的交叉编译工具链?
  8. 信息学竞赛一本通提高版AC题解—例题1.1活动安排
  9. Jenkins的详细安装及使用
  10. sqlserver2008 job设定