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

Spring Boot中@Autowired注入JPA Repository出现NullPointerException

Spring Boot中@Autowired注入静态Repository导致NullPointerException的问题分析与解决

问题描述

我创建了简单的实体类TestModel与JPA Repository接口TestRepository,想要测试save方法:

@Data
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name = "TEST")
public class TestModel {

    @Id
    @NonNull
    @Basic(optional = false)
    @Column(name = "id")
    private String id;

    @NonNull
    @Basic(optional = false)
    @Column(name = "testValue")
    private String testValue;

}

@Repository
public interface TestRepository extends JpaRepository<TestModel, String> {

}

在主启动类DemoApplication中,我通过静态变量使用@Autowired注入TestRepository,调用save方法时触发NullPointerException,指向该注入的Repository。我尝试过ComponentScan、EntityScan、EnabledAutoConfiguration等注解,但均无效。当前使用Spring Boot 2.7.10、Spring Data JPA和Spring Web依赖。请问问题出在哪里?

问题原因与解决办法

核心问题:Spring不支持直接注入静态变量

Spring的依赖注入机制是针对实例对象的,静态变量属于类本身,不属于任何实例,因此Spring容器无法直接对静态变量完成注入操作,这就是触发NullPointerException的根本原因。

正确实现方式

有两种常见解决方案:

方案1:使用实例变量注入(推荐)

将TestRepository改为启动类的实例变量,Spring可正常完成注入:

@SpringBootApplication
public class DemoApplication {

    @Autowired
    private TestRepository testRepository;

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        DemoApplication app = context.getBean(DemoApplication.class);
        app.testSave();
    }

    private void testSave() {
        TestModel model = new TestModel("1", "test");
        testRepository.save(model);
    }
}

方案2:通过实例变量间接赋值给静态变量(仅兼容场景使用)

如果必须使用静态变量,可通过实例变量作为中间层,利用@PostConstruct在实例初始化后给静态变量赋值:

@SpringBootApplication
public class DemoApplication {

    private static TestRepository staticTestRepository;

    @Autowired
    private TestRepository testRepository;

    @PostConstruct
    public void init() {
        staticTestRepository = testRepository;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        TestModel model = new TestModel("1", "test");
        staticTestRepository.save(model);
    }
}

注意:方案2会破坏Spring依赖注入的设计原则,增加代码耦合度,非必要场景不建议使用。

内容的提问来源于stack exchange,提问作者André Ribeiro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 23:33:13