可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm creating a web service, which run in GlassFish, and I want to have some custom properties. For this I'm using the Properties
class. The code I'm using is:
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?
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...
I've added my own solution, but if anyone could come up with a better solution, it would be highly welcome...
回答1:
Place your property files in the <glassfish-install-dir>/glassfish/domains/<domain-name>/lib/classes directory and they will be accessible from within your applications via the ResourceBundle class. For example, add a property file named settings.properties to this directory and then access values from the file like this:
ResourceBundle.getBundle("settings").getString("my-property-key");
回答2:
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?
回答3:
Alternatives:
Depending on how your domain is configured, you might be able to use asadmin create-system-properties
from the command line. Run/see asadmin create-system-properties --help
for more info.
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
回答4:
See here for 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.
回答5:
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...
It would be nice though if anyone could come up with another solution, that could work withyout the ServletContext
.
回答6:
+1 for putting it in your classpath.
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:
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.
回答7:
Copy your property file to the web/WEB-INF/classes path
Properties p=new Properties();
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myproperty.properties"));
回答8:
Be aware that you have to close the InputStream. Otherwise you will get a SocketException sooner or later.
#|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