如何让不添加任何code类配置? [英] How to make a class configurable without adding any code?

查看:118
本文介绍了如何让不添加任何code类配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够写(在Java中)类似如下:

I would like to be able to write (in Java) something like the following:

public class A implements MagicallyConfigurable {

    @configuration_source_type{"xml"}
    @configuration_dir{"some/rel/path/"}

    /* or maybe some other annotations specifying a db-based configuration etc. */

    int length = 4 /* some kind of default */;
    char c = '*' /* some kind of default */;

    @do_not_configure
    String title;

    A(String title) {

        /* possibly, but hopefully no need to, something like:
        configure();
        call here. */

        this.title = title;
    }

    void goForIt() {
        System.out.println(title);
        for(int i=0; i < length; i++) {
            System.out.print(c);
        }
    }
}

和按预期工作。也就是说,我需要根据配置将加入​​一些注解,实现接口,并可能使一个函数调用(但不希望它)来初始化领域的唯一的事情。

and for it to work as expected. That is, the only thing I would need to initialize fields based on a configuration would be adding some annotations, implementing an interface and possibly making a single function call (but hopefully without it).

我敢肯定,这在理论上是可行的,问题在于是否有一个现有的库/框架/插件/的thingie这使得它。 (也许阿帕奇commons.configuration不知何故?有没有与它之前的工作。)

I'm sure this is theoretically doable, the question is whether there's an existing library/framework/add-on/thingie which enables it. (Maybe Apache commons.configuration somehow? Haven't worked with it before.)

推荐答案

你能做什么,是使用Spring 3.0 EL。一个例子可以在这里找到 ,但它是什么基本上可以归结为是,你可以做到以下几点:

What you can do, is to use Spring 3.0 EL. An example can be found here, but what it basically boils down to is that you can do the following:

@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String dbName) { ... }

和您的属性将被自动设置。这将工作的制定者和属性。

And your properties will be automatically set. This will work on setters and on properties.

这篇关于如何让不添加任何code类配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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