Есть несколько способов получить любое значение из файла свойств. Одним из способов является
. У вас может быть отдельный класс, чтобы получить все свойства, чтобы все, что вы получите, было здесь
@Component
@ConfigurationProperties(prefix = "server")
@PropertySource("classpath:server.properties")
public class ServerProperties {
private String protocol;
private String port;
private String host;
...get and set methods here
}
, и просто связывайте их в вашем классе с помощью
@Autowired
private ServerProperties serverProperties;
Второй способ получения свойств -
private static Properties prop = new Properties();
prop.load(DummyClass.class.getResourceAsStream("/server.properties"));
String port = prop.getProperty("server.port");
Получить порт для запуска приложения
@Configuration
public class ServletConfig {
private static Properties prop = new Properties();
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
prop.load(DummyClass.class.getResourceAsStream("/server.properties"));
String port = prop.getProperty("server.port");
return (container -> {
container.setPort(port);
});
}
}