doe*_*man 10 java configuration glassfish java-ee
我正在创建一个在GlassFish中运行的Web服务,我想要一些自定义属性.为此,我正在Properties上课.我正在使用的代码是:
Properties p=new Properties();
File f=new File(System.getProperty("user.dir"), "settings.properties");
p.load(new FileInputStream(f));
Run Code Online (Sandbox Code Playgroud)
但是如何settings.properties在配置目录中获取-file?
我不确定我的类路径,因为它由NetBeans和GlassFish管理.我假设.war在部署时我的-file被添加到类路径中...
我已经添加了自己的解决方案,但如果有人能提出更好的解决方案,那将非常受欢迎......
Bre*_*lay 11
将属性文件放在<glassfish-install-dir>/glassfish/domains/<domain-name>/lib/classes目录中,可以通过ResourceBundle类从应用程序中访问它们.例如,将名为settings.properties的属性文件添加到此目录,然后从文件中访问值,如下所示:
ResourceBundle.getBundle("设置")的getString("我的属性键").
有效的解决方案实际上非常简单:
URL url = this.getClass().getResource("/package/name/file.properties");
p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));
Run Code Online (Sandbox Code Playgroud)
为什么没有人来这个?