Guoqin Zheng
2016-11-29 20:13:46 UTC
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!
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!
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/0e1032a8-9dd5-4cee-ad95-ef7f422d6d1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/0e1032a8-9dd5-4cee-ad95-ef7f422d6d1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.