What are the different approaches to configure application.properties file in spring boot - spring-boot

One way is to directly edit appliaction.properties file in any editor and write the contents.
Is there any other external approach to do the same because in one of my project, I am unable to find anything in application.properties file when I open it in editor but when I run the application I get some information out of application.properties file.

The properties may be configured in many different standard ways with SpringBoot. I think the best way to identify where your parameters are is to list the locations given in the SpringBoot configuration documentation and check if your parameters are here.
EDIT List the locations :
Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
#TestPropertySource annotations on your tests.
properties attribute on your tests. Available on #SpringBootTest and the test annotations for testing a particular slice of your application.
Command line arguments.
Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
ServletConfig init parameters.
ServletContext init parameters.
JNDI attributes from java:comp/env.
Java System properties (System.getProperties()).
OS environment variables.
A RandomValuePropertySource that has properties only in random.*.
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
Application properties outside of your packaged jar (application.properties and YAML variants).
Application properties packaged inside your jar (application.properties and YAML variants).
#PropertySource annotations on your #Configuration classes.
Default properties (specified by setting SpringApplication.setDefaultProperties).

Related

In spring, what's the difference between putting a property in jvm properties and bootstrap.yml?

I know bootstrap properties are used in the bootstrapping phase before the application yml is loaded as per What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?
However, I don't understand how adding a property using jvm options or system properties relates to these phases.

Spring Log4j2 xml file location

using Spring 2.0.5 with Log4j2
Have found works as expected if I place the file log4j2.xml in the resources folder.
However, have found the log4j2 option of monitorInterval="60" to be super useful as if some production issue can increase logging on some class without a restart. However if log4j2.xml is embedded in the jar of course it cannot be modified. so far the best I have been able to do is use 2 copies of log4j2.xml one in the resources and the other in the folder running the jar from.
I can then run:
java -Dlog4j.configurationFile=log4j2.xml -jar myapp.jar
it seems to work the same if started from the maven spring-boot plugin or from Eclipse.
Have tried a number of things such as setting the classpath, the absolute file name etc. but no luck
My question is how can I remove the log4j2.xml file from the resources folder and only specify it on the command line when starting spring?
I am not entirely clear on your question but I can provide the following information which I hope addresses it:
Log4j 2.12.0 added support for Spring Cloud Config and enhanced the support for Spring Boot. As of that version you can include a file named log4j2.system.properties and place any system properties you want defined there. They will be set before Log4j initializes. So you can specify the full URL to the configuration there if you want instead of the command line. You can also add the definition to a file named log4j2.component.properties.
The support for Spring Cloud Config allows you to place the configuration in your Spring Cloud Config server. See Spring CLoud Config support for more details.
Spring Boot initializes logging at least 3 times. The first is usually because the SpringApplication class declares a Logger so logging is initialized before anything else happens. The configuration for that will use "normal" log4j 2 initialization. After that Spring influences how logging initialization occurs primarily because Spring Boot sets the class path to include the jars inside BOOT-INF/lib directory inside your Spring Boot jar.
By using one of the configuration options I outlined above you can move the logging configuration outside of your application and you should not require a logging configuration in the resources directory. In fact, if you look at the sample Spring Cloud Config Application in Log4j you will see it does not include a configuration file in it.

Log all loaded configuration in spring boot application

I have 2 spring configuration files in my application(application.properties and custom-application.properties.) Each property file contains some of the configuration properties. I just wanted to log all the properties which are loaded.
How to print properties loaded for the profile?

Loading spring application-*.properties

I was wondering if there is a way to dynamically reference keys in multiple application-*.properties files in a spring application. The challenge I have is the property file names can be different for each app. I have tried various combinations of spring.config.location, spring.config.name, used ClassPathResource but no luck
For e.g. in src/main/resources I might have for application A application.properties, application-system-X.properties, application-system-Y.properties
For e.g. in src/main/resources I might have for application B, application.properties, application-system-P.properties, application-system-Q.properties
I am not sure whether I understand you problem correctly. But in Spring Boot you can dynamically use different application-*.properties by using profiles.
These profile-specific application properties can live inside and outside of your packaged jar (application-{profile}.properties and YAML variants).
I can also recommend to read the documentation on externalising your configuration with Spring Boot.

Spring Boot external configuration without ignoring the packaged configuration

I am developing a Spring Boot JAR application and what I want is for some configuration properties to be found in an external path. I want something like this:
the jar to be located at /home/myapps/my-spring-boot-app.jar
the configuration to be located at /apps/configuration/app/config.properties
I have set the spring-boot-maven-plugin with layout:ZIP and I have tried with different configurations like spring.config.location="/apps/configuration/app/" and loader.path="/apps/configuration/app/" and it didn't work.
In some cases, it ignored my external configuration, and in some cases, it ignored my packaged configuration. I don't want to use the Spring Boot defined hierarchy, to have the configuration in ./config/
Thanks for the help
Acording to documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html spring.config.location can accept more than one file, so you can try something like this:
spring.config.location=classpath:/application.properties,/apps/configuration/app/config.properties
or just directories:
spring.config.location=classpath:/,/apps/configuration/app/

Resources