How to change application.properties settings at boot time in Spring boot

The influence of the new corona is gradually beginning to appear in the workplace, and I am thrilled if I will stop working.

Meanwhile, I was addicted to it for the first time in a long time, so I decided to record it. Moreover, if you can understand it, it's a very simple story and it's quite dented. Perhaps it's too rudimentary for Java enthusiasts to publish to the Web. ..

Thing you want to do

8h for such an easy thing.

Addictive points

Event reproduction

https://github.com/t-o-z/configuration-demo

Set up according to the Readme,

docker run config-demo

Then it is included in application.properties

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-27 09:51:15.557  INFO 7 --- [           main] c.z.c.ConfigurationDemoApplication       : Starting ConfigurationDemoApplication v0.0.1-SNAPSHOT on 6c889b4a8ada with PID 7 (/usr/src/myapp/configuration-demo-0.0.1-SNAPSHOT.jar started by root in /usr/src/myapp)
2020-02-27 09:51:15.566  INFO 7 --- [           main] c.z.c.ConfigurationDemoApplication       : No active profile set, falling back to default profiles: default
2020-02-27 09:51:16.918  INFO 7 --- [           main] c.z.c.ConfigurationDemoApplication       : Started ConfigurationDemoApplication in 2.482 seconds (JVM running for 3.809)
key1-value

Is displayed.

Apply application.properties under classes

sample.key1=key1-value1

Even if you rewrite it and execute it again

user:configuration-demo user$ docker run config-demo

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-27 09:58:45.004  INFO 6 --- [           main] c.z.c.ConfigurationDemoApplication       : Starting ConfigurationDemoApplication v0.0.1-SNAPSHOT on 9999dad12dac with PID 6 (/usr/src/myapp/configuration-demo-0.0.1-SNAPSHOT.jar started by root in /usr/src/myapp)
2020-02-27 09:58:45.014  INFO 6 --- [           main] c.z.c.ConfigurationDemoApplication       : No active profile set, falling back to default profiles: default
2020-02-27 09:58:46.425  INFO 6 --- [           main] c.z.c.ConfigurationDemoApplication       : Started ConfigurationDemoApplication in 2.564 seconds (JVM running for 3.881)
key1-value

It will not be reflected. ..

I tried various things.

Even if you check the countermeasures

There are various things like that, but is it possible to do it with just one file? I didn't have much information. The last Hot Reload is to set up a server just to do this.

Conclusion

** Copy to / config! ** **

I've already made it bold after trial and error,

classes/config

That's no good.

/config

Create a new one and place classes / application.properties in it.

target 2020-02-27 19-21-35.png
sample.key1=modify

And start Docker (because it is COPY, rebuild is required).

Otsuka:configuration-demo otsukatakuya$ docker run config-demo

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-27 10:54:32.393  INFO 6 --- [           main] c.z.c.ConfigurationDemoApplication       : Starting ConfigurationDemoApplication v0.0.1-SNAPSHOT on c43d6b28259a with PID 6 (/usr/src/myapp/configuration-demo-0.0.1-SNAPSHOT.jar started by root in /usr/src/myapp)
2020-02-27 10:54:32.403  INFO 6 --- [           main] c.z.c.ConfigurationDemoApplication       : No active profile set, falling back to default profiles: default
2020-02-27 10:54:33.804  INFO 6 --- [           main] c.z.c.ConfigurationDemoApplication       : Started ConfigurationDemoApplication in 2.474 seconds (JVM running for 3.59)
modify

Where is it written?

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

It was mentioned in the official Spring boot reference.

SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

  1. A /config subdirectory of the current directory
  1. The current directory
  1. A classpath /config package
  1. The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

I intended to try 1, but I couldn't try 3 first. .. Perhaps the classpath is in the jar and will conflict with it?

Some people have suggested a way to write exclusions in pom.xml and not include them in the build, but all you have to do is add them! I learned a lot because there were few articles that I wrote briefly.

Recommended Posts

How to change application.properties settings at boot time in Spring boot
Change session timeout time in Spring Boot
How to add a classpath in Spring Boot
How to bind to property file in Spring Boot
Spring Boot --How to set session timeout time
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
How to set Spring Boot + PostgreSQL
How to call and use API in Java (Spring Boot)
How to use ModelMapper (Spring boot)
How to control transactions in Spring Boot without using @Transactional
How to change app name in rails
How to include Spring Tool in Eclipse 4.6.3?
How to split Spring Boot message file
How to not start Flyway when running unit tests in Spring Boot
How to change the value of a variable at a breakpoint in intelliJ
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
How to define multiple orm.xml in Spring4, JPA2.1
[Spring Boot] How to refer to the property file
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to set environment variables in the properties file of Spring boot application
Settings for connecting to MySQL with Spring Boot + Spring JDBC
How to set the display time to Japan time in Rails
How to set Dependency Injection (DI) for Spring Boot
How to write a unit test for Spring Boot 2
Change date and time to Japanese notation in Rails
[Spring Boot] How to create a project (for beginners)
How to test file upload screen in Spring + Selenium
Introduce swagger-ui to REST API implemented in Spring Boot
How to boot by environment with Spring Boot of Maven
[Beginner] How to use devise Change settings from introduction
How to use In-Memory Job repository in Spring Batch
How to implement authentication process by specifying user name and password in Spring Boot
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
How to set chrony when the time shifts in CentOS7
[Spring Boot] Move validation (@Validated / @Valid) at any time [Bean Validation]
How to change a string in an array to a number in Ruby
Spring Boot 2 multi-project in Gradle
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
How to get values in real time with TextWatcher (Android)
Spring Boot Hot Swapping settings
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Spring Boot performance related settings
Introduction to Spring Boot Part 1
How to monitor application information in real time using JConsole
How to change BackgroundColor etc. of NavigationBar in Swift UI
Let's find out how to receive in Request Body with REST API of Spring Boot
Use @ControllerAdvice, @ExceptionHandler, HandlerExceptionResolver in Spring Boot to catch exceptions
[Sprint Boot] How to use 3 types of SqlParameterSource defined in org.springframework.jdbc.core.namedparam
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
Change Spring Boot REST API request / response from CamelCase to SankeCase
How to create your own Controller corresponding to / error with Spring Boot
How to set and use profile in annotation-based Configuration in Spring framework
What I did in the migration from Spring Boot 1.4 series to 2.0 series