jeudi 26 mars 2015

Spring @Scope("prototype") no does not work

I'm new in the world Spring. I'm making some tests and I wish every URL request my list of users is instantiated again. So I would not want a singleton for the duration of the life of my application. This and the code and do not understand where I'm wrong I read many posts to putScope ("prototype") but this does not seem to work:


Controller:



@Controller
public class UtenteController {

@Autowired
private UtenteService service;

@RequestMapping(value="/aggiungiUtente",method=RequestMethod.GET)
public ModelAndView aggiuntiUtente(ModelMap model){

Utente utente=new Utente();
utente.setNome("Diego");
utente.setCognome("pippo");

service.addUtente(utente);

List<Utente>utenti=service.getUtenti();
model.addAttribute("utenti", utenti);

return new ModelAndView("listautenti","modello",model);
}
}


SERVICE:



@Service
@Scope("prototype")
public class UtenteService {

@Autowired
private UtenteDao dao;

public void addUtente(Utente utente){

dao.addUtente(utente);
}

public List<Utente>getUtenti(){

return dao.getUtenti();
}
}


REPOSITORY



@Repository
public class UtenteDao {


public List<Utente>listUtenti=new ArrayList<Utente>();

public void addUtente(Utente utente){

listUtenti.add(utente);
}
public List<Utente> getUtenti(){
return this.listUtenti;
}
}


JSP



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:out value="${nome }"></c:out>
</body>


Even changing the browser to each request my list continues to increase; (I wish that a new instance of List Thank you for your help


Aucun commentaire:

Enregistrer un commentaire