Answer the question
In order to leave comments, you need to log in
How to properly load resources in Spring?
Good afternoon
I am writing an application in which at startup it is necessary that configuration files (properties + json) be pulled up.
Application services will constantly access this configuration and I don’t want to take it out separately to the database.
The idea is that configuration files are read at startup, configuration data can be obtained by calling the singleton method
Question one:
As I understand it, the logic for reading application settings at startup can be done using the InitializingBean Interface https://www.baeldung.com/running-setup -logic-on-st...
In the same class, the received parameters are "added" to the singleton, which will be accessed by the services
Question two
Do I have the right approach to solving this problem?
MyConfig.getInstance().getValue("key")
Answer the question
In order to leave comments, you need to log in
Another simple solution -
Create an ApplicationProperties file
@Configuration
@ConfigurationProperties(prefix = "application")
public class AppProperties {
@Getter
@Setter
private String baseUrl;
@Getter
@Setter
private String uploadPath;
}
application.base_url=http://localhost:7777
application.upload_path=/home/admin/application.com/uploads
base_url => baseUrl upload_path => uploadPath
@EnableConfigurationProperties({
ApplicationProperties.class
})
public class MyApplication {
}
@Controller
@RequiredArgsConstructor
public class MyController {
private final ApplicationProperties properties;
// ... тут методы контроллера
String uploadPath = properties.getUploadPath();
//...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question