在Java Webapp和java普通应用程序中读取公共外部属性文件 [英] Read common external property file in Java Webapp and java normal app
问题描述
我知道这个问题已经回答了很多次,其中最有用的答案如下,
在何处放置以及如何在基于servlet的应用程序中读取配置资源文件?
但是,我们有以下特殊要求,
- 将部署Webapp to tomcat。
- .jar形式的普通java应用程序将放在文件夹/ myapp下
- myappConfig.property文件将放在/ myapp下
客户端计算机上的目录结构
/ myapp
/myapp.jar
/assests/myappConfig.property
/tomcat/webapps/myapp.war
基本上 myapp.jar 和 myapp.war 将访问MySql的sql db连接值数据库连接和数据库操作。
从myapp.jar访问myappConfig.property - >工作正常
文件jarPath =新文件(Myapp.class.getProtectionDomain()。getCodeSource()。getLocation()。getPath());
String propertiesPath = jarPath.getParent();
System.out.println(propertiesPath-+ propertiesPath);
mainProperties.load(new FileInputStream(propertiesPath +\\\\ setssets \\ myapp.property));
任何人都可以帮助/建议如何实施,
从mywebapp访问myappConfig.property文件,
如果myappConfig.property文件中的运行时更改,则不需要重新部署myapp.war
感谢您提前的帮助。
编辑
以下是我们希望将项目交付给客户的步骤。
-
下面是我的应用程序目录
/ myapp
/myapp.jar
/assests/myappConfig.property
/tomcat/webapps/myapp.war
-
将所有内容打包在一个包中一些打包工具。
-
在客户端计算机上的任何位置运行此包,它将具有与上面相同的目录结构
-
在这里,我不想硬编码webapp或tomcat中的任何位置/assests/myappConfig.property
-
普通的Java应用程序我可以读取属性文件但是对于wepapp我不知道如何做到这一点?
您可以将< context>
添加到tomcat / conf / server.xml(在此示例中为linux路径):
< Context docBase =/ home / yourusername / tomcat / assestspath =/ assets/>
如果您使用的是Windows:
< Context docBase =C:\ path\to\myapp\assetspath =/ assets/>
然后您可以像访问webapp中的任何其他资源一样访问它(例如:/ assets / myappConfig。例如,如果您使用JDBC,则可以将连接属性存储在Singleton中并从那里请求它,并且该类可以处理更改检查在该文件上。
I know this question has answered many a time with most useful answer as below,
Where to place and how to read configuration resource files in servlet based application?
However, We have some special requirement as below,
- Webapp will be deployed to tomcat.
- Normal java app in form of .jar will be placed under folder /myapp
- myappConfig.property file will be placed under /myapp
Directory Structure on client machine
/myapp
/myapp.jar
/assests/myappConfig.property
/tomcat/webapps/myapp.war
Basically myapp.jar and myapp.war will access sql db connection values for MySql database connection and db operations.
Accessing myappConfig.property from myapp.jar --> Working fine
File jarPath = new File(Myapp.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String propertiesPath = jarPath.getParent();
System.out.println(" propertiesPath-" + propertiesPath);
mainProperties.load(new FileInputStream(propertiesPath + "\\assets\\myapp.property"));
Can anyone help/suggest how to implement,
Accessing myappConfig.property file from mywebapp,
provided run time change in myappConfig.property file does not required myapp.war to be redeployed
Thanks for your help in advance.
edit
Below is the steps in which we want to deliver the project to client.
Below is my app directory
/myapp /myapp.jar /assests/myappConfig.property /tomcat/webapps/myapp.war
pack everything in one package with some packaging tool.
Run this package in client machine at any location and it will have same directory structure as above
Here, I do not want to hard code any location in webapp or tomcat for "/assests/myappConfig.property"
Normal Java app I can read property file but for wepapp I am not getting clear idea for how to do that?
You can add a <context>
to tomcat/conf/server.xml (in this example, linux path):
<Context docBase="/home/yourusername/tomcat/assests" path="/assets" />
If you are using Windows:
<Context docBase="C:\path\to\myapp\assets" path="/assets" />
And then you can access it like any other resource within your webapp (e.g.: /assets/myappConfig.property).
If you are using JDBC for example, you could store the connection properties in a Singleton and request it from there, and that class could take care of change checks on that file.
这篇关于在Java Webapp和java普通应用程序中读取公共外部属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!