0

I have checked similiar questions:

Spring - @Value returns null

Spring Boot: @Value returns always null

Yet, I cannot find what is wrong with my annotation

This how the code looks in IntelliJ screenshot from IntelliJ

This is whats beneath

@Component
public class VisitMapper {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

//more code below

I have two .properties files: application-dev.properties and application.properties. Active profile is set to dev. In application-dev.properties I have:

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

Not sure if it is important since active profile is set to dev, but in application.properties I have:

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

To sum up - why is password value shown as empty?

bkomo
  • 65
  • 1
  • 9
  • Have you actually ran the code? I suspect it isn't empty and you are thrown of by something in your IDE (which probably hides the passwords from prying eyes). – M. Deinum Nov 22 '21 at 09:47
  • I have some problems with running the code, which are probably unrelated to this issue. Applications starts, but when I try to debug and send any request I get errors. – bkomo Nov 22 '21 at 09:53
  • If it starts it is resolved, so as stated you are probably thrown of by the IDE (unless your errors are database related, but that would prevent your application from starting in the first place). – M. Deinum Nov 22 '21 at 09:54
  • Thank you for your answer. But wouldn't a message like "hidden" be better then "empty" to stop peepers? – bkomo Nov 22 '21 at 09:58
  • You would have to take that up with the people from Jetbrains. If your application starts the config is read correctly, else it would blow up. – M. Deinum Nov 22 '21 at 09:59
  • Can you post your comment as an answer, so I can mark it? – bkomo Nov 22 '21 at 10:02
  • Can you provide log app when started ? I think app running with application.properties. – Viettel Solutions Nov 24 '21 at 02:11

1 Answers1

1

I suspect that either Intellij hides the fields with password in them (although <empty> is a bit of a crappy filler) or Intellij has a hard time resolving the right property value for that field (for whatever reason).

However the fact that your application starts indicates that, at least, Spring Boot does resolve the properties correctly, else it wouldn't start and blow up. It wouldn't start either due to not being able to resolve the property or due to not being able to connect to the database.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224