【问题标题】:Java-spring error: Actual and formal argument differ in lengthJava-spring 错误:实际参数和形式参数的长度不同
【发布时间】:2016-02-04 17:18:35
【问题描述】:

我收到以下错误:

错误:类 CommonCtx 中的构造函数 Commonctx 不能应用于 给定类型; 必需:org.springframework.core.env.Environment 发现:无参数原因:实际参数列表和形式参数列表不同 长度。

正在使用的代码:

@Component
@PropertySource("file:${input.file.loc}")
public class CommonCtx implements IContext {

    private String tempDir;

    @Autowired
    public CommonCtx(Environment inputProperties) {

        tempDir = inputProperties.getProperty("temp.dir");
...
}

@Component
@Conditional(cond1.class)
@PropertySource("file:${input.file.loc}")
public class NewCCtx extends CommonCtx implements NewCContext{

    private String productName;

    /**
     * @param inputProperties
     */
    @Autowired
    public NewCCtx(Environment inputProperties) {
        this.productName = inputProperties.getProperty("product.name");
    }

【问题讨论】:

    标签: java spring


    【解决方案1】:

    在这个构造函数中:

    public NewCCtx(Environment inputProperties) {
        this.productName = inputProperties.getProperty("product.name");
    }
    

    您应该使用正确的参数显式调用超级构造函数 (CommonCtx):

    public NewCCtx(Environment inputProperties) {
        super(inputProperties);
        this.productName = inputProperties.getProperty("product.name");
    }
    

    这是必需的,因为您的父类没有零参数构造函数。

    【讨论】:

    • 非常感谢您的帮助!
    • 欢迎您,乐于助人。 :)
    猜你喜欢
    • 2015-07-25
    • 2014-05-13
    • 2016-05-31
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    相关资源
    最近更新 更多