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

Spring Boot中main()方法使用@Autowired触发NullPointerException的问题与解决

解决方案说明

我已找到解决方案,特此更新。

本问题与《为什么我的Spring @Autowired字段为空?》类似,但我是在main()方法中遇到该问题。解决方案是为Foo添加@Component注解,并通过@PostConstruct方法将其自动注入到Main中。

关于@PostConstruct

@PostConstruct可确保所有Bean(包括当前编辑的Bean类)完全初始化后再执行方法。

核心问题在于手动执行了Foo foo = new Foo(),这样创建的foo不在Spring上下文范围内,其生命周期完全由自己管理。需要通过自动注入将所有对象交由Spring管理。

修改后的Main.java

package org.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class Main {
    @Autowired
    Foo foo;

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

    @PostConstruct
    public void test() {
        foo.message();
    }
}

修改后的Foo.java

package org.example;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class Foo {

    @Autowired
    Bar bar;

    public Foo() {
        log.info("Foo bean created");
    }

    public void message() {
        log.info(bar.getWord());
    }

}

原始问题

我按照许多Spring教程的写法实现,但始终无法成功,不知道哪里出错了,急需帮助解决。

原始Main.java

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);

        Foo foo = new Foo();
        foo.message();
    }
}

原始Foo.java

package org.example;

import org.springframework.beans.factory.annotation.Autowired;

public class Foo {

    @Autowired
    Bar bar;

    public void message() {
        System.out.println(bar.getWord());
    }

}

Bar.java

package org.example;

import org.springframework.stereotype.Component;

@Component
public class Bar {
    String word = "word";

    public String getWord() {return word;}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>untitled</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.5</version>
        </dependency>

    </dependencies>

</project>

运行Main()时,我得到以下异常:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.example.Bar.getWord()" because "this.bar" is null
    at org.example.Foo.message(Foo.java:11)
    at org.example.Main.main(Main.java:14)

所有类都在同一个目录/包下。我已经折腾了两个小时,不知道为什么无法运行,非常感谢任何帮助。

我尝试的操作:

  • 使用@Autowired创建可在其他类中使用的Bean。

我期望的结果:

  • 字符串word被打印到控制台。
  • 我的理解是,带有@Component注解的类可被Spring识别为Bean,通过@Autowired注入即可使用,我满足该条件但未得到预期结果。

实际结果:

  • 抛出NullPointerException

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 15:31:01