Java-spring错误: Actual and formal argument differ in length

标签 java spring

我收到以下错误:

Error:constructor Commonctx in class CommonCtx cant be applied to given types; required: org.springframework.core.env.Environment found:no arguments reason: actual and formal argument lists differ in length.

正在使用的代码:

@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");
    }

最佳答案

在此构造函数中:

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

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

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

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

关于Java-spring错误: Actual and formal argument differ in length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33518021/

相关文章:

java - 复杂的 cron 表达式来运行quartz作业?

java - 调用惰性初始化字段的 getter 后出现 org.hibernate.lazyinitializationException

java - 工具栏不会显示在 Activity 中

java - HikariCP可以显示sql吗?

java - 枚举列表与 boolean 类

java - 创建 java RESTful 应用程序

java - CORBA 不清楚的东西

java - 如何解释命令 "od -b file"的结果

java - Spring Roo 将 database.properties 放在 META-INF/spring 中有充分的理由吗?

java - JdbcTemplate 类型未定义方法 queryForInt(String)