【发布时间】:2018-08-30 10:45:10
【问题描述】:
我编写了一个简单的 Spring Boot 应用程序,稍后我将对其进行扩展以构建一个 Spring REST 客户端。我有一个工作代码;我试图更改一些实例变量名称和方法名称并四处玩弄。
代码:
@SpringBootApplication
public class RestClientApplication {
public static void main(String[] args) {
SpringApplication.run(RestClientApplication.class, args);
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
RestClientApplication.class)) {
System.out.println(" Getting RestTemplateBuilder : " + ctx.getBean("restTemplateBuilder"));
System.out.println(" Getting RestTemplate : " + ctx.getBean("restTemplate"));
}
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
@Bean
public CommandLineRunner runner() {
return args -> { SOP("hello"); }
}
}
观察:
- 实例变量名称遵循驼峰式表示法,如 预期的。因此,restTemplate 和 restTemplateBuilder 有效。
- 通过restTemplate() 方法创建RestTemplate 实例时,我尝试将参数名称更改为builder。它有效。
- 通过 restTemplate() 方法创建 RestTemplate 实例时,我尝试将方法名称更改为随机名称,但出现“没有可用的名为 'restTemplate' 的 bean”的异常。
- CommandLineRunner 接口是通过 lambda 表达式实现的。访问 commandLineRunner 会引发异常。
问题
为什么我会看到第 2 点和第 3 点中提到的结果?
【问题讨论】:
标签: spring spring-boot lambda functional-interface