Quando uso a anotação @Transactional as variáveis @Autowired ficam null
22/08/2018 09:08
0
E ai pessoal tudo bem?

Quando uso a anotação @Transactional em um método anotado com @RequestMapping todas as variáveis dentro da classe anotadas com @Autowired estão ficando null.

Fiz algumas pesquisas e achei uma possível solução no arquivo application.properties adicionar a propriedade  spring.aop.proxy-target-class=false mais não funcionou.

Alguém já passou por isso? Poderiam me dar algum rumo para tentar resolver o problema?



Segue a classe que estou tento o problema e a classe dao.
package au.com.petsonme.admin.controller;
import java.util.Arrays;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import au.com.petsonme.admin.controller.forms.ChargingValuesForm;
import au.com.petsonme.dao.ChargingValuesDao;
import au.com.petsonme.model.ChargingValues;
import au.com.petsonme.util.PagesLocationsAdmin;
import au.com.petsonme.util.messages.MessageType;
import au.com.petsonme.util.messages.MessagesHtmlsAndToasts;
import au.com.petsonme.validation.ChargingValuesValidation;
@Controller
@RequestMapping("/admin/system/charging-values")
public class AdminSystemChargingValuesController {

@Autowired
private ChargingValuesDao valueDao;

@Autowired
private ApplicationContext context;

@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(propagation=Propagation.REQUIRED)
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);
}

}


package au.com.petsonme.dao;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import au.com.petsonme.model.ChargingValues;
@Repository
public class ChargingValuesDao {

@PersistenceContext //inject manager by spring
    private EntityManager manager;
    
    private DAO<ChargingValues> dao;
@PostConstruct
void init() {
this.dao = new DAO<ChargingValues>(this.manager, ChargingValues.class);
}

public EntityManager getManager() {
return this.manager;
}

public void insert(ChargingValues o) {
o.setDateTimeInsertion(Calendar.getInstance());
dao.insert(o);
}
public ChargingValues searchById(Integer id) {
return dao.searchById(id);
}

public void update(ChargingValues o) {
dao.update(o);
}

public void delete(ChargingValues o) {
dao.delete(o);
}
public int countAll() {
return dao.countAll();
}

public List<ChargingValues> listAll() {
return dao.listAll();
}

public ChargingValues searchActiveValue() {
ChargingValues value = null;

try {
value = manager.createQuery("SELECT c from ChargingValues c "
+ "where c.dateTimeInactivated is null",ChargingValues.class)
.getSingleResult();
} catch (Exception e) {
// TODO: handle exception
}


return value;
}
}

Tags: springMVC, maven, @Transactional


0
resolvido!!!

Esse é o erro

? @RequestMapping(method=RequestMethod.GET)
private
 
29/08/2018 09:40


0
Acredito que seria Interessante Ter no Titulo do Post apos solução da duvida (RESOLVIDO).
15/09/2018 22:21



Ainda não faz parte da comunidade???

Para se registrar, clique aqui.

Podcast da itexto



Aprenda Groovy e Grails, Spring e mais com a Formação itexto!

Livro de Spring


/dev/All

Os melhores blogs de TI
em um único lugar!

 
Spring Brasil é mantido por itexto Consultoria.
Em caso de problemas contacte Henrique Lobo Weissmann (Kico) por e-mail: kico@itexto.com.br
Todo o conteúdo presente neste site adota o Creative Commons como licença padrão.