Среда:
jdk 11
пружинный башмак 2.1.3
Код:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public ApplicationRunner calculationRunner(Calculator calculator,
@Value("${lhs}") int lhs,
@Value("${rhs}") int rhs,
@Value("${op}") char op) {
return args -> calculator.calculate(lhs, rhs, op);
}
}
public class Calculator {
private final Collection<Operation> operations;
public Calculator(Collection<Operation> operations) {
this.operations = operations;
}
public void calculate(int lhs, int rhs, char op) {
for (var operation : operations) {
if (operation.handles(op)) {
var result = operation.apply(lhs, rhs);
System.out.printf("%d %s %d = %s%n", lhs, op, rhs, result);
return;
}
}
throw new IllegalArgumentException("Unknown operation " + op);
}
}
public interface Operation {
int apply(int lhs, int rhs);
boolean handles(char op);
}
@Component
public class Addition implements Operation {
@Override
public int apply(int lhs, int rhs) {
return lhs + rhs;
}
@Override
public boolean handles(char op) {
return '+' == op;
}
}
Консоль:
:: Spring Boot :: (v2.1.3.RELEASE)
ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-11 15:16:13.709 ERROR 4652 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method calculationRunner in com.example.demo.DemoApplication required a bean of type 'com.example.demo.Calculator' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.Calculator' in your configuration.
Структура введите описание изображения здесь
Я изучаю загрузку Spring на Externalize Propertiesсм. вопрос.Я знаю, как с этим бороться.
Надеюсь, вы поможете мне решить проблему, большое спасибо.