简体   繁体   English

在JAVA中使用配置-最佳做法

[英]Using configuration in JAVA - best practice

In my case I have: 就我而言:

  1. LangDetector is java project with HtmlTags.config that exported to LangDetector.jar . LangDetector是带有HtmlTags.config的 Java项目, 导出到LangDetector.jar
  2. LangDetectionService is java jersey service that exported to WAR file. LangDetectionService是导出到WAR文件的java jersey服务。 LangDetectionService uses LangDetector.jar (has reference in buildpath) LangDetectionService使用LangDetector.jar (在buildpath中具有引用)

When I deploy WAR file on tomcat, I don't see HtmlTags.config in $CATALINA_BASE/webapps/LangDetectionService. 当我部署在Tomcat WAR文件,我没有看到HtmlTags.config在$ CATALINA_BASE / webapps /目录LangDetectionService。

I tried to add HtmlTags.config to LangDetectionService also, and after deploying HtmlTags.config is in $CATALINA_BASE/webapps/LangDetectionService folder, but it doesn't work. 我也尝试将HtmlTags.config添加到LangDetectionService中 ,并且在部署HtmlTags.config之后位于$ CATALINA_BASE / webapps / LangDetectionService文件夹中,但是它不起作用。

What's wrong here? 怎么了 What's the best practices to do it? 最佳做法是什么?

  1. Check that HtmlTags.config is inside LangDetector.jar. 检查HtmlTags.config是否在LangDetector.jar中。 It should be directly under jar file root. 它应该直接在jar文件的根目录下。
  2. Check that LangDetector.jar is inside LandDetectionService war file WEB-INF/lib 检查LangDetector.jar是否在LandDetectionService战争文件WEB-INF / lib中

If it is like that, HtmlTags.config should be in classpath and readable to your service. 如果是这样,则HtmlTags.config应该在类路径中,并且对您的服务可读。 But what code should read this HtmlTags.config file is unclear from your question. 但是您的问题尚不清楚应该读取此HtmlTags.config文件的什么代码。

You can try to read this file like that: 您可以尝试像这样读取此文件:

log.debug("HtmlTags.config exists: " + new File("HtmlTags.config").exists());

, where log is your logger. ,其中log是您的记录器。

And like always, you should start checking your server and application logs for errors and warnings. 与往常一样,您应该开始检查服务器和应用程序日志中的错误和警告。

And your question leaves me wonder, why you need to see that file in that directory? 您的问题让我感到奇怪,为什么您需要在该目录中查看该文件? If you want to read it from your application, then it should be in classpath. 如果您想从您的应用程序中读取它,那么它应该在类路径中。 Physical location is not really important, or is it? 地理位置不是很重要,还是吗?

It's solution. 解决方案。 1. Add HtmlTags.config to service project. 1.将HtmlTags.config添加到服务项目。 2. Change line "Configuration.class.getResourceAsStream("/" + configName)" to line "Configuration.class.getClassLoader ().getResourceAsStream(configName)" . 2.将“ Configuration.class.getResourceAsStream(“ /” + configName)”行更改为“ Configuration.class.getClassLoader().getResourceAsStream(configName)”行。 3. When LangDetector.jar will load HtmlTags.config it will get it from service project (behavior like in .NET) 3.当LangDetector.jar加载HtmlTags.config时,它将从服务项目中获取它(行为类似于.NET)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM