Java属性文件绑定到Java接口 [英] Java Properties File binding to Java Interface

查看:200
本文介绍了Java属性文件绑定到Java接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GWT你有这样的东西:

With GWT you have stuff like this:

public interface LoginConstants extends Constants {
   @DefaultStringValue("Wellcome to my super app")
   @Key("appDescription")
   String appDescription();

   @DefaultStringValue("Ok")
   @Key("okButtonLabel")
   String okButtonLabel();
}

然后你可以在你的类中使用GWT.create(LoginConstant.class)通过这种方式,接口由动态实现支持,当我调用loginConstants.appDescription()时,使用@Key批注返回属性文件中包含的值,以引用属性文件中的键。如果属性文件遗漏了该属性,则返回de @DefaultStringValue。这用于国际化,但也可能用于配置。
但是对于GWT,这意味着在客户端使用(即转换为JavaScript),对于i18n,而不是用于配置。

Then you can use from your classes doing GWT.create(LoginConstant.class), in this way the interface is backed by dynamic implementation that, when I call loginConstants.appDescription() returns the value contained from a property file using the @Key annotation to reference the key in the property file. If the property file misses the property, then de @DefaultStringValue is returned. This is used for internationalization, but can possibly work also for configuration. But with GWT, this is meant to be used on the client side (ie. translated to JavaScript), and for i18n, not for configuration.

但是,我发现这个想法对于配置处理也非常方便。

But, I find this idea very convenient also for configuration handling.

我想知道是否有人知道在服务器端执行类似操作的框架,而不必将代码绑定到GWT。即。如果有任何库实现了专门为配置处理而设计的这种逻辑。我不知道这样的事情。

I wonder if somebody knows a framework to do a similar thing on the server side, without necessarily bind your code to GWT. ie. if there is any library that implements this kind of logic specifically designed for the configuration handling. I am not aware of anything like this.

参考GWT中的功能: https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nConstants

Reference to the feature in GWT: https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nConstants

推荐答案

我实现了自己的问题解决方案:

I implemented my own solution to the question:


基本使用

OWNER API使用的方法是定义与属性文件关联的Java接口

The approach used by OWNER APIs, is to define a Java interface associated to a properties file.

假设您的属性文件定义为 ServerConfig.properties

Suppose your properties file is defined as ServerConfig.properties:

port=80
hostname=foobar.com
maxThreads=100

To访问此属性,您需要在 ServerConfig.java中定义一个方便的Java接口

To access this property you need to define a convenient Java interface in ServerConfig.java:

public interface ServerConfig extends Config {
    int port();
    String hostname();
    int maxThreads();
}

我们将此接口称为属性映射接口或仅
映射接口,因为它的目标是将属性映射到易于
使用一段代码。

We'll call this interface the Properties Mapping Interface or just Mapping Interface since its goal is to map Properties into an easy to use a piece of code.

然后,您可以在代码中使用它:

Then, you can use it from inside your code:

public class MyApp {
    public static void main(String[] args) {
        ServerConfig cfg = ConfigFactory.create(ServerConfig.class);
        System.out.println("Server " + cfg.hostname() + ":" + cfg.port() +
                           " will run " + cfg.maxThreads());
    }
}

但这只是冰山一角。

继续阅读:基本用法 || 网站 || Github

Continue reading here: Basic usage || Website || Github

我还有几个考虑到这一点,但目前的实施方式比问题中描述的基本功能稍微前进了一些。

I still have a couple of features in mind, but the current implementation goes a little forward than the basic functionalities described in the questions.

我需要添加样本和文档。

I need to add samples and documentation.

这篇关于Java属性文件绑定到Java接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆