How do I access Spring properties in a logback configuration - spring

Is there a way to access Spring properties within a logback.xml file?
I know one can import a properties file if you know its location, but I'm using Spring profiles to control where the properties file should be loaded or not.
Is there done kind of connector that asked me to feed Spring data into logback? This would only be at startup; I don't need to be able to do this on the fly.

I'm guessing you do have to import a property file (common property file, non-environment specific one) that will contain the name of the property that you are going to use in the logback.xml, and that you want to optionally override the value of the property for some environment (you need at least one property file containing the name of the property, because you will be using that property in the logback.xml, and you need it to be available to be able to use it).
For the optional environment-override, how about including an additional property file? For example, we use both application.properties and application-${spring.profiles.active}.properties files. Then if we don't need to override the property for some environment, we simply don't include it in the environment specific property file (application-dev.properties, etc.)

Related

Using variables from different properties file in spring application properties

I have multiple application-[profile]properties files for different profiles but all of them use the same DB.
I don't want to have the username and password hardcoded in each file, instead I want to have them as variables in some sort of global variables file.
Is is possible to do something like that without using system environment variables?
As Karthikeyan suggested, you can put your username and password only in the application.properties file. The file application.properties will always be loaded even if you use an environment-specific property file like application-dev.properties. However, if there is a property collision, the environment-specific property file takes precedence.

Profile-specific spring.config.additional-location?

I have specified an external properties file to a Spring Boot app by setting spring.config.additional-location in the SpringApplicationBuilder.
new SpringApplicationBuilder(MyApplication)
.properties(['spring.config.additional-location': myExternalProperties])
.run(myArgs)
This works insofar as it allows me to override properties in the application.properties file using myExternalProperties.
However, myExternalProperties are in turn overridden by any profile-specific properties, e.g. application-myProfile.properties.
I understand this to be consistent with Spring's prioritization of Externalized Configuration, but I want myExternalProperties to override even profile-specific properties.
How can I achieve that order of priority?
I do not control the file name or location of myExternalProperties. This variable is a System property that is preset in the environment.
I have been looking at Profile-specific Properties and in particular this quote.
If you have specified any files in spring.config.location, profile-specific variants of those files are not considered. Use directories in spring.config.location if you want to also use profile-specific properties.
I assume this note applies equally to spring.config.additional-location, but without control over the property file name or location I don't think that helps me.
I'm afraid it cannot be achieved in a non-hacky way.
The docs state:
Profile-specific files always overriding the non-specific ones.
Also:
If several profiles are specified, a last-wins strategy applies. For example, profiles specified by the spring.profiles.active property are added after those configured through the SpringApplication API and therefore take precedence.
Could you consider using profile based configuration for myExternalProperties?
You can even use env vars as placeholders.
Hope this helps: Env vars in Spring boot properties

How can i refer one property file from another property file in spring cloud config

I have to implement spring cloud config for an existing project where i have an use case to handle
Here Some property files are referred by other property files like
logging.propFile=classpath:/cfg/xyz.properties and these property files are used in multiple places like
Properties property = new Properties();
property.load(new FileInputStream(propsPath));
logger = somefactory.createfactory(property.get("logging.propFile")); and this factory creation is defined in a different jar.
If i move all property files in git repo files i mentioned will not be available in class path and if i go for code change a lot of code change is involved.What is the best way to handle this situation.
Is there any way to refer one property file to another in spring cloud config .
Spring Cloud Config server allows for reading static files through HTTP. So you can put your references properties file into the configuration git repository and reference them through http://<configserver_url>/*/*/<branch_name>/xyz.properties. This only works when your code can handle URLs, so the FileInputStream in your example would not do it.
See https://cloud.spring.io/spring-cloud-config/multi/multi__serving_alternative_formats.html and https://cloud.spring.io/spring-cloud-config/multi/multi__serving_plain_text.html.
Also note the resolvePlaceholders query parameter that defaults to true.
We are using this for keeping our logback.xml configuration in the config server by setting logging.config: http://<configserver_url/*/*/master/logback.xml?resolvePlaceholders=false

SpringBoot custom spring.config.location

I have a simple SpringBoot application with the following structure:
I'm using a standard application.yml file where I'm storing all the necessary props and use #ConfigurationProperties annotation to inject them where necessary.
Now for one bean I have quite a lot of props and I don't want to overwhelm my common application.yml file with all that props. So I want a separate one (which I placed under service dir in classpath).
According to Spring docs I can use something like:
java -jar myproject.jar --spring.config.location=classpath:/service/application.yml
But that's not working, I got NullPointer which means property was not injected.
What Am I doing wrong? How can I use another *.yml file together with application.yml?
P.S. I know I could place it under the config folder in classpath, but what if I need two custom files?
If you have 2 configs in different places, spring.config.location will accept a comma separated list of those locations
--spring.config.location=classpath:/resources/,classpath:/service/
You could also just call the other file like "config.yml" and then use a different name
--spring.config.name=application,config

Is it possible to reference a specific property from spring boot application.properties in an external property file

In spring boot I have an application.properties file in which I have a property
apple = X.
I have another custom property file Custom.properties. I want to reference apple in this custom property file so I have this:
orange = ${spring.apple}.
But this does not work at runtime.
Is there any way to get access to an application.properties property from another custom properties file?
Edit:
I also tried orange=${apple}, that does not work either. I set spring.config.location to point to the directory that has application.properties and my custom.properties file also. I assumed spring boot would load any properties files it sees in spring.config.location but looks like it does not.
Is there any way to load my custom.properties file in addition to application.properties?

Resources