4

使用 GWT,您可以拥有以下内容:

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,而不是用于配置。

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

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

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

4

4 回答 4

7

我对这个问题实施了自己的解决方案:

基本用法

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

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

port=80
hostname=foobar.com
maxThreads=100

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

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

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

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

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

我仍然想到了一些功能,但是当前的实现比问题中描述的基本功能要进步一些。

我需要添加示例和文档。

于 2012-12-23T14:16:46.763 回答
2

我非常喜欢这个想法,因此我使用Java 动态代理快速组装了一些代码。

所以基本上你创建一个带有相关方法的接口,并用@Key、@DefaultStringValue 注释对它们进行注释。

以下是示例 Java 代码:

主.java

package net.viralpatel;

import net.viralpatel.annotations.DefaultStringValue;
import net.viralpatel.annotations.Key;

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

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

public class Main {
    public static void main(String[] args) {
        LoginConstants constants = DynamicProperty.create(LoginConstants.class);
        System.out.println(constants.appDescription());
        System.out.println(constants.okButtonLabel());
    }
}

我们加载的后台属性文件也是

配置属性

okButtonLabel=This is OK

只需执行 Main java 类,将显示以下输出:

输出:

Wellcome to my super app 
This is OK

这是其余的代码:http: //viralpatel.net/blogs/dynamic-property-loader-using-java-dynamic-proxy-pattern/

于 2012-12-13T16:18:19.750 回答
1

你可以用 spring 来模仿它(但我不确定它是否值得):

@Component
public class SomeBean {
   @Value("${appDescription:Wellcome to my super app}")
   private String appDescription;

   @Value("${okButtonLabel:Ok}")
   private String okButtonLabel;

   // accessors
}

带一个PropertyPlaceHolderConfigurer

于 2012-12-13T13:43:32.213 回答
0

我想将 CDI 视为以下内容:-

预选赛

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({
    ElementType.METHOD,
    ElementType.FIELD,
    ElementType.PARAMETER,
    ElementType.TYPE
    })
@Documented
public @interface MessageTemplate {
    @Nonbinding
    String baseName();

    @Nonbinding
    Locale locale() default Locale.ENGLISH;

    @Nonbinding
    String key();
}

制片人

public class CustomizedProducer {
    @Produces
    @MessageTemplate(baseName = "",
                     key      = "")
    public String createMessageTemplate(final InjectionPoint ip) {
        MessageTemplate configure = null;
        ResourceBundle  bundle    = null;
        try{
            configure = ip.getAnnotated().getAnnotation(MessageTemplate.class);
            bundle    = ResourceBundle.getBundle(configure.baseName(),
                                                 configure.locale());
            return bundle.getString(configure.key());
        } finally{
            configure = null;
            bundle    = null;
        }
    }
}

服务配置

public class MyServiceConfigure {
    @Inject
    @MessageTemplate(baseName = "com.my.domain.MyProp",
                     key      = "appDescription")
    private String appDescription;

    @Inject
    @MessageTemplate(baseName = "com.my.domain.MyProp",
                     key      = "okButtonLabel")
    private String okButtonLabel;

    //Getter
}

工人阶级

public class MyService {
    @Inject
    private MyServiceConfigure configure;

    public void doSomething() {
        System.out.println(configure.getAppDescription());
        System.out.println(configure.getOkButtonLabel());
    }
}

关于上面的编码,您可以使用java.util.Properties代替java.util.ResourceBundle并为限定符提供默认成员。

如果您在 JavaEE 6 下运行这些,CDI 已经为您启用。只需将空 beans.xml 放入 META-INF 或 WEB-INF。如果您在 Java SE 下运行,您可能需要一些进一步的工作,如Weld 网站及其文档中所述。

我正在使用 CDI 作为我当前生产项目的主要部分,它运行良好。

编辑:-

使用 CDI 的好处是作用域,我们可以生成@MessageTemplateas @ApplicationScope@SessionScoped@RequestScoped@ConversationScoped伪作用域为@Singletonor@Depenendent

如果您将 MyServiceConfigure 注释为@Named,那么它也可以在 JSF 中使用。

于 2012-12-14T09:58:30.027 回答