Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import lombok.Data;
- import org.springframework.boot.ApplicationRunner;
- import org.springframework.boot.WebApplicationType;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.context.annotation.Bean;
- @SpringBootApplication
- public class Sample {
- public static void main(String[] args) {
- new SpringApplicationBuilder(Sample.class).web(WebApplicationType.NONE)
- .properties("sample.used.calculator=first")
- .run(args);
- }
- @Bean
- ApplicationRunner sampleRunner(Calculator calculator) {
- return args -> System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" + calculator.calculate(new Parameter(3)));
- }
- @Bean
- @ConditionalOnProperty(prefix = "sample.used", name = "calculator", havingValue = "first", matchIfMissing = true)
- FirstCalculator firstCalculator() {
- return new FirstCalculator();
- }
- @Bean
- @ConditionalOnProperty(prefix = "sample.used", name = "calculator", havingValue = "second")
- SecondCalculator secondCalculator() {
- return new SecondCalculator();
- }
- }
- interface Calculator {
- Result calculate(Parameter parameter);
- }
- class FirstCalculator implements Calculator {
- @Override
- public Result calculate(Parameter parameter) {
- Integer value = parameter.getValue() * parameter.getValue();
- return new Result(value, getClass());
- }
- }
- class SecondCalculator implements Calculator {
- @Override
- public Result calculate(Parameter parameter) {
- Integer value = parameter.getValue() * parameter.getValue();
- return new Result(value, getClass());
- }
- }
- @Data
- class Parameter {
- private Integer value;
- Parameter(Integer value) {
- this.value = value;
- }
- }
- @Data
- class Result {
- private Integer value;
- private Class<? extends Calculator> calculator;
- Result(Integer value, Class<? extends Calculator> calculator) {
- this.value = value;
- this.calculator = calculator;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement