I would like to make all property placeholder and their resolved values of a running Spring (Boot) application available for process monitoring. In the first step this could be just by writing them to the logs or by creating a 'resolved.properties' file similar to the application.pid file.
All properties where property placeholder are used (implicit/explicit) should be considered.
Motivation: It is usually hard during operation to know the values of resolved properties. System properties or command line arguments are "visible" but e.g. hidden default values in the code (like #Value("${timeout:30000}")) are hard to find out. I would like to be able to answer the question "How does the configuration of the running application looks like?" in a generic way that I can use in all of my spring applications.
I know about the Spring Boot Actuator /configprops endpoint, but this only includes #ConfigurationProperties. I would like to get a list of all properties where placeholder are used.
The requirement does not seem to be new (see here or here) but I wonder if there is an appropriate (bootiful) way nowadays.
There is (currently) no way to obtain all the properties in the Environment abstraction. This is intentional as can be read here. This is also why it isn't possible to obtain all the values used for resolution.
The values and resolutions are logged at runtime telling which key was resolved from where at runtime. But that logging is quite verbose and logged each time a StringValueResolver is used.
You might get a partial result by providing your own customized PropertySourcesPlaceholderConfigurer which maintains a collection of resolved key/value pairs. But not every resolution uses the PropertySourcesPlaceholderConfigurer some directly use a StringValueResolver implementation bypassing the PropertySourcesPlaceholderConfigurer.
It doesn't cover all your needs (ie: properties from all files, default values, application arguments, etc.).
I'll still keep the answer for other readers/future reference.
Spring Boot's Actuator /env endpoint
You may use the /env endpoint. It lists a bunch of stuff but it also includes the content of application.properties (near the end):
applicationConfig: [classpath:/application.properties]={myproperty=blah, server.port=8080}
Related
I need to load a configuration property fully into java.util.Properties file in my spring boot project and then need to pass this wherever needed. With Spring boot I can load the full file and can get the access of the values though keys. But how I can load the whole configuration file into Properties object or pass the spring loaded property (not a single value rather all the values) wherever required?
My current code:
Properties myProps= new Properties();
myProps.load(resourceAsStream);
If you're looking for specific ways of loading them using Spring-boot I'd suggest looking into:
Binding properties to an object by using the #Configuration, #ConfigurationProperties and #PropertySource annotations if you want to enforce and implicitly manage type-safety at all times
The Environment interface you can #AutoWire to your classes, if you don't need to enforce type-safety at all times (you can still do it, but you're not forced to). #PropertySource can be used in this case as well to load properties outside the default default-loaded application.properties, although they'll be loaded only when the application context refreshes (e.g. they won't be available while the application is booting up)
The PropertiesLoaderUtils class, as suggested in the comments, if you want to selectively load a configuration file at runtime for example
I usually recommend the first. The result is the same as using an #AutoWired Environment, with the advantages of implicit type-safety and improved readability. You can then get the properties and write them inside your java.util.properties if you need them to be there.
However, there is more than one way to do that, both using Spring-Boot or not. Loading properties like that is also perfectly fine, although arguably not the best practice since you're using Spring-boot.
In short - is there a way to set the value of spring properties to what's been set as a JVM arg? E.g. I have a netflix turbine cluster which needs the following property set:
turbine.aggregator.clusterConfig=myCluster
Is it possible to a way of setting a JVM param as
-DturbineCluster=myCluster
and then in the property file setting:
turbine.aggregator.clusterConfig=${turbineCluster}
I did actually try this and it didn't work. Can this be done from a property file or does this kind of thing need to be done programatically?
(Apologies if this has been asked before - had a quick search and couldn't find anything.)
You can do this easily in spring boot, it may be supported by spring as well
-Dspring.application.json='{"turbine.aggregator.clusterConfig":"myCluster"}'
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
I'm writing a central console for our IT group to display properties retrieved from Spring Boot Actuator endpoint /configprops for each microservice running in our SOA ecosystem. I have 2 questions
Seems #Value annotated properties to not get returned despite the documentation saying
63.7 Discover built-in options for external properties ....
The definitive list comes from searching the source code for #ConfigurationProperties and #Value annotations, as well as the occasional use of RelaxedEnvironment
Looking at ConfigurationPropertiesReportEndpoint.java, looks like it searches only for #ConfigurationProperties annotated classes:
beans.putAll(context.getBeansWithAnnotation(ConfigurationProperties.class));
Is there an easy way to determine where the final property value resolved from? For example: Did the property get overridden via environment variable? Or, Did it come from the git repo?
Section 63.7 is trying to say that the only definitive way to discover all properties (since they can be bound in numerous ways) is to search the source code. We try to use #ConfigurationProperties whenever possible so that they can be discovered by IDEs and exposed by the actuator. Unfortunately we don't have a way to detect and expose every single property in the report.
There isn't a definitive way to tell where a property came from at the moment. You might be able to look at getPropertySources() from ConfigurableEnvironment. That will let you iterate PropertySource items, and in turn you can check if they are an EnumerablePropertySource and get the values. Each PropertySource has a name which might provide come clues as t what added it.
Our application exports its configuration as several sets of Spring beans.
I have no control over how it does this.
I want to write something that documents the dependencies between the configuration items defined in these beans. Note: these are dependencies at the application level, nothing to so with Spring dependencies (so we might have configuration items of type Actresses who have a dependency on certain items from the type Bishops, but - at the Spring bean level - this is merely that the value of a property in the Actress matches the value of a different property of the Bishop).
So I'd like to use some library or toolset that lets me load up a set of bean definition XML, iterate over them and the content of each, extract property values and so on.
From some googling, I can find ways of extending the parsing Spring does itself, but I don't want that - I want something I can run offline outside of the Spring-using app itself.
Can someone point me at some resources for doing this?
The closest thing I've heard of to this is Spring BeanDocs:
http://spring-beandoc.sourceforge.net/
I need to load an environment-specific property file, and I'd like to be able to both set it from the JVM (using -D) and to provide a default value in the main properties file or, failing that, somewhere else (like the applicationContext.xml)
I'm using the new hotness Spring 3.1 with its unified property management, but I can't find a lot of info on the property system.
UPDATE:
To clarify:
<context:property-placeholder location="/WEB-INF/myapp.properties,
/WEB-INF/myapp-${deploy.env}.properties"/>
You can do it using Spring 3.1, JVM property will be put into placeholders and you can define default values using ":", for example:
${property1:defValue}
where defValue is default value, it can be overridden by JVM option -Dproperty1=newValue
You should read this API - it is pretty informative. Example here.
EDIT
As the example points to outdated version of Spring the more modern approach is illustrated here