注意:这是一个常见问题的规范答案。

我有一个Spring @Service类(MileageFeeCalculator),它有一个@Autowired字段(rateService),但当我尝试使用它时,该字段为空。日志显示正在创建MileageFeeCalculator bean和MileageRateService bean,但是每当我试图在服务bean上调用mileageCharge方法时,都会得到一个NullPointerException。为什么Spring没有自动装配该字段?

控制器类:

@Controller
public class MileageFeeController {    
    @RequestMapping("/mileage/{miles}")
    @ResponseBody
    public float mileageFee(@PathVariable int miles) {
        MileageFeeCalculator calc = new MileageFeeCalculator();
        return calc.mileageCharge(miles);
    }
}

服务类:

@Service
public class MileageFeeCalculator {

    @Autowired
    private MileageRateService rateService; // <--- should be autowired, is null

    public float mileageCharge(final int miles) {
        return (miles * rateService.ratePerMile()); // <--- throws NPE
    }
}

服务bean,应该在MileageFeeCalculator自动连接,但它不是:

@Service
public class MileageRateService {
    public float ratePerMile() {
        return 0.565f;
    }
}

当我尝试GET /mileage/3时,我得到了这个异常:

java.lang.NullPointerException: null
    at com.chrylis.example.spring_autowired_npe.MileageFeeCalculator.mileageCharge(MileageFeeCalculator.java:13)
    at com.chrylis.example.spring_autowired_npe.MileageFeeController.mileageFee(MileageFeeController.java:14)
    ...

当前回答

如果这发生在测试类中,请确保您没有忘记注释类。

例如,在Spring Boot中:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTests {
    ....

一段时间过去了……

Spring Boot继续发展。如果您使用正确的JUnit版本,则不再需要使用@RunWith。

要让@SpringBootTest单独工作,您需要使用JUnit5中的@Test而不是JUnit4。

//import org.junit.Test; // JUnit4
import org.junit.jupiter.api.Test; // JUnit5

@SpringBootTest
public class MyTests {
    ....

如果配置错误,测试将被编译,但是@Autowired和@Value字段(例如)将为空。由于Spring Boot的操作方式很神奇,因此可以直接调试此故障的方法很少。

其他回答

还要注意,如果出于某种原因,您将@Service中的方法作为final,那么您将从中访问的自动连接bean将始终为空。

如果你不是在编写web应用程序,确保你的类中@Autowiring是一个spring bean。通常,spring容器不会知道我们可能认为是spring bean的类。我们必须告诉Spring容器关于Spring类的信息。

这可以通过在appln- context中配置来实现,或者更好的方法是将类注释为@Component,并且请不要使用new操作符创建注释类。 确保从Appln-context中获取它,如下所示。

@Component
public class MyDemo {


    @Autowired
    private MyService  myService; 

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            System.out.println("test");
            ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
            System.out.println("ctx>>"+ctx);

            Customer c1=null;
            MyDemo myDemo=ctx.getBean(MyDemo.class);
            System.out.println(myDemo);
            myDemo.callService(ctx);


    }

    public void callService(ApplicationContext ctx) {
        // TODO Auto-generated method stub
        System.out.println("---callService---");
        System.out.println(myService);
        myService.callMydao();

    }

}

当我还不太适应IoC世界的生活时,我也遇到过同样的问题。我的一个bean的@Autowired字段在运行时为空。

根本原因是,我没有使用Spring IoC容器维护的自动创建的bean(它的@Autowired字段确实被正确地注入了),而是更新了该bean类型的我自己的实例并使用它。当然,这个的@Autowired字段是null,因为Spring没有机会注入它。

如果您正在使用私有方法,它将为空,请尝试在控制器中将private更改为public。

这是给NullPointerException MileageFeeCalculator calc = new MileageFeeCalculator()的罪魁祸首;我们使用Spring -不需要手动创建对象。对象的创建将由IoC容器负责。