You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring项目@Autowired注入Repository仍为null空指针异常如何解决

问题根因

你遇到的空指针根本原因是:BatchRequest 作为接口请求的反序列化载体,是Spring MVC通过Jackson直接new出来的对象,不由Spring IoC容器托管,所以你加在它里面的@Autowired注解完全不会生效,TypeRepository自然就是null。
请求类(DTO/Request)本身就不该注入任何Spring Bean,这是架构职责划分的问题,请求类的作用只应该是承载接口传参,不应该包含业务逻辑(查询数据库属于业务逻辑范畴)。

修正方案

你只需要调整代码结构,把查询Type的逻辑挪到Service层即可:

步骤1:简化BatchRequest类,去掉所有业务逻辑和Bean注入

package org.example.BeerMachine.data.payloads.request;

import javax.validation.constraints.NotNull;

public class BatchRequest {
    @NotNull
    private Integer amount;

    @NotNull
    private Integer type_id;

    public Integer getAmount() {
        return amount;
    }

    public Integer getType_id() {
        return type_id;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public void setType_id(Integer type_id) {
        this.type_id = type_id;
    }
}

注意:这里不需要加@Component注解,请求类不需要被Spring托管

步骤2:修改BatchServiceImpl,注入TypeRepository,把查询Type的逻辑移到这里

package org.example.BeerMachine.service;

import org.example.BeerMachine.data.models.Batch;
import org.example.BeerMachine.data.models.Type;
import org.example.BeerMachine.data.payloads.request.BatchRequest;
import org.example.BeerMachine.data.payloads.response.MessageResponse;
import org.example.BeerMachine.data.repository.BatchRepository;
import org.example.BeerMachine.data.repository.TypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.Optional;

@Service
public class BatchServiceImpl implements BatchService {
    @Autowired
    private BatchRepository batchRepository;
    // 注入TypeRepository
    @Autowired
    private TypeRepository typeRepository;

    @Override
    public MessageResponse createBatch(BatchRequest batchRequest) {
        // 直接用type_id查询对应的Type,不用查全表遍历,效率更高
        Optional<Type> typeOpt = typeRepository.findById(batchRequest.getType_id());
        if (typeOpt.isEmpty()) {
            // 传入的type_id不存在,直接抛异常返回错误提示
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "对应type id不存在");
        }
        Batch newBatch = new Batch();
        newBatch.setAmount(batchRequest.getAmount());
        newBatch.setType(typeOpt.get());
        batchRepository.save(newBatch);
        return new MessageResponse("New Batch created successfully");
    }
}

你之前写的遍历全表查type的逻辑完全没必要,JpaRepository自带的findById方法直接就能根据主键查对应记录,性能高很多。

步骤3:TypeRepository可以去掉你额外加的findAll方法

JpaRepository本身已经内置了findAll方法,你不需要重复定义:

package org.example.BeerMachine.data.repository;

import org.example.BeerMachine.data.models.Type;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TypeRepository extends JpaRepository<Type, Integer> {
}
验证

改完之后你再用之前的Python脚本调用接口即可正常创建batch,空指针问题会完全解决。

内容的提问来源于stack exchange,提问作者Oliver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 09:06:03