When I use the @Transactional annotation the Autowired variables become null

Asked

Viewed 94 times

2

When I use the annotation @Transactional in a method annotated with @RequestMapping all variables annotated with @Autowired are getting null.

Has anyone ever been 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);
    }

}
  • In your file application.properties, try adding this property and see if it works: spring.aop.proxy-target-class=false.

  • all right hkotsubo, I tried here and it didn’t work :/

1 answer

0

solved!!!

That is the mistake

@RequestMapping(method=RequestMethod.GET)
private

Browser other questions tagged

You are not signed in. Login or sign up in order to post.