2
server.port = ?
spring.datasource.url = ?
spring.datasource.username = ?
spring.datasource.password = ?

I want to externalize all the "?" values outside the application.properties, and have them in a text file or something.

I already have a configuration.txt file which holds other values, used in the services, but I just don't know how it works for the application.properties.

Solved, just have the property file in the same path where the jar file is, then spring boot will replace the values for you.

locomotive
  • 25
  • 5
  • The application.properties file is already a text file. Why do you want it to reference a second text file to serve the same purpose? Are you simply looking to the put the application.properties file outside of your jar? If so you can do that: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – Jeremy Smith Sep 20 '19 at 14:29
  • Yes, trying to take the values outside the jar, so I do not need to re-package everytime when I change DB or port values – locomotive Sep 20 '19 at 14:33
  • The answer in this post may help: https://stackoverflow.com/questions/39427675/application-properties-outside-jar-file-how-to – Jeremy Smith Sep 20 '19 at 14:40
  • Possible duplicate of [Precedence order among properties file, YAML file, and Command Line arguments in SpringBoot](https://stackoverflow.com/questions/45822326/precedence-order-among-properties-file-yaml-file-and-command-line-arguments-in) – Alan Hay Sep 20 '19 at 14:52

1 Answers1

1

Spring.io Externalized Configuration

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
@TestPropertySource annotations on your tests.
properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
Command line arguments.
Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
ServletConfig init parameters.
ServletContext init parameters.
JNDI attributes from java:comp/env.
Java System properties (System.getProperties()).
OS environment variables.
A RandomValuePropertySource that has properties only in random.*.
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
Application properties outside of your packaged jar (application.properties and YAML variants).
Application properties packaged inside your jar (application.properties and YAML variants).

What this means is that what you want to do is supported without needing to do anything fancy inside your application.properties file.

Spring Boot will look at the application.properties file outside of your jar and consider any values in there and use them instead of any values in the application.properties file inside of your jar.

So, wherever your jar is, put the application.properties file you want for that environment. See the link for more details on just how much you can customize this (profiles, YAML, system properties, environment variables, etc.)

You might also consider moving to a Spring Cloud Config implementation, but that's a bit more work.

Jeremy Smith
  • 311
  • 2
  • 13
  • I'm making this jar into a service using winsw.net4. Do I have to have the argument "java -Dspring.config.location=" inside the winsw.net4? Or As you said "So, wherever your jar is, put the application.properties file you want for that environment.", does the file type have to be in PROPERTIES File or can it be .txt type? – locomotive Sep 20 '19 at 14:51
  • If you want Spring to detect it, it needs to be application.properties. It needs to be in the working directory when the jar is called, so specific location depends on the working directory / startup script. For example, if you're in /user/locomotive and ./java -jar ./bin/myApp.jar - then you'd want the application.properties file in the /user/locomotive folder, but if you're in /usr/locomotive/bin and run ./java -jar myApp.jar, then you'd have them in the same folder. – Jeremy Smith Sep 20 '19 at 14:55
  • So, if I put it in the same place where the jar is, I do not need to touch the arguments or anything? – locomotive Sep 20 '19 at 15:02
  • Correct, as long as that's the working directory when you run the jar. – Jeremy Smith Sep 20 '19 at 15:07
  • Awesome, Thank you Jeremy – locomotive Sep 20 '19 at 15:24