Correctness2 - SpotBugsExtensionForSpringFrameWork/CS5098 GitHub Wiki
Stack Overflow - Why is my Spring @Autowired field null?
@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
}
}
----------------------------------------------------
@Service
public class MileageRateService {
public float ratePerMile() {
return 0.565f;
}
}