有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么这个构造函数参数不起作用

我用下面的逻辑创建了一个新类

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参数构造函数,那么在以后的阶段将抛出null指针异常

我们该如何应对

有人能帮忙吗


共 (1) 个答案

  1. # 1 楼答案

    首先,必须创建EnhancedBigtableStubSettings类的bean。您可以在applicationContext中定义它。xml或@Configuration类,然后用@Autowired注释构造函数

    <bean name="beanName" class="foo.EnhancedBigtableStubSettings"/>
    
    @Bean(name = "beanName")
    public EnhancedBigtableStubSettings getEnhancedBigtableStubSettings() {
        //return object somehow
        return new EnhancedBigtableStubSettings();
    }