In a standard Spring application, a PropertyPlaceholderConfigurer may be defined, which will load one or more property files. The values defined in the files will then be visible to the rest of the application, both in XML ("${}") and Java (@Value).
Is there a way, once a context has been loaded, to get such a property value from the context itsef, in the similar way that a bean can be retrieved (ctx.getBean("bean-name")) ?
I tried the following, but it does not work:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/spring-context.xml");
ctx.refresh();
ctx.start();
ctx.getEnvironment().getProperty("key-name")); // RETURNS NULL
Thanks
You need to get access to the BeanFactory:
ctx.getBeanFactory().resolveEmbeddedValue("${key-name}");
See this answer for a simple approach that could work by adding an interface called EmbeddedValueResolverAware to the class in which you want to resolve the property values.
https://stackoverflow.com/a/16106729/1325237
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With