0
在一个部分的同一个请求中,我得到了NullPointerException,而在其他部分的代码中它没问题。Spring @Autowired bean null null
@Controller
@RequestMapping(value="/event")
public class EventController {
@Autowired
EventValidator eventValidator;
@Autowired
private EventService eventService;
@InitBinder
private void initBinder(WebDataBinder binder) {
System.out.println(eventValidator.toString()); <<—— NULL HERE
binder.setValidator(eventValidator);
}
@RequestMapping(value="/add_event",method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<AjaxJSONResponse> postAddEventForm(@RequestPart("event") Event event, MultipartHttpServletRequest request,BindingResult result) throws MethodArgumentNotValidException, NoSuchMethodException
{
eventValidator.validate(event, result); <<——- OK HERE
//Check validation errors
if (result.hasErrors()) {
throw new MethodArgumentNotValidException(
new MethodParameter(this.getClass().getDeclaredMethod("postAddEventForm", Event.class, MultipartHttpServletRequest.class, BindingResult.class), 0),
result);
}
Boolean inserted = eventService.addEvent(event);
String contextPath = request.getContextPath();
String redirectURL = StringUtils.isEmpty(contextPath)?"/event":contextPath+"/event";
return new ResponseEntity<AjaxJSONResponse>(new AjaxJSONResponse(inserted,"Event Added Successfully",redirectURL), HttpStatus.OK);
}
}
在相同的请求,在initBinder()
功能eventValidator
为NULL,并且在postAddEventForm()
功能eventValidator
握持实例。
请帮忙。 initBinder()
方法
尝试改变initBinder方法的范围公开。 –
谢谢Chirdeep。你能否给我简要的资料,说明为什么我们需要公开。 –