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

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

当前回答

实际上,您应该使用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);
    }
}

其他回答

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

下面的一种方法可以奏效:

您正在使用@Autowired的类不是Bean(我确信您可能在某处使用过new())。 在SpringConfig类中,你没有提到Spring应该寻找的包@Component(我说的是@ComponentScan(basePackages“here”))

如果以上两个不工作....开始放入System.out.println(),并找出出错的地方。

这里没有提到的,在本文的“执行顺序”一段中有描述。

在“学习”到必须用@Component或派生的@Service或@Repository(我猜还有更多)来注释一个类,以自动装配它们内部的其他组件之后,我突然发现这些其他组件在父组件的构造函数中仍然是空的。

使用@PostConstruct解决了这个问题:

@SpringBootApplication
public class Application {
    @Autowired MyComponent comp;
}

and:

@Component
public class MyComponent {
    @Autowired ComponentDAO dao;

    public MyComponent() {
        // dao is null here
    }

    @PostConstruct
    public void init() {
        // dao is initialized here
    }
}

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

另一个解决方案是调用 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(这) 到MileageFeeCalculator构造函数,如下所示:

@Service
public class MileageFeeCalculator {

    @Autowired
    private MileageRateService rateService; // <--- will be autowired when constructor is called

    public MileageFeeCalculator() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this)
    }

    public float mileageCharge(final int miles) {
        return (miles * rateService.ratePerMile()); 
    }
}