当前位置: 首页 > 知识库问答 >
问题:

遇到错误"考虑在您的配置中定义类型为'java.util.concurrent.atomic.原子参考'的bean"

张宣
2023-03-14

我在启动spring boot应用程序时遇到以下错误。

注入点具有以下注释:

  • @org.springframework.beans.factory.annotation.自动装配(必需=true)

行动:

考虑在您的配置中定义一个类型为“java.util.concurrent.atomic.原子参考”的bean。

下面是代码。

package de.summer.sampleapplayerv1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication(scanBasePackages = {"de.summer.sampleapplayerv1"})
@EnableConfigurationProperties
@EnableJpaRepositories (basePackages ="de.summer.sampleapplayerv1.repository")
@EnableTransactionManagement
public class Sampleapplayerv1Application {

    public static void main(String[] args) {
        SpringApplication.run(Sampleapplayerv1Application.class, args);
    }

}

package de.summer.sampleapplayerv1.service;

import de.summer.sampleapplayerv1.domain.QueueAndPublish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Slf4j
@Service
public class QueueAndPublishServiceImpl implements QueueAndPublishService{

    private final AtomicReference<List<QueueAndPublish>> currentJob;

    public QueueAndPublishServiceImpl(
            @Qualifier("currentJob") AtomicReference<List<QueueAndPublish>> currentJob
    ){
        this.currentJob=currentJob;
    }

    @Override
    public QueueAndPublish getJobStatus(UUID jobId) {
        return (QueueAndPublish) currentJob.get().stream()
                        .filter(j -> j.getJobId()==jobId)
                        .collect(Collectors.toList());
    }

    @Override
    public List<QueueAndPublish> getAllJobStatus() {
        return currentJob.get();
    }

    @Override
    public QueueAndPublish getCategoryDataProcess() {
        List<QueueAndPublish> processList=new ArrayList<QueueAndPublish>();
        QueueAndPublish process=QueueAndPublish.builder()
                                                .jobId(UUID.randomUUID())
                                                .jobName("Name for Job")
                                                .jobStatus("Not Yet Started")
                                                .build();
        Thread t1=new Thread(process.getJobId().toString()){
          @Override
          public void run(){
              log.info("How are you doing");
              process.setJobStatus("Completed");
          }
        };
        t1.start();
        processList.add(process);
        currentJob.set(processList);
        return process;
    }

    @Override
    public QueueAndPublish getCatgeoryDataProcessStatus() {
        return null;
    }
}

package de.summer.sampleapplayerv1.domain;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.UUID;

@Getter
@Setter
@Builder
@Entity
public class QueueAndPublish implements Serializable {

    @Id
    private UUID jobId;
    private String jobName;
    private String jobStatus;
}

如果我删除了构造函数,spring boot应用程序将在没有任何错误的情况下启动。如果包含,则启动失败,并出现不满意的依赖项错误。

有人能帮我解释一下配置有什么问题吗?

共有2个答案

裴楚青
2023-03-14

考虑在您的配置中定义一个类型为“java.util.concurrent.atomic.原子参考”的bean。

意思是“类似于”将此添加到您的SampleApplayerV1应用程序中:

@Bean("currentJob") AtomicReference<List<QueueAndPublish>> currentJob() {
   // or a list implementation of your choice.
   return new AtomicReference<>(new java.util.ArrayList<>()); 
}

董鸣
2023-03-14

您希望Spring创建类QueueAndPublishServiceImpl的实例,以实现QueueAndPublishService。此实例需要类型为AtomicReference的构造函数参数

但是您显然没有定义任何Springbean(bean、组件、服务等等)那种。

编辑:

public QueueAndPublishServiceImpl(
            @Qualifier("currentJob") AtomicReference<List<QueueAndPublish>> currentJob
    ){
        this.currentJob=currentJob;
    }

在这里,您定义了一个构造函数参数,使其具有AtomicReference

 类似资料: