Oscar,
Thanks for the input however I am not weighing the merits of Constructor
based VS. Setter based Spring DI.
The problem here appears to be that Spring beans get initialized BEFORE
Stripersist initializes itself... or at least that is how it appears...
and as such invocations to Stripersist after bean creation / DI will
throw errors regardless of whether I am doing Constructor vs. Setter
based injection... unless I am missing something?
If I did miss your point what do you propose as a solution?
--Nikolaos
Oscar Westra van Holthe - Kind wrote:
On 26-05-2010 at 03:16, Nikolaos Giannopoulos wrote:
So we have the following code excerpt:
@Service
public class ModalityServiceImpl implements ModalityService {
@Autowired
private ModalityDao modalityDaoImpl;
public ModalityServiceImpl() {
this.initService();
}
private void initAfter() {
List<Modality> modalityList = this.modalityDaoImpl.findAll();
// ** NPE ** - this.modalityDaoImpl ** IS NULL **
this.modalityCache = new ModalityCache();
this.modalityCache.init(modalityList);
}
However, the above results in a NullPointerException at the line marked
with ** NPE ** because this.modalityDaoImpl is NULL which clearly
indicates that Spring has not completed the Autowiring and we are trying
to invoke a method on.
I may be going against dogma here, but I prefer all classes to be in a valid
state all the time. So no Spring injection into fields: after construction
the object is in an invalid state until Spring completes the injection.
Instead, I let Spring do Constructor injection.
CON: I need to set the field myself (but I can make it final)
PRO: The constructor can do the initialization
Oscar
------------------------------------------------------------------------------
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users