【问题标题】:Execution failure when using @Autowired annotation in Spring app在 Spring 应用程序中使用 @Autowired 注解时执行失败
【发布时间】:2022-09-27 16:53:51
【问题描述】:

我有两个班级,ErpDataRepositoryRequestProcessingService。我想自动装配 \"Request\" 类,并在下面的 \"ErpDataRepo\" 类中这样做。

但是,当我添加该行时,我的 Spring 应用程序崩溃并出现以下命令错误:

以非零退出值 1 失败

我的理解是它崩溃了,因为 Spring 找不到我定义为组件或服务的类。但是,我已经在RequestProcessingService 类中添加了@Service 注释,Spring 仍然无法检测到它。我还在我的配置类中检查了@ComponentScan,它应该能够找到我为它设置的类。

关于为什么会发生此错误的任何想法?

ErpDataRepository班级

@Slf4j
@Repository
public class ErpDataRepository extends BaseRepository implements IAccountingObjectTypeRepository {

    private static final int DELETE_ALL_FOR_TYPE_PAGE_LIMIT = 1500;

    protected DynamoDBMapper dynamoDBMapper;
    protected AmazonDynamoDB amazonDynamoDB;

    @Autowired
    private RequestProcessingService requestProcessingService;

    public ErpDataRepository(AmazonDynamoDB amazonDynamoDB, DynamoDBMapper dynamoDBMapper) {
        super(EcpConstants.ERP_DATA_TABLE, EcpConstants.DATA_COMPANY_ID_ERP_ID_INDEX, dynamoDBMapper, amazonDynamoDB);
        this.dynamoDBMapper = dynamoDBMapper;
        this.amazonDynamoDB = amazonDynamoDB;
    }
}

请求处理服务班级

@Service
@CustomLog
public class RequestProcessingService {
    @Autowired
    private ProcessingErrorRepository processingErrorRepository;
    @Autowired
    private ErpDataRepository erpDataRepository;

    public PaginatedDataResponse<ProcessingError> getProcessingErrors(String requestId) throws NotFoundException {
        return getProcessingErrors(requestId, EcpConstants.DEFAULT_NEXT_PAGE);
    }
}

    标签: java spring spring-boot autowired


    【解决方案1】:

    您的代码中存在循环依赖关系,因为您已将 RequestProcessingService 自动连接到 ErpDataRepository 和 RequestProcessingService 中,您自动连接了 ErpDataRepository 所以它看起来像 -

    RequestProcessingService
        |                 ^
        |                 |
    ErpDataRepository     |
        |.                |
        |_________________|
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多