Spring Boot Application在运行时失败,出现以下错误:

***************************
APPLICATION FAILED TO START
***************************
Description:

Parameter 0 of constructor in com.bla.Application.Domain.SomeJavaClass required a bean of type 'java.util.Date' that could not be found.

Action:

Consider defining a bean of type 'java.util.Date' in your configuration.


我在用:


Spring Boot(v1.5.8.RELEASE)
STS4 IDE
Java 8
Linux Ubuntu 18


user @ host:〜$ java -version
    openjdk版本“ 1.8.0_222”
    OpenJDK运行时环境(内部版本1.8.0_222-8u222-b10-1ubuntu1〜18.04.1-b10)
    OpenJDK 64位服务器VM(内部版本25.222-b10,混合模式)

我完全重新安装了Java,错误仍然相同...
有问题的课程:

问题类位于域模型中:

package com.vip.SmartScheduler.Domain;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.vip.SmartScheduler.Enum.PeriodUnit;

@Component
public class PeriodContainer {

    private Date periodBeginTimestamp;
    private List<UserStats> agentsWithStatistics;
    private PeriodUnit periodUnit;

    public PeriodContainer(Date periodBeginTimestamp, List<UserStats> agents, PeriodUnit periodUnit){
        this.periodBeginTimestamp = periodBeginTimestamp;
        this.agentsWithStatistics = agents;
        this.periodUnit = periodUnit;
    }

    public Date getPeriodBeginTimestamp() {
        return periodBeginTimestamp;
    }

    public void setPeriodBeginTimestamp(Date periodBeginTimestamp) {
        this.periodBeginTimestamp = periodBeginTimestamp;
    }

    public List<UserStats> getAgentsWithStatistics() {
        return agentsWithStatistics;
    }

    public void setAgentsWithStatistics(List<UserStats> agents) {
        this.agentsWithStatistics = agents;
    }

    public PeriodUnit getPeriodUnit() {
        return periodUnit;
    }

    public void setPeriodUnit(PeriodUnit periodUnit) {
        this.periodUnit = periodUnit;
    }

}

最佳答案

问题是您所讨论的类的构造函数具有参数,但没有为参数定义bean

您可以做几件事:

定义你的豆子

@Configuration
public class MyBeans {

  @Bean
  public Date periodBeginTimestamp() {
    return new Date();
  }

  @Bean
  public List<UserStats> agents() {
    return new ArrayList<UserStats>();
  }

  @Bean
  public PeriodUnit periodUnit() {
    return new PeriodUnit();
  }

}


这将创建必要的bean,以注入到您的参数中,但是不确定是否会为您提供要用来填充值的控件。

零精氨酸构造函数

另一种方法是创建一个零arg构造函数,然后有一个方法来设置所需的内容:

@Component
public class PeriodContainer {

  private Date periodBeginTimestamp;
  private List<UserStats> agentsWithStatistics;
  private PeriodUnit periodUnit;

  public PeriodContainer(){}

  public void setValues(Date periodBeginTimestamp, List<UserStats> agents, PeriodUnit periodUnit){
    this.periodBeginTimestamp = periodBeginTimestamp;
    this.agentsWithStatistics = agents;
    this.periodUnit = periodUnit;
  }

  ...
}


那您需要吗:

@Service
public class MyClass {
  @Autowire
  private PeriodContainer container;

  @PostConstruct
  private void init() {
    Date periodBeginTimestamp = getTimeStamp();
    List<UserStats> agents = getUserStatsList();
    PeriodUnit periodUnit = getPeriodUnit();
    container.setValues(periodBeginTimestamp, agents, periodUnit);
  }
}

10-08 14:36