找不到类型的java.util.Date bean

时间:2019-11-12 20:02:49

标签: java spring

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.

我正在使用:

  1. Spring Boot(v1.5.8.RELEASE)
  2. STS4 IDE
  3. Java 8
  4. 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;
    }

}

2 个答案:

答案 0 :(得分:1)

问题是您所讨论的类的构造函数具有参数,但没有为参数定义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); 
  }
}

答案 1 :(得分:0)

您似乎尚未创建if let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen bean。如果是这样,请遵循步骤here,以正确方式注入Date bean而不会出现错误。如果不是这种情况,请添加您的bean配置文件(或maven pom.xmlm或等效文件)以帮助理解该问题。