4 paths in application.properties - spring

What are the differences between the following 4 paths in application.properties file in Spring project and not Spring Boot?
application.path
servlet.path
server.servlet.context-path
spring.mvc.servlet.path

These examples are all shown assuming your application is running on localhost, port 8080.
server.servlet.context-path defines the context, if not set then it's the root context (/). If you set this to foo, your app will listen on http://localhost:8080/foo. Information can be found here
spring.mvc.servlet.path sets the path to the dispatch servlet, it immediately follows the server.servlet.context-path. If you set this to bar and don't have server.servlet.context-path set, your app will listen on http://localhost:8080/bar. If you have the server.servlet.context-path set to foo and spring.mvc.servlet.path set to bar, your app will be listening on http://localhost:8080/foo/bar. Information can be found
here
I am unaware of the properties application.path or servlet.path. That's not to say that you aren't seeing them in an application.properties file somewhere, just that they don't have any meaning.

Related

Quarkus #ConfigMapping: when no prefix defined, skip/ignore system and environment variables, only scan properties with defined keys

Some properties defined in my app are used by other applications in the same organization, so I cannot add a dedicated namespace before them to differentiate. While moving to Quarkus #ConfigMapping, I found Quarkus by default scans all system and environment variables as well as application scoped properties, and non-mapped properties will stop app from launching, showing a lot of "cannot find any root to map" error.
Quarkus YAML config is based on Smallrye config, which has:
smallrye.config.mapping.validate-unknown=false
to stop this behaviour.
https://smallrye.io/smallrye-config/2.11.1/config/mappings/#retrieval
For a Config Mapping to be valid, it needs to match every configuration property name contained in the Config under the specified prefix set in #ConfigMapping. This prevents unknown configuration properties in the Config. This behaviour can be disabled with the configuration smallrye.config.mapping.validate-unknown=false.

Where does my Spring Boot Config Value come from?

Spring Boot allows a lot of ways to configure your application and to overwrite existing configuration properties (https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-external-config.html)
In our application I guess we use every single feature described there (at least twice). Now starting the application (of corse in a container in kubernetes) I have discovered some misconfiguration.
Is there a way to find out where a configuration property comes from? Something providing some output like
property "foo":
-> from PropertySource classpath://myFooSource.proproperties = baa
-> from application.properties = baz
-> from environment variable FOO = bar
-> final from command line --foo=ping
I would say that Spring Boot Actuator /env endpoint is what you are looking for, given that it returns the current environment properties in your application. You can enable it with management.endpoint.shutdown.enabled=true. You can check more details in the reference documentation at https://docs.spring.io/spring-boot/docs/current/actuator-api/htmlsingle/#env.

Custom application property to be supplied to spring boot app through cmd line

I was wondering if we can supply a custom attribute (a key to be in application.properties file), I know for sure that -Dserver.port=8080 works, and overrides the property value, but server.port is a spring boot's expected property value.
How about something other than that, for example a jdbc connection string or service name? does -Ddb.service.name=dbservice work?
Yes, any property can be set via system property. You can use -D or -- notation. There are also a variety of property sources Spring Boot uses:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Set application.properties value from JNDI Environment value

I am building a spring boot application, that has RabbitMQ configuration like below.
spring.rabbitmq.host=host
spring.rabbitmq.port=5672
spring.rabbitmq.username=userName
spring.rabbitmq.password=password
I need to set environment specific configuration. But that configuration needs to be read from tomcat context.xml file.
I would need to pass the values for host, username, and password by reading from tomcat Environment tag set in context.xml.
How can I do this?
Spring docs says: 24.3 Application Property Files
If your application runs in a container, then JNDI properties (in java:comp/env) or servlet context initialization parameters can be used instead of, or as well as, environment variables or system properties.
can they be directly used like this:
spring.rabbitmq.host="${rabbitMQHost}"
spring.rabbitmq.port=5672
spring.rabbitmq.username=${rabbitMQUserName}
spring.rabbitmq.password=${rabbitMQPassword}

spring boot YAML default and environment variable override like HOCON files

Is there a way in spring-boot YAML file to do the same as in HOCON files where you can have a default and be able to override it with an environment variable like this:
basedir = "/whatever/whatever"
basedir = ${?FORCED_BASEDIR}
In this case in HOCON if you don't define a environment variable named FORCED_BASEDIR then basedir will be "/whatever/whatever" but if you do then the value of basedir will be whatever is defined in the environment variable.
Thanks
So based on webdizz answer below I looked up a little bit and I found a pretty good description in book "Spring Boot in Action". Here is the hierarchy:
There are, in fact, several ways to set properties for a Spring Boot application. Spring
Boot will draw properties from several property sources, including the following:
Command-line arguments
JNDI attributes from java:comp/env
JVM system properties
Operating system environment variables
Randomly generated values for properties prefixed with random.* (referenced
when setting other properties, such as `${random.long})
An application.properties or application.yml file outside of the application
Licensed to Thomas Snead 58 CHAPTER 3 Customizing configuration
An application.properties or application.yml file packaged inside of the
application
Property sources specified by #PropertySource
Default properties
Spring Boot provides means to define variables at many levels and your case is supported, you just need to define variable in following way:
in application.yml:
basedir: "/whatever/whatever"
and in environment:
export BASEDIR = "/another/whatever"
Then in runtime application will use value from environment.
For more details check this out enter link description here.

Resources