Config Annotation binding?

51 views
Skip to first unread message

Guoqin Zheng

unread,
Nov 29, 2016, 3:13:46 PM11/29/16
to google-guice
Hey all,

I am trying to inject configuration pojo with guice by annotation. But I find difficulty to do that. Basically what I want to achieve is like following:

Say I have the following classes 

@Retention(RUNTIME)
@Target({ FIELD, PARAMETER })
@BindingAnnotation
public @interface Config {
/**
* The config key to bind
*/
String key();
}

/**
* A tag interface to indicate that a class is a Configuration.
 */
public interface Configuration {
}

/**
* An example: App Configuration
*/
public class AppConfiguration implements Configuration {
private String appId;
private String appOwner;
private String appDesc;
}


/**
* Another config example: MySql Connection setting
*/
public static class MysqlConnection implements Configuration {
private String host;
private int port;
private int timeout;
}

You could define any other configuration POJOs.

Also, assuming I have a config provider that could load the config file and map to the pojo:

public interface ConfigurationProvider {

/**
* Get the configuration pojo at the node path
*/
<T extends Configuration> T getConfiguration(
Class<T> configKlass,
String nodePath) throws ConfigurationProviderException;
}

Now, I would like Guice to inject these configuration somehow like:

public class MyService {
@Inject
public MyService(
@Config("app_config")Configuration.AppConfiguration appConfig,
@Config("mysql_conn")Configuration.MysqlConnection mysqlConnection) {

}
}

Just curious if this is possible with Guice injection? If yes, how can I achieve that?

Thank you!
Reply all
Reply to author
Forward
0 new messages