Arborescent structure in application.properties - spring-boot

I am reading/practicing a security management tutorial with Spring-boot. I noticed that I could customize the application by using either a file named application.properties or a file named application.ymldepending on the syntax I prefer.
Then I reached a point in the tutorial where it is asked to put this piece of information in application.yml :
security:
sessions: NEVER
Problem : this piece of configuration is obviously arborescent and fits well in application.yml but what would be the equivalent in application.properties which, AFAIK, like every .properties files is not meant to store arborescent data?

Where YAML uses new lines and indentation, a properties file uses . separators. The equivalent configuration in application.properties would be:
security.sessions=NEVER

Related

Spring cloud config fails to pick configuration from local file system

I am trying to setup spring cloud config using local file system. However, I couldn't get it working.
Below is my application.properties file:
spring.application.name=spring-cloud-config-servers
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/config
Inside limits-service.properties, I have the following:
limits-service.minimum=4
limits-service.maximum=400
Once i run the server, i get the following. Please help in fixing it.
It might be a simple mistake of naming?
For example, your spring application name is "limit-service" while your property files are named "limits-service" and that might be why it is not reading them.
yes above one it is working, spring.applicatiom.name must be equals to the properties file(Lets say uppercase must follow uppercase of properties file as well )

Any way to split Spring Boot configuration into multiple properties files without having to specify an environment variable/system property

New to Spring Boot here, long-time Spring Framework user though.
I'm looking for a way to split my externalised configuration into multiple .properties files, for better readability and manageability.
I already saw this SO answer: having the ability to specify a list of configuration file names in spring.config.name (which, by the way, doesn't seem to be mentioned in Boot reference documentation, correct me if I'm wrong) would solve my problem perfectly, however that configuration property can be specified only via system properties or environment variables. If I try to specify it inside my application.properties file, it gets ignored. The same happens for spring.config.additional-location. I understand this happens because, when application.properties is read, it's too late to tell Spring Boot to search for different externalised configuration file names. However this is not a proper solution, because the way I split my configuration should be an "implementation detail" that the consumer of my application shouldn't be aware of, so I don't expect the consumer to specify an external parameter otherwise my application breaks out-of-the-box.
I think that a way to do this should be provided. Perhaps some import mechanism for .properties files or the ability to specify spring.config.name even in application.properties (some known and reasonable limitations would be acceptable).
The best I could find out is to use #PropertySource, but this is not profile aware: unless you use some ugly nested class hack, or you put spring.profiles.active variable in the resource name (which will break if multiple profiles have been activated), you won't get the benefit you have for application.properties profile-specific files.
I was not able to find an "official way" to do this, apart from some statements from Spring Boot devs that say that they're rather promoting the use of a single (possibly giant...) externalised configuration file. It seems like this position is not so popular, judging from the post reactions on GitHub, and IMHO it really seems to be a basic feature missing. I have been working with multiple properties files in Spring Framework (using XML configuration) for years and I never felt that having an only huge file would have been better.
If I understand it right, in Boot 1.x this was in some way possible using the location attribute of #ConfigurationProperties, which is however missing in Boot 2.x.
Any suggestion?
Have you tried with Spring Profile?
What you can do is create application-file1.properties/yml, application-file2.properties/yml and put it in config location and then add spring.profile.active=<your env profiles>,file1,file2.
It will load the files.
This profile entry can be in bootstrap.yml, or JVM args to application, in Manifest-<env>.yml in case of Pivotal Cloud Foundry. Not sure on AWS and other cloud provider.
Hope this will help.

Quarkus: How to define and read properties file (or application.properties) outside application or at runtime?

In Quarkus, We have properties file inside project itself called application.properties.
Is there any Quarkus way to define external properties file in my use case like i am developing a mail sender and i want to add recipients in future.
Is it possible to give application.properties outside at local and inject it at runtime?
You can add a configuration file in your application working directory under config/application.properties : https://quarkus.io/guides/config#overriding-properties-at-runtime
There is ongoing discussion to have more runtime configuration capabilities here: https://github.com/quarkusio/quarkus/issues/1218
You can achieve this by keeping .properties (or .yaml) in Spring Cloud Config Server.
It's really easy to set it up. It's is well documented in following link (official documentation):
Quarkus - Reading properties from Spring Cloud Config Server
As loïc says, you can follow the convention and create a config/application.properties. You can also use the property quarkus.config.locations to specify additional config locations. It can be defined at runtime like below
java -Dquarkus.config.locations=app-config/config.properties -jar my-app.jar

Can Spring IDE support editing multiple application.yml files?

I'm using Eclipse Neon with Spring IDE Version 3.8.3.201612191259-RELEASE.
Whenever I create an application.yml file under src/main/resources it shows with a Spring embellished icon and allows me to see Spring Boot's configuration (for example see here: https://docs.spring.io/sts/nan/v370/NewAndNoteworthy.html).
For different profiles developers can either use a single YAML file with different sections for each spring.profiles via the --- notation. Developers might also choose to use separate YAML files named with the profile as part of the name (i.e. application-dev.yml) as noted in this link: https://www.mkyong.com/spring-boot/spring-boot-profile-based-properties-and-yaml-example/ .
The functionality provided by the Spring Boot YML Editor is very helpful not only for YAML editing but also because it allows auto-complete for Spring Boot's configuration. Is there a way to associate more than one file to be see by the Spring Boot YML Editor so that I can easily edit files like application-dev.yml in that editor? I know I can right-click on the file and have it open-with Spring Yaml Properties Editor but it would be nice if similar to configuring the Spring Bean support Config Files you could also pick multiple files to be seen as Spring YAML files (or perhaps look for all application-XXX.yml files and associate them to the editor).
The place where to specify file associations for the content type Spring Yaml Properties File can be found in the Eclipse preferences in General > Content Types. Search for the content type Yaml Content Type > Spring Yaml Properties File. In the box below you can add your required file associations.
As of Eclipse Neon you have to specify all your variants, e.g. application-prod.yml, application-some-profile.yml, ...
In Eclipse Photon it will be possible to use wildcards, e.g. application-*.yml. See https://www.eclipse.org/eclipse/news/4.8/M2/ for details

How to access environment variable externally in spring boot?

Is it possible to access environment variable of different application.properties from a single place. Actually we are building this software where we have different application.properties for different projects like user-asset. So is it possible to have all environment variable at one external place. If yes, how will it be accessed?
You would have an application.properties file that defines variables that would never change in what every situation you have.
application.properties
server.error.whitelabel.enabled=true #Just an example
Then you could have a separate application.properties with a different name such as application-active.properties. This file would add onto the base application.properties file.
application-active.properties
example.enviroment.variable=${I_AM_AN_ENVIROMENT_VARIABLE}
Then you could have a different application.properties file that has the same property name, in this case example.enviroment.variable.
application-dev.properties
example.enviroment.variable=${I_AM_A_DIFFERENT_ENVIRONMENT_VARIABLE}
Then in your code, you would just need to grab the example.enviroment.variable property depending on the current profile and it would grab the correct environment variable.
To specify what application.properites look at using profiles in spring-boot.
If its an environment variable, then I think multiple applications can access the same variable.
But if its inside the application.properties file, then I think its not possible.
Not only that, if you really feel that one application needs to access the application.properties of another, then I believe this is not a right way to proceed.
Rather, you should externalize the configurations (maybe by using a config server like spring-cloud-config) and share the common properties between the applications.

Resources