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
Related
I have a yaml file that contains some credentials for azure keyvault. This project is I am working on is a shared git repo so I would like to set these values as environment variables for the whole project not just for myself
application.yml:
azure:
keyvault:
uri:someUri
client-id:someClientId
client-key:someClientKey
but I want to have them set up like this:
azure:
keyvault:
uri: ${uri}
client-id:${clientId}
client-key:${clientKey}
Is there a way to set those values and have this work for others without them having to manually set these values in their environment?
You can ignore the .yaml in gitignore so you can use your config in your environment. And if you pull from others their config won't overwrite your config.
If you want if like dynamically
java -jar myapp.jar --spring.application.json='{"foo":"bar"}'
You can add variables like this in start command. Configure in your Run configuration.
The syntax you've presented above is supposed to work.
I usually use capital letters for 'convention' but other than that its fine.
However providing such a syntax effectively means that you don't want to define property values in your application so you'll have to get them from elsewhere.
From your question I understand is that your primary concern is your teammates and in your setup each one of your peers has clientId, key, etc.
In this case you can create a script that will "export" all the variables to be environment variables automatically, they'll run it only one time and it will work since than.
Another option which is kind of similar is providing a property SPRING_APPLICATION_JSON that will refer to the json with a configuration, spring boot application can read it as written in the official documentation
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}
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.
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
Is there a way to pass data or setting to log4j before it loads and then use that property within the config file.
I was assuming there is a system properties I could use:
log4j.appender.R.File=/usr/local/pfs/logs/${ws.host}/log4j.log
Where ws.host is the property I want to use.
But how can I set that value?
Also, I am in a web environment. How can I know at what point to set the property setting before log4j loads.
The default log4j PropertiesConfigurator supports variable substitution.
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html
So, you could pass system properties like this "-DmyProject.logFile="/temp/test.log" to your Java startup, and then in the properties files have "log4j.appender.R.File=${myProject.logFile}".
If working from a web environment, you might want to check out Spring's Log4jConfigListener. It uses a listener (Servlet API 2.4+) to initialization log4j ahead of other components. Even if not using Spring, you should be able to use the source as an example to easily create your own listener.