O
O
Orkhan Hasanli2018-12-28 20:34:32
Java
Orkhan Hasanli, 2018-12-28 20:34:32

Why does Spring ignore configuration properties of profile files?

Hello!
I've asked this question before, but unfortunately I haven't found an answer yet.
Old question - How to understand Spring profiles?
In short:
- Built a project from https://start.spring.io/ and imported it into intellij idea.
- Created 2 additional properties file application-production.properties & application-development.properties. Both files are in resources at the level with the application.properties file
- Added to the application.properties file:
spring.profiles.active=development
- Added to the application-development.properties file:

server.port=7777
spring.mail.host=smtp.yandex.ru
[email protected]
spring.mail.password=12345
spring.mail.port=465
spring.mail.protocol=smtps
mail.debug=true

- Created a regular java file for mail configuration and imported string values ​​using @Value.
For example,
@Value("${spring.mail.host}")
    private String host;

I run the application and get an exception:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.mail.host' in value "${spring.mail.host}"

Intellij "sees" the values ​​from the fields because it automatically substitutes them into @Value.
Only if you add configs to the main file, then it will be able to run the application correctly.
I tried to "enable" profiles in intellij in the following ways:
1) a line in the application.properties file (see above)
2) the change profiles link displayed in Intellij IDEA (in the TestApplication class)
5c265e61c611f028574165.png
3) passing the profile name to the application configuration
5c265e83b3689849737189.png
Thank you in advance for your help!
PS I found the following sources with similar "problems".
https://stackoverflow.com/questions/32861600/how-t...
https://www.jetbrains.com/help/idea/spring-support.html
Configuration made by intellij by default:
5c266ca7b59b4774407578.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey P, 2018-12-30
@azerphoenix

Try using YAML and put all profiles in one file, like this:
#application.yml

spring.profiles.active: development

---

spring:
  profiles: development

server.port: 7777
spring.mail.host: smtp.yandex.ru
spring.mail.username: [email protected]
spring.mail.password: 12345
spring.mail.port: 465
spring.mail.protocol: smtps
mail.debug: true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question