Java 如何在 GlassFish 中使用属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/531593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:54:06  来源:igfitidea点击:

How to use a property-file with GlassFish

javaconfigurationjakarta-eeglassfish

提问by doekman

I'm creating a web service, which run in GlassFish, and I want to have some custom properties. For this I'm using the Propertiesclass. The code I'm using is:

我正在创建一个在 GlassFish 中运行的 Web 服务,并且我想要一些自定义属性。为此,我正在使用Properties该类。我正在使用的代码是:

Properties p=new Properties();
File f=new File(System.getProperty("user.dir"), "settings.properties");
p.load(new FileInputStream(f));  

But how do I get the settings.properties-file in my config directory?

但是如何settings.properties在我的 config 目录中获取-file 呢?

I'm not sure about my classpath, since this is managed by NetBeans and GlassFish. I assume my .war-file is added to the classpath, when deploying...

我不确定我的类路径,因为这是由 NetBeans 和 GlassFish 管理的。我假设我的.war-file 已添加到类路径中,在部署时...

I've added my own solution, but if anyone could come up with a better solution, it would be highly welcome...

我已经添加了我自己的解决方案,但是如果有人能提出更好的解决方案,那将是非常受欢迎的......

采纳答案by doekman

The solution that works is actually pretty simple:

有效的解决方案实际上非常简单:

URL url =  this.getClass().getResource("/package/name/file.properties");
p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

Why didn't anybody come with this?

为什么没有人带着这个来?

回答by kgiannakakis

See herefor how you can read a properties file from your classpath:

请参阅此处了解如何从类路径中读取属性文件:

URL url =  ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

You then only need to add your config directory to the classpath.

然后您只需要将您的配置目录添加到类路径中。

If you have problems using the above code try ServletContext.getResource.

如果您在使用上述代码时遇到问题,请尝试ServletContext.getResource

回答by cletus

+1 for putting it in your classpath.

+1 用于将其放入您的类路径中。

If you're using Spring (and I'd highly recommend you do if you're not already for many reasons) when you can load a properties file like this:

如果您正在使用 Spring(如果您出于多种原因还没有使用 Spring,我强烈建议您这样做),那么您可以加载这样的属性文件:

database.username=scott
database.password=tiger

and put references in your application context like:

并将引用放在您的应用程序上下文中,例如:

<property name="username" value="${database.username}"/>

(assuming you've configured the property configurator) and it will cause an error if the file can't be loaded or the property doesn't exist. The application will fail to start. This is actually a good thing. It lets you find problems really really quickly and much better than failing silently, which can sometimes have catastrophic effects.

(假设您已经配置了属性配置器),如果无法加载文件或属性不存在,则会导致错误。应用程序将无法启动。这其实是一件好事。它可以让您真正快速地发现问题,而且比默默地失败要好得多,后者有时会产生灾难性的影响。

回答by doekman

I've tried a lot, but I solved this with:

我已经尝试了很多,但我解决了这个问题:

        // ServletContext ctx
        InputStream stream = ctx.getResourceAsStream("version.properties");
        p = new Properties();
        p.load(stream);

I have to pass the ServletContext from a jsp-page with a call to getServletContext()getServletContext(). Not ideal, but it's the only way I could get it working...

我必须通过调用getServletContext()getServletContext(). 不理想,但这是我让它工作的唯一方法......

It would be nice though if anyone could come up with another solution, that could work withyout the ServletContext.

如果有人能想出另一种解决方案,那就太好了,如果没有ServletContext.

回答by fredarin

Alternatives:

备择方案:

Depending on how your domain is configured, you might be able to use asadmin create-system-propertiesfrom the command line. Run/see asadmin create-system-properties --helpfor more info.

根据域的配置方式,您或许可以asadmin create-system-properties从命令行使用。运行/查看asadmin create-system-properties --help更多信息。

Or you might like administering system properties through the Glassfish admin interface. Here's the default link: http://localhost:4848/configuration/systemProperties.jsf?configName=server-config

或者您可能喜欢通过 Glassfish 管理界面管理系统属性。这是默认链接:http://localhost:4848/configuration/systemProperties.jsf?configName=server-config

回答by mssb

Copy your property file to the web/WEB-INF/classes path

将您的属性文件复制到 web/WEB-INF/classes 路径

Properties p=new Properties();
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myproperty.properties"));

回答by Brent Clay

Place your property files in the <glassfish-install-dir>/glassfish/domains/<domain-name>/lib/classesdirectory and they will be accessible from within your applications via the ResourceBundleclass. For example, add a property file named settings.propertiesto this directory and then access values from the file like this:

将您的属性文件放在<glassfish-install-dir>/glassfish/domains/<domain-name>/lib/classes目录中,它们将可以通过ResourceBundle类从您的应用程序中访问。例如,将名为settings.properties的属性文件添加到此目录,然后从该文件中访问值,如下所示:

ResourceBundle.getBundle("settings").getString("my-property-key");

ResourceBundle.getBundle("settings").getString("my-property-key");

回答by thommy

Be aware that you have to close the InputStream. Otherwise you will get a SocketException sooner or later.

请注意,您必须关闭 InputStream。否则你迟早会得到一个 SocketException。

#|2013-xx-xxTxx:xx:xx.162+0200|WARNING|sun-appserver2.1|sun.rmi.transport.tcp|_ThreadID=431; _ThreadName=RMI TCP Accept-0;_RequestID=xyz;|RMI TCP Accept-0: accept loop for ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=42384] throws
java.net.SocketException: Too many open files
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390)
    at java.net.ServerSocket.implAccept(ServerSocket.java:453)
    at java.net.ServerSocket.accept(ServerSocket.java:421)
    at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:369)
    at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:341)
    at java.lang.Thread.run(Thread.java:662)
|#]

Loading Properties

加载属性