#Configuration bean to set spring.application.name - spring-boot

What would be the appropriate way to set spring.application.name in an #Configuration file instead of within an applicaiton.properties file?
Thanks,
Brian

Basically I want to set the application name the same as the
following: myartifiact-myversion myartifact
myversion-SNAPSHOT
From docs , you can use #..# placeholders to refer to properties in pom.xml if you use spring-boot-starter-parent (Normally most spring-boot project already use it) .
So in the application.properties:
spring.application.name=#artifactId#-#version#

Related

Spring Boot yml file read order

I have below files under resources folder in a standard Spring Boot app .
Spring.active.profile is set to dev
In which order the properties files are read .?
1)application.yml
2)bootstrap.yml
3)application_dev.yml
4)bootstrap_dev.yml
As Spring doc mentions
Profile specific properties are loaded from the same locations as
standard application.properties, with profiles specific files
overriding the default ones
This would mean that first the application.yml is read and then the application_dev.yml is read and overrides values from the default application.yml if needed.
Same for bootstrap.yml and bootstrap-dev.yml
Also as you can see here
bootstrap.yml is loaded before application.yml.
So to answer your question the order should be
bootstrap.yml
bootstrap_dev.yml
application.yml
application_dev.yml
bootstrap files are always the first: bootstrap.yml then bootstrap-{profile}.yml. then application.yml and application-{profile}.yml.
the property values are overridden by the next files so:
a: 1 from application.yml will be overridden by a: 55 from application-{profile}.yml

spring-boot - how to specify path of application.properties in envrionment or system properties?

If I want to run spring boot application, and want to use difference application.properties in difference path (other than using profile)
How can I specify path for application.properties?
If you want to use difference application.properties in difference path, use this command to start the jar file
nohup java -jar project.jar --spring.config.location=file://{file-path}/application.properties
environment variable SPRING_CONFIG_LOCATION can be also used.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
#PropertySource annotation is used to provide properties file into the environment and it is used with #Configuration classes.
#PropertySource({ "classpath:config.properties" })
use bellow code above main class:
#PropertySource(value = {"file:///${HOMEDIR}/application.properties"})

Can spring-boot application.properties file woking with log4j2.xml configuration?

I want to define high-level file logging in application.properties as a convenience to leverage my log4j2.xml file configuration. By itself my log4j2 config is working fine, however I was hoping to control logging levels and log file and path info from the application.properties file. I have the spring-boot-starter-log4j2 dependency in the application's pom file.
In log4j2.xml I have as one of the properties
<Property name="LOG_FILE">${LOG-DIR}/test.log</Property>
, where LOG-DIR is defined in another (previous) property in the same file. In my application.properties file, I have
logging.file=LOG_FILE
as a property, plus several level properties such as
logging.level.org.springframework.web=DEBUG
none of these log-related properties as defined in my application.properties file are working to build the corresponding log file. Again, when I simply use log4j2.xml by itself it works fine, but wanted to take advantage of the convenience of application.properties for logging configuration.
Any insights into what I am doing wrong are greatly appreciated. thank you
If I understood your question right, you are looking at Property Substitution feature of log4j2.
You can define logging property in application.properties file like below:
log.file.path=/myDir/logpath
And then the property(s) lookup defined as Property in log4j2.xml:
<Configuration>
<Properties>
<property name="LOG_FILE">${bundle:application:log.file.path}</property>
</Properties>
Now all the property can be referred in same log4j2.xml with ${propertyName} notation, which eventually points to values from application.properties file.

SpringBoot: Separate property files

I've created my application.properties file:
spring.config.additional-location=C:\Users\user\
spring.datasource.url=jdbc:postgresql://<db>:<port>/<db>
I need to feed Spring with an additional file located on C:\Users\user\application.properties:
spring.datasource.username=user
spring.datasource.password=password
As you can see I've tried to use spring.config.additional-location property into my application.properties file.
However, bootstrap tells me that no authentication has been provided.
you can use another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:
$ java -jar myproject.jar --spring.config.name=myproject
Reference URL:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
You can specify your alternative properties using #PropertySources like this:
#PropertySources({
#PropertySource({"classpath:application.properties"}),
#PropertySource(value = {"file:${conf-dir}}/application-override.properties" },ignoreResourceNotFound = true)
})
public class AppConfig {
...
The properties in the bottom PropertySource will override properties from first one, if the file exists.
The documentation says to use file:C:/Users/user/
the "file:" or "classpath:" part is important.
There are numerous other ways to do this, too.
- profiles (and per profile application-<profile>.properties)
- #Configuration + #PropertySource
- ...

How can I suppress the default config location of Spring boot

How can I have Spring Boot only look for config files under the the directories specified by spring.config.location property and not look under the default location as specified in the ConfigFileApplicationListener javadoc.
Setting spring.config.location, causes the ConfigFileApplicationListener to look in both spring.config.location directories and the default locations.
you can use profiles.
in your application.properties you will only have :
spring.profiles.active=other
and have a file in the same folder named application-other.properties where you define your properties.
the default properties can be in a file named application-default.properties and when you want to use it, juste change the value in application.properties to 'default'.
Spring-boot documentation of Profiles

Resources