mighty_mike
mighty_mike

Reputation: 1090

java.util.Date bean of type could not be found

Spring Boot Application is failing on runtime with the following error:

***************************
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.

I am using:

  1. Spring Boot (v1.5.8.RELEASE)
  2. STS4 IDE
  3. Java 8
  4. Linux Ubuntu 18

user@host:~$ java -version
openjdk version "1.8.0_222" OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

I reinstalled Java completely, the error is still the same... Problematic Class:

Problematic Class is located in the domain model:

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;
    }

}

Upvotes: 0

Views: 336

Answers (2)

locus2k
locus2k

Reputation: 2935

The issue is you class in question has parameters for its constructor but you have no beans defined for your parameters

You can do a couple things:

Define your beans

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

}

This will create the necessary beans to inject into your parameters, but not sure if this will give you the control you want to populate the values with.

Zero Arg Constructor

The other way would be create a zero arg constructor then have a method to set what you need:

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

  ...
}

Then were you need it:

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

Upvotes: 1

Revanth Chetluru
Revanth Chetluru

Reputation: 3

It looks like you might have not created a java.Util.date bean. If so, follow the steps here for the right way of injecting the Date bean without errors. If this is not the case, please add your bean configuration file (or a maven pom.xmlm or equivalent) to help understand the issue.

Upvotes: 0

Related Questions