2

I added the following property into my application.properties to separate sensitive account infos from the main application.

#/src/main/resources/application.properties
spring.config.additional-location=/etc/java/myapp/

spring.datasource.url=jdbc:mysql://localhost/mytable

Additional:

#/etc/java/myapp/application.properties:
spring.datasource.username=theuser
spring.datasource.password=thepw

Result: the properties are not found!

java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

What am I doing wrong here?

Sidenote: if I add default values, the problem remains. The properties are not overridden!

spring.datasource.username=replaceme spring.datasource.password=replaceme

Result: java.sql.SQLException: Access denied for user 'replaceme'@'localhost' (using password: YES)

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Annotate your class where you are doing the DB stuff (dataSource related stuff)... Annotations that can be of help -> @Configuration, Value – N00b Pr0grammer Sep 20 '18 at 11:14
  • This question is about externalizing `application.properties`. Of course my setup works completely if I move the additional properties to my main ìnternal properties... – membersound Sep 20 '18 at 11:19
  • Maybe this SO answer will help: https://stackoverflow.com/questions/30171430/how-can-i-externalize-datasource-configuration-with-spring-boot – karen Sep 20 '18 at 11:27

1 Answers1

1

Note : from doc, from spring boot 2.0 spring.config.location upgraded to spring.config.additional-location

Command line : java -jar name --spring.config.additional-location=/etc/java/myapp/

spring.config.name and spring.config.location are used very early to determine which files have to be loaded, so they must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • 1
    This does not help, see my update. it seems the additional config is not taken into account if the path is configured from within the `application.properties` itself? – membersound Sep 20 '18 at 11:38
  • which version spring boot? and also did you try command line? `java - jar name --spring.config.additional-location=path` – Ryuzaki L Sep 20 '18 at 11:43
  • most recent. `spring-boot.2.0.5.RELEASE`. I thought it would work exactly as you proposed, but in fact it does not... – membersound Sep 20 '18 at 11:45
  • so this should not be part of `spring.config.additional-location=path` application.properties – Ryuzaki L Sep 20 '18 at 12:14
  • Ok so it has to clearly come from varargs... the question is: what can I do when having multiple applications in a `tomcat` server? Then each application would require it's own additional-location, which is impossible as the property must be provided globally as vararg... – membersound Sep 20 '18 at 12:44
  • In that case i will prefer system property or env variable options , if you are running each application with embedded tomcat then command line works prety good, but if you are deploying war in tomcat server then choose system poperty or env variable option – Ryuzaki L Sep 20 '18 at 12:57
  • i d like to know how to use properly spring.config.additional-location when building war file. i know that i can use a tomcat variable in bin/classpath.sh or bin/setenv.sh file like this JAVA_OPTS="$JAVA_OPTS -D spring.config.additional-location=/path/to/my/secret-location". But I don't wanna let configure that in tomcat config file. As i'm using gradle is there a way to let gradle configure that ? – soung Dec 23 '18 at 18:18
  • i never played with war files, i will try to find it out @soung – Ryuzaki L Dec 23 '18 at 19:21