【发布时间】:2013-02-20 01:43:38
【问题描述】:
我正在尝试找到一种方法,可以在配置文件中指定一个类,以及应该传递给构造函数的参数。
例如,想象以下 XML 配置文件:
<AuthServer>
<Authenticator type="com.mydomain.server.auth.DefaultAuthenticator">
<Param type="com.mydomain.database.Database" />
</Authenticator>
</AuthServer>
现在,在我的 Java 代码中,我想执行以下操作:
public class AuthServer {
protected IAuthenticator authenticator;
public AuthServer(IAuthenticator authenticator) {
this.authenticator = authenticator;
}
public int authenticate(String username, String password) {
return authenticator.authenticator(username, password);
}
public static void main(String[] args) throws Exception {
//Read XML configuration here.
AuthServer authServer = new AuthServer(
new DefaultAuthenticator(new Database()) //Want to replace this line with what comes from the configuration file.
);
}
}
我当然可以读取 XML,并从中获取值,但是我不确定如何使用 XML 配置文件中的值的注释来模拟上面指示的行(想要替换...) .有没有办法做这样的事情?
【问题讨论】:
-
你可以考虑spring framework这个..
标签: java xml configuration