I am new to Java and spring boot. I am trying to create simple application where some data will be read from config.yml file like url and file for different server to update the data from file. My config.yml looks like this.
appserver1:
url: http://localhost:8080/api1
file: \file1.txt
appserver2:
url: http://localhost:8081/api2
file: \file2.txt
appserver3:
url: http://localhost:8082/api3
file: \file3.txt
I created 3 class where naming AppServer1 , AppServer2 and AppServer3 all have only 2 property containing URL and file path.
public class AppServer1 {
private String URL;
private String filePath;
public String getURL() {
return URL;
}
public void setURL(String URL) {
this.URL = URL;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
How can I make the configuration such that I can use the above classes as Autowired where ever I need to access the AppServer1 or AppServer2 or AppServer3 all over my project. For eg. - If I want to use AppServer1 details I can directly Autowired AppServer1 and use the URL and File path in that class.
I have tried 1 solution using @Value but wanted to know if there is any other as if my config file is big so using @Value will be too hard. Below is the code I tried.
@Configuration
public class AppServer1 {
@Value("${appserver1.url}")
private String url;
@Value("${appserver1.file}")
private String file;
@Bean
AppServer1 beanExample() {
return new AppServer1(url, file);
}
}
Is there any annotation which can be used with @Configuration to directly identify the config data and used as Autowired.
Thanks.