JBossは:トップパス上のリバースパスを使用:/ XXX

環境

  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 : /

おすすめ

転載: www.cnblogs.com/jiangbo44/p/11953601.html