When I use the annotation @Transactional
in a method annotated with @RequestMapping
all variables annotated with @Autowired
are getting null
.
Has anyone ever gone through this? Could you give me some direction to try to solve the problem?
@Controller
@RequestMapping("/admin/system/charging-values")
public class AdminSystemChargingValuesController {
@Autowired
private ApplicationContext context;
@Autowired
private ChargingValuesDao valueDao;
@Autowired
private MessagesHtmlsAndToasts messagesHtmlsAndToasts;
@Autowired
private ChargingValuesValidation changingValidation;
@RequestMapping(method=RequestMethod.GET)
private ModelAndView page(ChargingValuesForm chargingValuesForm) {
return loadPage(chargingValuesForm);
}
private ModelAndView loadPage(ChargingValuesForm chargingValuesForm){
ModelAndView modelAndView = new ModelAndView(PagesLocationsAdmin.getPageSystemChargingValuesUrl().toModelAndViewPath());
ChargingValues value = valueDao.searchActiveValue();
if(value != null)
chargingValuesForm = new ChargingValuesForm(value);
modelAndView.addObject("chargingValuesForm", chargingValuesForm);
return modelAndView;
}
@RequestMapping(value="/save", method=RequestMethod.POST)
@Transactional
public ModelAndView save(@ModelAttribute("chargingValuesForm") ChargingValuesForm chargingValuesForm, BindingResult result,
RedirectAttributes redirectAttributes, HttpServletRequest request)
{
ModelAndView modelAndView = new ModelAndView(PagesLocationsAdmin.getPageSystemChargingValuesUrl().toModelAndViewPath());
changingValidation.validate(chargingValuesForm, result);
if(result.hasErrors()){
messagesHtmlsAndToasts.init(context, redirectAttributes, request, modelAndView);
modelAndView.addObject("chargingValuesForm", chargingValuesForm);
return modelAndView;
}
saveChargingValues(chargingValuesForm, modelAndView);
return modelAndView;
}
private void saveChargingValues(ChargingValuesForm chargingValuesForm, ModelAndView modelAndView) {
//disable the last register active on DB
ChargingValues value = valueDao.searchActiveValue();
if(value != null) {
value.setDateTimeInactivated(Calendar.getInstance());
valueDao.update(value);
}
value = null;
//Create new ChargingValues on DB
value = new ChargingValues();
value.setMinPricDogWalking(chargingValuesForm.getMinPricDogWalking());
value.setMinPricPetGrooming(chargingValuesForm.getMinPricPetGrooming());
value.setMinPricDogBoarding(chargingValuesForm.getMinPricDogBoarding());
value.setMinPricPetSitting(chargingValuesForm.getMinPricPetSitting());
value.setPercDogWalking(chargingValuesForm.getPercDogWalking());
value.setPercPetGrooming(chargingValuesForm.getPercPetGrooming());
value.setPercDogBoarding(chargingValuesForm.getPercDogBoarding());
value.setPercPetSitting(chargingValuesForm.getPercPetSitting());
valueDao.insert(value);
messagesHtmlsAndToasts.toHtmlMessages(MessageType.SUCCESS, Arrays.asList("form.submit.success"));
chargingValuesForm = new ChargingValuesForm(value);
modelAndView.addObject("chargingValuesForm", chargingValuesForm);
}
}