Java: 任意对象的方法引用

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

Java : method reference of an arbitrary object

问题

以下是翻译好的内容:

给定以下代码(为保持简单,已脱离上下文复制)

它可以正常运行,并启用了OAuth2 JWT令牌验证。

...
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
...
protected void configure(HttpSecurity http) throws Exception {
    http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
}
...

我难以理解OAuth2ResourceServerConfigurer::jwt方法引用。

httpConfig.oauth2ResourceServer期望一个Customizer作为输入。Customizer是一个函数式接口,定义了一个void customize(T t)实例方法。

OAuth2ResourceServerConfigurerjwt实例方法返回一个类型为OAuth2ResourceServerConfigurer.JwtConfigurer的对象,该对象并未定义void customize(T t)方法。

它如何符合Customizer接口?

此外,我不理解哪个OAuth2ResourceServerConfigurer实例将用于调用jwt()方法。

基本的Java文章并没有对我有所帮助。(https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html)

我对Lambda和方法引用很熟悉,但这个我无法理解。

英文:

Given the following code (copied out of context to keep things simple)
It works fine, and enables OAuth2 JWT token validation.

...
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
...
protected void configure(HttpSecurity http) throws Exception {
    http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
}
...

I have a hard time understanding the OAuth2ResourceServerConfigurer::jwt method reference

httpConfig.oauth2ResourceServer expects an Customizer as input. The Customizer is a functionnal interface that defines one instance method which is void customize(T t).

The jwt instance method of OAuth2ResourceServerConfigurer returns an object of type OAuth2ResourceServerConfigurer.JwtConfigurer, which does not define the void customize(T t) method.

How can it comply with the Customizer interface?

Also, I don't understand which instance of OAuth2ResourceServerConfigurer will be used to call the 'jwt()' method.

The basic Java article does not help me. (https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html)

I am familiar with Lambdas and method references, but this one I don't get.

答案1

得分: 1

> 它如何与 Customizer 接口保持一致?

当方法引用实现一个函数式接口时,参数和返回类型必须保持一致(兼容),而不是方法名。

与 lambda 表达式必须具有与函数式方法兼容的参数和返回类型一致,但方法名没有给出的方式一致。

> 另外,我不理解将使用 OAuth2ResourceServerConfigurer 的哪个实例来调用 jwt() 方法。

在您提供的链接中,查看名为“特定类型的任意对象的实例方法的引用”的部分。

由于您的方法引用是 OAuth2ResourceServerConfigurer::jwt,它是一个无参实例方法,customize(T t) 方法的参数 t 将成为 jwt() 调用的 this 值。

这基本上与以下 lambda 表达式相同:

http.oauth2ResourceServer(t -> t.jwt());

这等同于以下匿名类:

http.oauth2ResourceServer(new Customizer<OAuth2ResourceServerConfigurer<HttpSecurity>>() {
    @Override
    public void customize(OAuth2ResourceServerConfigurer<HttpSecurity> t) {
        t.jwt();
    }
});
英文:

> How can it comply with the Customizer interface?

When a method reference implements a functional interface, the parameters and the return type have to comply (be compatible), not the method name.

Consistent with how a lambda expression has to have parameters and a return type that is compatible with the functional method, but the method name isn't given anywhere.

> Also, I don't understand which instance of OAuth2ResourceServerConfigurer will be used to call the jwt() method.

In the link you provided, see the section named "Reference to an Instance Method of an Arbitrary Object of a Particular Type".

Since your method reference is OAuth2ResourceServerConfigurer::jwt, which is a no-arg instance method, the t parameter of the customize(T t) method becomes the this value of the jwt() call.

It is basically the same as this lambda expression:

http.oauth2ResourceServer(t -> t.jwt());

Which is equivalent to this anonymous class:

http.oauth2ResourceServer(new Customizer<OAuth2ResourceServerConfigurer<HttpSecurity>>() {
    @Override
    public void customize(OAuth2ResourceServerConfigurer<HttpSecurity> t) {
        t.jwt();
    }
});

huangapple
  • 本文由 发表于 2020年10月17日 04:01:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64395679.html
匿名

发表评论

匿名网友

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

确定