mercredi 1 avril 2015

Mixing prototypes with singletons

I've got some application in Spring, which on every request creates prototype bean X (from factory). This bean X has some DAO singletons e.g. Y, Z. and SomeObject on which I work (set values etc.)



@Scope(value="prototype")
public class X{
@Autowired
private X x;
@Autowired
private Z z;

private SomeObject obj;

public void someMethod(){
obj.setProperty();
}
}


Is it right approach? I mean, in every request Spring container must search for those singleton beans and inject them into prototype, is it effective? Or maybe it's better to create stateless bean and create SomeObject instance in appropriate method, and then pass it in function arguments whenever I want to use it (it's not so comfortable)?


Aucun commentaire:

Enregistrer un commentaire