How to get resource files out with spring-boot

What I want to solve is as the title says. In spring-boot, DB connection information etc. are often written in application.properties under src / main / resources, but I will explain how to put this file externally and operate it with a definition different from that at compile time. I will. Previously, I posted "How to use Maven to place resource files outside JAR", but it is the spring-boot version of it. ..

Solution

It's really easy with spring-boot. Simply place the resource file (application.properties, etc.) you want to place externally in another folder and specify it as the classpath at runtime.

That is, if you run my-java-app.jar and want to load application.properties in the external_resource folder, include the external_resource folder in your runtime classpath.

Folder structure


my-java-app.jar
external_resource
  └ application.properties

command


java -classpath ./external_resource -jar my-java-app.jar

Commentary

The reason for this behavior is that in spring-boot, the resource file read priority is as follows.

  1. Run-time classpath
  2. Compile-time src / main / resources

In fact, there is a more detailed definition of read priorities, as others have explained below.

Finally

I'm grateful that spring-boot can reach the itchy place nicely. And thank you to @niwasawa for translating the official documentation.

Recommended Posts

How to get resource files out with spring-boot
How to get started with slim
How to share files with Docker Toolbox
[Note] How to get started with Rspec
How to get started with Eclipse Micro Profile
How to test a class that handles application.properties with SpringBoot (request: pointed out)
How to get started with creating a Rails app
How to get jdk etc from oracle with cli
[Ruby] How to comment out
How to number (number) with html.erb
How to update with activerecord-import
How to rollback migration files
Let's write how to make API with SpringBoot + Docker from 0
How to use Maven to place resource files outside the JAR
How to run only specific files with gem's rake test
How to get values in real time with TextWatcher (Android)
How to get started with Gatsby (TypeScript) x Netlify x Docker
How to get started with JDBC using PostgresSQL on MacOS
How to scroll horizontally with ScrollView
How to use Java HttpClient (Get)
How to get parameters in Spark
How to use mssql-tools with alpine
How to decompile java class files
Memo to get with Struts2 + Ajax
How to start Camunda with Docker
How to get boolean value with jQuery in rails simple form
How to install Adopt OpenJDK on Debian, Ubuntu with apt (-get)
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to use binding.pry for view files
How to get date data in Ruby
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to put out the error bundling
[Rails] How to use rails console with docker
I tried to get started with WebAssembly
How to switch thumbnail images with JavaScript
[IOS] How to get data from DynamoDB
[Java] How to output and write files!
How to do API-based control with cancancan
Try to implement login function with Spring-Boot
[Java] How to get the current directory
How to use args :, environment :, env_file: and .env files with docker-compose command
How to update related models with accepts_nested_attributes_for
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
[Rails] How to get the user information currently logged in with devise
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to get the date in java
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
How to deal with Precompiling assets failed.
[SpringBoot] How to write a controller test
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to get the query string to actually issue when using PreparedStatement with JDBC
How to get the ID of a user authenticated with Firebase in Swift