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

-- 
   ,-_
  /() ) Oscar Westra van Holthe - Kind      http://www.xs4all.nl/~kindop/
 (__ (
=/  ()  QED - Quite Easily Done

Attachment: signature.asc
Description: Digital signature

------------------------------------------------------------------------------

_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to