I would like to create the right property management strategy in java webapp that relays on google guice as a DI infrastructure.
I would like to have a mechanism that meets the following three requirements:
- I would like to be able to enter properties using guice (@Named)
- I would like to have access to properties in a static way.
- The mechanism must support property prioritization, which means that the property can be wrapped in a deployed war with a certain value, but it can also be redundant at the target system level or local file system (on the target machine that I will deploy to), in which case the value in war will be overridden by the value that exists on the target machine.
I believe this is a standard requirement. Now, using a standard binder potion, I can easily get the first requirement, but not the other two. To get the other two, I created my own class that does the following:
- Wraps around and provides guice binding methods (those that bind properties) For example:
public static void bindString(AnnotatedBindingBuilder<String> binder, String property, String defaultValue) { binder.annotatedWith(Names.named(property)).toInstance(getProperty(property, defaultValue)); }
Where the getProperty method knows how to handle my properties (get the value from a war or system level), and also provides properties statically.
So basically, while I use this utility that I created for property bindings, I'm good, it covers all my requirements, but as soon as I use standard guice bindings, I lose the second and third requirements.
Is there a way to override the guice bindings and get all these 3 requirements?
Once I had the same call in a spring application and it was pretty easy. I applied ApplicationContextInitializer as follows:
@Override public void initialize(ConfigurableWebApplicationContext ctx) { PropertySource<Map<String, Object>> localProps = null; try { localProps = new ResourcePropertySource(new ClassPathResource(LOCAL_PROPERTIES_FILE_NAME)); } catch (IOException e) { LOG.fatal("Could not load local properties from classpath " + LOCAL_PROPERTIES_FILE_NAME); return; } LOG.info("Loaded configuration from classpath local file " + LOCAL_PROPERTIES_FILE_NAME); ctx.getEnvironment().getPropertySources().addFirst(localProps); }
so this gave me a way to add local properties with the highest priority for my environment. In case of coincidence with the properties of war, local ones have a higher priority. In addition, I set my environment statically, so I have static access to my properties (for services that are not controlled by the container, mostly mainly).
How can I achieve this with guice?
java properties dependency-injection guice
forhas
source share