I'm trying to find a way that I can specify a class in a configuration file, as well as the parameters that should be passed to the constructor.
For example, imagine the following XML configuration file:
<AuthServer>
<Authenticator type="com.mydomain.server.auth.DefaultAuthenticator">
<Param type="com.mydomain.database.Database" />
</Authenticator>
</AuthServer>
Now, in my Java code, I want to do the following:
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.
);
}
}
I can read the XML of course, and get the values from it, but then I'm unsure how to emulate the line indicated above (want to replace...) with the comment with the values from the XML configuration file. Is there a way to do something like this?