Builder在Builder中具有私有访问权限

时间:2018-03-06 08:55:14

标签: java builder

这个Java代码片段出了什么问题?

ClientRegistration registration = new ClientRegistration.Builder(properties.getClientId())
        .authorizationUri(properties.getAuthorizationUri())
        .build();

我收到以下错误消息但不明白:

error: Builder(String) has private access in Builder
        ClientRegistration registration = new ClientRegistration.Builder(properties.getClientId())
                                          ^

据我了解,此错误通常来自以下代码:

ClientRegistration registration = new ClientRegistration().Builder(properties.getClientId())

以上代码是解决问题的正确方法。但现在我反而遇到了这个问题。

任何想法?

1 个答案:

答案 0 :(得分:2)

无法使用ClientRegistration.Builder关键字创建new,因为其构造函数是私有的。

你应该是:

ClientRegistration.Builder builder = ClientRegistration.withRegistrationId(registrationIdAsString)

这会为您创建一个新的ClientRegistration.Builder

之后,您可以在ClientRegistration.Builder中设置字段,然后致电.build()以获取ClientRegistration。像那样:

ClientRegistration registration = builder.authorizationUri(properties.getAuthorizationUri())
    .build();

查看构建器的JavaDocs以查看可以设置的属性: https://docs.spring.io/spring-security/site/docs/5.0.x/api/org/springframework/security/oauth2/client/registration/ClientRegistration.Builder.html

或者在一行中,就像你做的那样:

ClientRegistration registration = ClientRegistration.withRegistrationId(properties.getClientId())
    .authorizationUri(properties.getAuthorizationUri())
    .build();