E
E
Evgeny Krasnov2020-09-28 11:13:48
Spring
Evgeny Krasnov, 2020-09-28 11:13:48

Spring application.properties how to do?

Greetings dear forum users.

Here I read here https://stackoverflow.com/questions/32058814 and I can't make out the solution. Solution with 100 likes.

I'm trying to do it, but it turns out what kind of mess. Can someone please explain the solution to this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-09-28
@Favorskij

Hello!
Easily...
1) Create a class for example,

@Configuration
@ConfigurationProperties(prefix = "custom")
public class AppProperties {

// названия полей должны соответствовать названиям из properties. 
private String baseUrl;
private String uploadPath;

// тут геттеры 
}

And here is an example application.properties
custom.base_url=http://localhost:8080
custom.upload_path=/home/example.com/uploads

Spring itself already binds base_url to baseUrl and so on.
The only thing, @ConfigurationProperties(prefix = "custom")
here in prefix, specify the prefix that you need. Those. here custom.base_urlcustom is a prefix.
Details here - https://www.baeldung.com/configuration-properties-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question