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

查看:30
本文介绍了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.IE.如果有任何库实现了专门为配置处理设计的这种逻辑.我不知道有这样的事情.

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

要访问此属性,您需要定义一个方便的 Java 接口在 ServerConfig.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天全站免登陆