英文:
Lambda function returned - trying to understand what this means
问题
在Spring Boot应用程序类的主方法内部,有一个简单的CommandLineRunner示例。
试图理解run方法中 **return args->** lambda函数的效果/含义。该方法应返回CommandLineRunner的实例。
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
// 在这里发生了什么?等价的Java 7 return语句是什么?
return args -> {
System.out.println("run正在工作");
};
}
英文:
In a simple example of CommandLineRunner from within the main method of a Spring Boot Application class.
Trying to understand the effect/meaning of return args-> lambda function in the run method. The method should return the instance of the CommandLineRunner.
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
// what happens here? What is the equivalent Java 7 return statement?
return args -> {
System.out.println("run is working");
};
}
答案1
得分: 1
查阅“functional interface”,Java 8 中新增的功能。CommandLineRunner 是其中之一。那段代码相当于返回一个匿名类。
英文:
Look into "functional interface", added in Java 8. CommandLineRunner is one. That code is equivalent to returning an anonymous class.
答案2
得分: 0
感谢为我澄清此事的人。因此,这将是相应的匿名类,看起来是这样的:
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
CommandLineRunner runner = new CommandLineRunner() {
public void run(String... s) {
System.out.println("run is working");
}
};
return runner;
}
英文:
Thanks to the person who clarified it for me. So this would be the corresponding anonymous class, it seems:
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
CommandLineRunner runner = new CommandLineRunner() {
public void run(String... s) {
System.out.println("run is working");
}
};
return runner;
}
答案3
得分: 0
谢谢,我也在寻找这个问题的答案。
只是补充一下 - CommandLineRunner是一个接口,更准确地说是一个"函数式接口",如果我理解正确的话,在Spring Boot中它只有一个方法run()。实际上,所有的函数式接口都只有一个抽象方法,当然也可以有默认实现或静态方法。
所以重点是 - 在这个上下文中,当我们返回一个lambda表达式时,基本上我们是在返回函数式接口commandlinerunner的run方法的实现。
这就是我的理解。这里有一些其他有用的信息和示例:
https://www.baeldung.com/spring-boot-console-app
根据下面的示例,我们也可以写出同样的内容:
public class SpringBootConsoleApplication
implements CommandLineRunner {
private static Logger LOG = LoggerFactory
.getLogger(SpringBootConsoleApplication.class);
public static void main(String[] args) {
LOG.info("STARTING THE APPLICATION");
SpringApplication.run(SpringBootConsoleApplication.class, args);
LOG.info("APPLICATION FINISHED");
}
@Override
public void run(String... args) {
LOG.info("EXECUTING: command line runner");
for (int i = 0; i < args.length; ++i) {
LOG.info("args[{}]: {}", i, args[i]);
}
}
}
英文:
Thanks I was also looking for the answer of this question.
Just to add - CommandLineRunner is an Interface, more precisely to say "functional interface", if I understand it correctly, in springboot that has only one method run(). Actually all the functional interface has only one abstract method, there could be default implementation or static method as well though.
So the point is - when we are returning a lambda in this context, basically we are returning an implementation of that run method of the functional interface commandlinerunner.
Thats how I understood. Some other useful information with example can be found here:
https://www.baeldung.com/spring-boot-console-app
We could also write the same thing as per the below example:
public class SpringBootConsoleApplication
implements CommandLineRunner {
private static Logger LOG = LoggerFactory
.getLogger(SpringBootConsoleApplication.class);
public static void main(String[] args) {
LOG.info("STARTING THE APPLICATION");
SpringApplication.run(SpringBootConsoleApplication.class, args);
LOG.info("APPLICATION FINISHED");
}
@Override
public void run(String... args) {
LOG.info("EXECUTING : command line runner");
for (int i = 0; i < args.length; ++i) {
LOG.info("args[{}]: {}", i, args[i]);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论