Java相当于app.config?

发布于 2024-07-07 06:28:44 字数 92 浏览 5 评论 0原文

Java 中是否有相当于 .NET 的 App.Config 的工具?

如果没有,是否有标准方法来保留应用程序设置,以便在应用程序分发后可以更改它们?

Is there a Java equivalent to .NET's App.Config?

If not is there a standard way to keep you application settings, so that they can be changed after an app has been distributed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠佳期 2024-07-14 06:28:44

对于 WebApps,web.xml 可用于存储应用程序设置。

除此之外,您可以使用 属性类来读取和写入属性文件。

您可能还想查看首选项 类,用于读写系统和用户首选项。 它是一个抽象类,但您可以使用 userNodeForPackage(ClassName.class)systemNodeForPackage(ClassName.class) 获取适当的对象。

For WebApps, web.xml can be used to store application settings.

Other than that, you can use the Properties class to read and write properties files.

You may also want to look at the Preferences class, which is used to read and write system and user preferences. It's an abstract class, but you can get appropriate objects using the userNodeForPackage(ClassName.class) and systemNodeForPackage(ClassName.class).

芸娘子的小脾气 2024-07-14 06:28:44

将 @Powerlord 使用 Properties 类的建议 (+1) 放入示例代码中:

public class SomeClass {
    public static void main(String[] args){
        String dbUrl = "";
        String dbLogin = "";
        String dbPassword = "";     
        if (args.length<3) {
            //If no inputs passed in, look for a configuration file
            URL configFile = SomeClass.class.getClass().getResource("/Configuration.cnf");
            try {
                InputStream configFileStream = configFile.openStream();
                Properties p = new Properties();
                p.load(configFileStream);
                configFileStream.close();

                dbUrl      = (String)p.get("dbUrl");
                dbLogin    = (String)p.get("dbUser");
                dbPassword = (String)p.get("dbPassword");               
            } catch (Exception e) {  //IO or NullPointer exceptions possible in block above
                System.out.println("Useful message");
                System.exit(1);
            }
        } else {
            //Read required inputs from "args"
            dbUrl      = args[0];
            dbLogin    = args[1];
            dbPassword = args[2];           
        }
        //Input checking one three items here
        //Real work here.
    }
}

然后,在容器的根目录(例如 jar 文件的顶部)放置一个文件 Configuration.cnf 包含以下内容:

#Comments describing the file
#more comments
dbUser=username
dbPassword=password
dbUrl=jdbc\:mysql\://servername/databasename

这感觉并不完美(我有兴趣听到改进),但足以满足我当前的需求。

To put @Powerlord's suggestion (+1) of using the Properties class into example code:

public class SomeClass {
    public static void main(String[] args){
        String dbUrl = "";
        String dbLogin = "";
        String dbPassword = "";     
        if (args.length<3) {
            //If no inputs passed in, look for a configuration file
            URL configFile = SomeClass.class.getClass().getResource("/Configuration.cnf");
            try {
                InputStream configFileStream = configFile.openStream();
                Properties p = new Properties();
                p.load(configFileStream);
                configFileStream.close();

                dbUrl      = (String)p.get("dbUrl");
                dbLogin    = (String)p.get("dbUser");
                dbPassword = (String)p.get("dbPassword");               
            } catch (Exception e) {  //IO or NullPointer exceptions possible in block above
                System.out.println("Useful message");
                System.exit(1);
            }
        } else {
            //Read required inputs from "args"
            dbUrl      = args[0];
            dbLogin    = args[1];
            dbPassword = args[2];           
        }
        //Input checking one three items here
        //Real work here.
    }
}

Then, at the root of the container (e.g. top of a jar file) place a file Configuration.cnf with the following content:

#Comments describing the file
#more comments
dbUser=username
dbPassword=password
dbUrl=jdbc\:mysql\://servername/databasename

This feel not perfect (I'd be interested to hear improvements) but good enough for my current needs.

情泪▽动烟 2024-07-14 06:28:44

简单的方法是简单地拥有一个属性文件,例如 myapp.properties,其中包含所有设置。这不是一种非常高级的设置方法,但它已经足够了,或者您可以拥有自己的基于 XML 的设置,或者从数据库等

The simple way is to simply have a properties file, e.g., myapp.properties, with all your settings in. It's not a very advanced way to do settings but it suffices, or you can have your own XML based setup, or get them from a database, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文