【发布时间】:2022-01-04 22:40:06
【问题描述】:
我创建了一个具有以下逻辑的新类。
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
import com.google.protobuf.ByteString;
import java.util.List;
import java.util.logging.Logger;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.threeten.bp.Duration;
@Component
@RefreshScope
public class PrimeChannel {
private final EnhancedBigtableStubSettings enhancedBigtableStubSettings;
private final List<String> tableIds;
public PrimeChannel(EnhancedBigtableStubSettings enhancedBigtableStubSettings, List<String> tableIds) {
this.enhancedBigtableStubSettings = enhancedBigtableStubSettings;
this.tableIds = tableIds;
}
}
运行时出现以下错误
Parameter 0 of constructor in myPackage.PrimeChannel required a bean of type 'com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings' that could not be found.
Action:
Consider defining a bean of type 'com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings' in your configuration.
我将 EnhancedBigtableStubSettings 对象作为第一个参数传递,那么为什么不考虑它。
如果我通过将 EnhancedBigtableStubSettings 初始化为 null 来尝试使用 0 参数构造函数,则会在后期抛出空指针异常。
如何处理?
有人可以帮忙吗?
【问题讨论】:
-
什么不清楚?您的 Spring 上下文没有构造函数需要的 bean。
-
"我将 EnhancedBigtableStubSettings 对象作为第一个参数传递,那么为什么不考虑它。"你在代码中的某个地方调用
new PrimeChannel()吗?如果是这样,你应该展示它。我认为您没有明确地这样做,而是使用依赖注入。 -
我无法为 EnhancedBigtableStubSettings 创建 bean,因为它具有私有访问权限。实际上我正在尝试将this 逻辑实现为一个单独的类。在此示例中未提及导入,但我可以从
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;导入 -
您链接到的示例使用构建器来构建
EnhancedBigtableStubSettings。将其放入@Bean方法中以创建该特定类型的实例。 -
我尝试了这两种方法 1)
@Bean private EnhancedBigtableStubSettings.Builder settingsTemplate(){ return new EnhancedBigtableStubSettings.Builder(); }我得到了错误'Builder()' has private access in 'com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings.Builder'2)@Bean private EnhancedBigtableStubSettings settingsTemplate(){ return EnhancedBigtableStubSettings.newBuilder().build(); }这没有抛出任何错误,但是我怎样才能在构造函数中新建构建器数据?
标签: java spring-boot google-cloud-platform constructor google-cloud-bigtable