Java的.properties文件为强类型类 [英] Java .properties files as strongly typed classes

查看:184
本文介绍了Java的.properties文件为强类型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让性文件作为强类型的类?
我想有code发电机但注释做起来会更凉爽。

Is there way to get properties files as strongly typed classes? I guess there are code generators but doing it with annotations would be much cooler.

我的意思是;

foo.properties file
keyFoo = valuefoo
keyBar = valuebar

可能与

@properties(file="foo.properties")
class foo { }

变为

class foo {
  String getKeyFoo() { }
  String getKeyBar() { }
}

如果不肯,我就开始一个开源项目是什么?

if not shall I start an open source project for that?

除了问题;

想我们有设A foo.properties文件说,10余项;
并认为它是作为一个简单的配置文件。我认为是这个配置项应与相关的getXXX方法来设计的其他部分配置类提供。然后,系统的其余部分通过访问提供的类,而不是处理键名的配置,不需要费心的地方配置来。然后,你可以当你正在测试呼叫者和依赖于文件系统消失了模拟代替这个类。在另一方面,这是非常好的
得到一个强类型的方式的所有条目。

Think we have a foo.properties file with let say more than 10 entries; and think it is used as a simple configuration file. What I believe is that this configuration entries should be provided as a configuration class with related getXXX methods to other parts of the design. Then rest of the system accesses the configuration via provided class instead of dealing with key names and don't need to bother where configuration comes. Then you can replace this class with a mock when you are testing callers and dependency to file system goes away. On the other hand it is really nice to get all entries in a strongly typed fashion.

所以这个问题是幕后code一代的问题,它是什么到运行时有关。但是,code代与外部的东西,而不是说明没有似乎对我很好。虽然我不是用注解非常熟悉,我想这是可以实现的(但我会记住,注释不能产生类作为麦克道尔指出)

So this issue is a code generation issue behind the scenes, it is nothing related to runtime. But code generation with an external something instead of annotations didn't seemed nice to me. Although I am not very much familiar with annotations, I guess this could be achieved (but I'll keep in mind that annotations can not generate classes as McDowell points)

推荐答案

有一个的有点类似项目做配置,静态类型的文件。它需要声明一个接口,但它的实现本身填充:

There is a somewhat similar project for doing configuration as statically typed files. It requires to declare an interface, but it fills in the implementation itself:

public interface AppConfig extends Config {
    long getTimeout ();
    URL getURL ();
    Class getHandlerClass ();
}

这篇关于Java的.properties文件为强类型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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