Spring.AppConfig

application.yaml

server:
  port: 8080

app:
  client-service:
    value: 1
    url: https://httpbin.org/get
    stub-mode: ${CLIENT_SERVICE_STUB} // depends on enviroment, in enviroment APP_CLIENT_SERVICE_STUB

in app config

@EnableConfigurationProperties(value = AppProperties.class)
@Configuration
@EnableConfigurationProperties(value = AppProperties.class)
public class AppConfig {

    @Value("$(app.client-service.url)") // Так можно делать, но не надо делать в проде
    private String value;

    @Bean
    @Profile("!dev")
    public ClientService clientService(AppProperties appProperties) {
        return new ClientServiceImpl(appProperties);
    }

    @Bean
    @Profile("dev")
    public ClientService clientServiceStub(AppProperties appProperties) {
        return new ClientServiceStubImpl();
    }
}

AppProperties.java

package SpringBootApp.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "app")
@Getter
@Setter
public class AppProperties {
    private String value;

    @Getter
    @Setter
    private ClientServiceProperties clientServiceProperties;

    static public class ClientServiceProperties {
        private String url;

        private String stubMode;
    }
}

This entry was posted in Без рубрики. Bookmark the permalink.