Advertisement
Guest User

Untitled

a guest
May 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package sample;
  2.  
  3. import lombok.Data;
  4. import org.springframework.boot.ApplicationRunner;
  5. import org.springframework.boot.WebApplicationType;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  8. import org.springframework.boot.builder.SpringApplicationBuilder;
  9. import org.springframework.context.annotation.Bean;
  10.  
  11. @SpringBootApplication
  12. public class Sample {
  13. public static void main(String[] args) {
  14. new SpringApplicationBuilder(Sample.class).web(WebApplicationType.NONE)
  15. .properties("sample.used.calculator=first")
  16. .run(args);
  17. }
  18.  
  19. @Bean
  20. ApplicationRunner sampleRunner(Calculator calculator) {
  21. return args -> System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" + calculator.calculate(new Parameter(3)));
  22. }
  23.  
  24. @Bean
  25. @ConditionalOnProperty(prefix = "sample.used", name = "calculator", havingValue = "first", matchIfMissing = true)
  26. FirstCalculator firstCalculator() {
  27. return new FirstCalculator();
  28. }
  29.  
  30. @Bean
  31. @ConditionalOnProperty(prefix = "sample.used", name = "calculator", havingValue = "second")
  32. SecondCalculator secondCalculator() {
  33. return new SecondCalculator();
  34. }
  35. }
  36.  
  37. interface Calculator {
  38. Result calculate(Parameter parameter);
  39. }
  40.  
  41.  
  42. class FirstCalculator implements Calculator {
  43.  
  44. @Override
  45. public Result calculate(Parameter parameter) {
  46. Integer value = parameter.getValue() * parameter.getValue();
  47. return new Result(value, getClass());
  48. }
  49. }
  50.  
  51.  
  52. class SecondCalculator implements Calculator {
  53.  
  54. @Override
  55. public Result calculate(Parameter parameter) {
  56. Integer value = parameter.getValue() * parameter.getValue();
  57. return new Result(value, getClass());
  58. }
  59. }
  60.  
  61. @Data
  62. class Parameter {
  63. private Integer value;
  64.  
  65. Parameter(Integer value) {
  66. this.value = value;
  67. }
  68. }
  69.  
  70. @Data
  71. class Result {
  72. private Integer value;
  73. private Class<? extends Calculator> calculator;
  74.  
  75. Result(Integer value, Class<? extends Calculator> calculator) {
  76. this.value = value;
  77. this.calculator = calculator;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement