简体   繁体   中英

What default values should the properties have in application.properties file in Spring

I am trying to figure out how to manage configuration profiles in my spring project.

I want to use different .properties files for different environments. There's a specific application-profileName.properties file for each environment and an application.properties file that is common for each environment. Sensitive data will only be contained in profile specific files.

I want to create a repository on GitHub. Only the common properties file will be added to the repository. Obviously, other people who want to run my project cloned from GitHub will have to create a properties file for their own profile, but then I need to somehow tell them which properties should be defined in the profile specific properties file. I don't want to list and describe this in the README, so I'm going to put stubs like this in the main properties file:

spring.datasource.url = YOUR_DATASOURCE_URL
spring.datasource.username = YOUR_DATASOURCE_USERNAME
spring.datasource.username = YOUR_DATASOURCE_PASSWORD

Thus, the person who cloned my project will be able to replace these values with his own in the main properties file, or create his profile specific properties file and override these values there.

Is this the right way to deal with the problem, and what are the common practices that apply in this situation?

Defining common properties across different profiles in the application.properties file is the correct way to go about it. You can have a setup like this:

src
   main
       java
         -- your src files here --
       resources
         application.properties
         application-sample.properties
              
pom.xml

If you want your code in GitHub to be useful to others, a clear documentation on how to use it should be given in the README.md file. But the call is left up to you. Remember that any popular Open Source project that has source code out there in GitHub became popular because people could understand the intend behind its creation. It is not always possible to read the code like a poem and understand what the original author intended to do.

You can mention

spring.profiles.active=yourprofilehere

inside your application.properties or you can ask user to create a new profile out of the sample profile provided and then pass the argument

--spring.profiles.active=yourprofilehere

as command line argument while running the jar built out of your codebase.

It would be great if you explain in README.md file that the person who clones will also have to edit pom.xml to include the new profile details if he intend to create a separate profile for himself.

PS I tend to remember what I wrote few weeks ago in my code only because it is immensely commented by myself. Otherwise I would end up asking which idiot wrote that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM