“Lambda函数返回 – 试图理解这是什么意思”

huangapple go评论81阅读模式
英文:

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(&quot;STARTING THE APPLICATION&quot;);
        SpringApplication.run(SpringBootConsoleApplication.class, args);
        LOG.info(&quot;APPLICATION FINISHED&quot;);
    }
 
    @Override
    public void run(String... args) {
        LOG.info(&quot;EXECUTING : command line runner&quot;);
 
        for (int i = 0; i &lt; args.length; ++i) {
            LOG.info(&quot;args[{}]: {}&quot;, i, args[i]);
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月13日 09:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定