【问题标题】:How set an Builder object from constructor如何从构造函数设置 Builder 对象
【发布时间】:2022-01-05 23:23:16
【问题描述】:

我想使用构造函数将现有(空构建器)对象替换为较新的对象。 我的代码是这样的

public class BigtableChannelPrimer implements ChannelPrimer {
    private List<String> appName;
    @Bean
    private EnhancedBigtableStubSettings settingsTemplate(){
        return EnhancedBigtableStubSettings.newBuilder().build();
    }
    @Bean
    @RefreshScope
    BigtableChannelPrimer create() {
        EnhancedBigtableStubSettings.Builder builder =
            EnhancedBigtableStubSettings.newBuilder()
                .setProjectId(projectId)
               .build());

        // Disable retries for priming request
        builder
            .readRowSettings()
            .setRetrySettings(
                builder
                    .readRowSettings()
                    .getRetrySettings()
                    .toBuilder()
                    .setMaxAttempts(1)                  
                    .build());
        return new BigtableChannelPrimer(builder.build(), tableIds);
    }
public BigtableChannelPrimer(
        EnhancedBigtableStubSettings settingsTemplate,
        List<String> appName) {
        this.settingsTemplate(). ======================= 
        this.appName= appName;
    }
}

在这个构造函数中如何设置this.settingsTemplate().这样的东西。

我在同一个类中还有其他方法需要这个构建器。如何做到这一点?

在编译时出现以下错误

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  settingsTemplate defined in class path resource [BigtableChannelPrimer .class]

这有什么问题?

【问题讨论】:

  • 为什么要进行此更改?一般来说,构建器更适合像这样的复杂对象。
  • 其实我正在尝试实现this。我添加了相同的代码,因为我在 Parameter 0 of constructor in ourPackage.BigtableChannelPrimer required a bean of type 'com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings' that could not be found 这样的构造函数中遇到了错误,所以我试图创建一个 bean 并像这样实现

标签: java spring-boot google-cloud-platform lombok google-cloud-bigtable


【解决方案1】:

实现自己的BigtableChanelPrimer的用例是什么?

如果您想设置频道启动,您可以使用BigtableDataSettings:

BigtableDataSettings settings = BigtableDataSettings.newBuilder()
BigtableDataSettings.setProjectId(bigtable_project_id)
    .setInstanceId(bigtable_instance_id)
    .setPrimingTableIds(tableIds)
    .setRefreshingChannel(true);

BigtableDataClient client = BigtableDataClient.create(settings.build());

您可以在此处找到文档:https://javadoc.io/doc/com.google.cloud/google-cloud-bigtable/latest/com/google/cloud/bigtable/data/v2/BigtableDataSettings.Builder.html 在 setRefreshingChannel 和 setPrimingTableIds 下。

【讨论】:

    猜你喜欢
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2015-07-24
    相关资源
    最近更新 更多