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

我有一个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)
    ...

当前回答

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

其他回答

我认为您错过了指示spring扫描带有注释的类。

您可以在spring应用程序的配置类上使用@ComponentScan("packageToScan")来指示spring进行扫描。

@Service, @Component等注释添加元描述。

Spring只注入这些类的实例,这些类要么创建为bean,要么用注释标记。

在注入之前,spring需要标识带有注释的类,@ComponentScan指示spring查找带有注释的类。当Spring找到@Autowired时,它会搜索相关的bean,并注入所需的实例。

仅仅添加注释,并不能修复或促进依赖注入,Spring需要知道在哪里查找。

实际上,您应该使用JVM托管对象或spring托管对象来调用方法。 在控制器类中的上述代码中,您创建了一个新对象来调用服务类,该服务类具有一个自动连接的对象。

MileageFeeCalculator calc = new MileageFeeCalculator();

所以它不会这样工作。

该解决方案使这个MileageFeeCalculator作为控制器本身的自动连接对象。

像下面这样更改你的Controller类。

@Controller
public class MileageFeeController {

    @Autowired
    MileageFeeCalculator calc;  

    @RequestMapping("/mileage/{miles}")
    @ResponseBody
    public float mileageFee(@PathVariable int miles) {
        return calc.mileageCharge(miles);
    }
}

这只在单元测试的情况下有效。

我的Service类有一个Service注释,它是@autowired另一个组件类。当我测试的组件类是空的。因为对于服务类,我使用new创建对象

如果您正在编写单元测试,请确保您没有使用new object()创建对象。使用injectMock代替。

这解决了我的问题。这里有一个有用的链接

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

简单来说,@Autowired字段为空主要有两个原因

你的班级不是一个春天的豆子。

在其中定义@Autowire注解的类不是spring bean。因此,弹簧不会自动连接成员。

这块地不是豆子。

您在@Autowired字段中指定的层次结构中的类型或类型的bean还没有出现在spring应用程序上下文或注册表中