Java属性文件绑定到Java接口
使用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。 即。 如果有任何库实现了专门为配置处理而设计的这种逻辑。 我不知道这样的事情。
参考GWT中的function: https : //developers.google.com/web-toolkit/doc/latest/DevGuideI18nConstants
我实现了自己的问题解决方案:
基本用法
OWNER API使用的方法是定义与属性文件关联的Java接口。
假设您的属性文件定义为
ServerConfig.properties
:port=80 hostname=foobar.com maxThreads=100
要访问此属性,您需要在
ServerConfig.java
定义一个方便的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上
我仍然有一些function,但目前的实现比问题中描述的基本function稍微前进了一些。
我需要添加样本和文档。
我非常喜欢这个想法,因此我使用Java动态代理快速组装了一些代码。
因此,基本上您使用相关方法创建一个接口,并使用@Key,@ DefaultStringValue注释对其进行注释。
下面是示例Java代码:
Main.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()); } }
我们加载的后台属性文件也是
config.property
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/
你可以用spring来模仿它(但我不确定它是否值得):
@Component public class SomeBean { @Value("${appDescription:Wellcome to my super app}") private String appDescription; @Value("${okButtonLabel:Ok}") private String okButtonLabel; // accessors }
使用PropertyPlaceHolderConfigurer
。
我想将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的好处是Scope,我们可以生成@MessageTemplate
作为@SessionScoped
@ConversationScoped
, @SessionScoped
, @SessionScoped
@ConversationScoped
, @SessionScoped
@ConversationScoped
或伪作用域作为@Depenendent
或@Depenendent
如果您将MyServiceConfigure注释为@Named
,它也可以在JSF中使用。