gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:18:58 25 4
gpt4 key购买 nike

Spring Boot 应用程序在运行时失败,并出现以下错误:

***************************
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 集成开发环境
  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 位服务器虚拟机(版本 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

您可以做几件事:

定义您的 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 来注入(inject)您的参数,但不确定这是否会给您提供用于填充值的控件。

零参数构造函数

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

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

关于找不到类型的 java.util.Date bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58825895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com