Java属性文件绑定到Java接口

Lui*_*ano 3 java api configuration properties

使用GWT你有这样的东西:

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

   @DefaultStringValue("Ok")
   @Key("okButtonLabel")
   String okButtonLabel();
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Lui*_*ano 7

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

基本用法

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

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

port=80
hostname=foobar.com
maxThreads=100
Run Code Online (Sandbox Code Playgroud)

要访问此属性,您需要在以下位置定义方便的Java接口ServerConfig.java:

public interface ServerConfig extends Config {
    int port();
    String hostname();
    int maxThreads();
}
Run Code Online (Sandbox Code Playgroud)

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

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

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

但这只是冰山一角.

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

我仍然有几个功能,但目前的实现比问题中描述的基本功能稍微前进.

我需要添加样本和文档.