IntelliJ - Some application properties are grayed out and unused - spring-boot

I have Spring Boot application which uses application properties files like so:
in src/resources:
application.properties
application-dev.properties
application-prod.properties
in test/resources:
application-test.properties
However, the problem I have is that most of the properties set in application-test.properties file and greyed out and marked as Unused properties

Turns out when you add resources folder to your test, you have to right-click on it > Mark Directory as > Test Resources Root.
After this, my properties are neither greyed out nor marked as unused.

Related

In which class in the source code of spring-boot or spring is the application.yml or application.properties file processed?

In which class in the source code of spring-boot or spring is the application.yml file or application.properties processed?
For spring boot (version 2.x) the application properties are loaded from the environment into the context via a PropertySourceLoader.
In for example the spring-boot-2.6.3.jar we can find the following file:
META-INF/spring.factories
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
Where PropertiesPropertySourceLoader loads .properties and .xml files, and YamlPropertySourceLoader loads .yml and .yaml.
These are loaded with the SpringFactoriesLoader, which we can see in action in org.springframework.boot.context.config.ConfigFileApplicationListener (deprecated) or org.springframework.boot.context.config.StandardConfigDataLocationResolver (via ConfigDataEnvironmentPostProcessor -> ConfigDataEnvironment -> ConfigDataLocationResolvers) :
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class,
getClass().getClassLoader());
You can read in the ConfigFileApplicationListener JavaDoc that the properties are indeed loaded with this class:
EnvironmentPostProcessor that configures the context environment by loading properties from well known file locations. By default properties will be loaded from 'application.properties' and/or 'application.yml' files in the following locations:
file:./config/
file:./config/*/
file:./
classpath:config/
classpath:
...
If you're interested in context loading from the environment in spring(boot), I suggest you setup your project with maven, download the sources jars, and have a look around in the mentioned factories file. You will find more relevant code in the org.springframework.boot.env and org.springframework.boot.context (config and properties) packages.
You can find your application.yml or application.properties at the src/main/resources. You can have as many as possible configurations for your spring boot application for every case. Lets assume that you have 3 local-profiles like demo, production and server, so you made 3 configuration and assumingyou set for active profile the demo at the application.yml . I hope you get the idea. Its the first thing that actually is running before the springboot is up.
Please look the officials docs !

How to disable auto configuration when test profile in #SpringBootApplication?

Are there elegant ways to prevent AutoConfigure configuration classes when certain profile (e.g. test) in use? I've all configs in my tests, and don't wanna mark every config in main folder with #Profile("!test")
there also is: #SpringBootApplication(exclude=****Configuration.class) possibility but it's for class, not for profile.
What I did (but not for test) its to disable the AutoConfigure via properties file when a certain profile is enabled.
For instance, I don't want Redis to be auto-configured when profile "withoutRedis" is enabled (dev purpose).
I created a property file "application-withoutRedis.properties" and its content :
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

how to load custom properties in application.properties

In spring boot , there is an property file application.property, along with this property , I have created an extra property file named myownprop.properties.
How can I load myownprop.properties in application.property? means how to include another named properties in application.properties?
any update ?
You could use the spring.config.additional-location property as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files. It allows you to add additional files without messing with the nice default hierarchy of property sources that Spring Boot defines.
You could add a new active profile in your current application.properties
spring:
profiles:
active: dev, additional
and then add a new application-additional.properties file in your resource folder. The entries within the new file are then available as they are in the application.properties.

parent properties for application-{profile}.properties with Spring-Boot app

There are common properties which are shared among different profiles for e.g. path location for temp files and path remains same among different env(tst,prd).
Is there a way to have a parent application-{parent}.properties from which all the profile specific properties files can inherit the properties.
That will help in avoiding writing same properties in all application-{profile}.properties
In addition, each application-{profile}.properties have something like :
profileLocation=xxx
abc=${profileLocation}/tempPath
Here can I move abc to a common location? I cannot in application.properties as it gets loaded before application-{profile}.properties
Actually, that is not entirely true that application.properties are loaded before any others. They are processed together. To set common properties that are used by all profiles, you should use the ordinary application.properties file. Two main thing you should know are described below.
Case 1. The keys that are placed inside the application.properties file can be overridden by profile specific configuration.
common.path.for.all.envs=/some/path
default.path=/another/path
Than in your e.g. application-dev.properties you can override some values.
default.path=/dev/path
At runtime with dev profile your application will have access to two keys. The value of common.path.for.all.envs will be set to /some/path as declared only in the main file and default.path will be set to /dev/path because you override the property in the profile configuration.
Case 2. The values defines in the application.properties file can use placeholders for the values included in profile configurations. For instance, in your application.properties define the following variable:
abc=${profileLocation}/tempPath
Next, in the application-dev.properties declare the missing variable:
profileLocation=xxx
Then running with the dev profile the value of abc will be set to xxx/tempPath. As you see, the variable declared in the profile configuration can be used in the main application.properties file as well.

Automatic restart of jar when property file is changed

I have a requirement where I am deploying a jar with its application.properties file outside of it. Is there any way that when I change an property in application.properties the jar automatically detects the changes and restarts/redeploys itself?
#RefreshScope annotation is provided in spring-boot.
By use of this annotation, you can reload a property value(use inside your code) from .properties.
here is the link for reference :http://projects.spring.io/spring-cloud/spring-cloud.html (Search #RefreshScope).
The idea is to reload whole bean(which is annotated with #RefreshScope),after hitting /refresh end point & you do not need to restart webapp again.
Read about http://cloud.spring.io/spring-cloud-config/
One of options is to restart/reload application after change in properties files.

Resources