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. ..
8h for such an easy thing.
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.
** 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.
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
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:
- A /config subdirectory of the current directory
- The current directory
- A classpath /config package
- 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