【问题标题】:Encountered error "Consider defining a bean of type 'java.util.concurrent.atomic.AtomicReference' in your configuration"遇到错误“考虑在您的配置中定义一个 'java.util.concurrent.atomic.AtomicReference' 类型的 bean”
【发布时间】:2021-05-30 04:45:46
【问题描述】:

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

注入点有如下注解:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

行动:

考虑定义一个 bean 类型 'java.util.concurrent.atomic.AtomicReference' 在您的配置中。

下面是代码。

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 应用程序将启动而没有任何错误。如果包含,则启动失败并出现不满足的依赖错误。

有人可以帮忙看看配置有什么问题吗?

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    您希望 Spring 为 QueueAndPublishService 的实现创建一个类 QueueAndPublishServiceImpl 的实例。该实例需要注入 AtomicReference&lt;List&lt;QueueAndPublish&gt;&gt; 类型的构造函数参数。

    但您显然没有定义任何该类型的 Spring bean(Bean、组件、服务...)。

    编辑:

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

    在这里,您定义一个构造函数参数以具有AtomicReference&lt;List&lt;QueueAndPublish&gt;&gt;,甚至使用@Qualifier 指定它。所以需要给这个类的Spring bean提供这个限定符,否则Spring无法将其注入到构造函数调用中。

    【讨论】:

    • 感谢您的回复。您能否详细说明您的答案,因为我无法理解您在这里的确切意思?
    【解决方案2】:

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

    表示“类似”将其添加到您的Sampleapplayerv1Application

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-27
      • 2018-06-22
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2019-02-14
      • 2018-12-21
      相关资源
      最近更新 更多