Constructor injection has these three distinct advantages over field/setter
injection:
- always thread safe
- fields can be marked final
- you don't have to keep references to objects you need during construction

Kalle


On Sun, Apr 6, 2014 at 1:08 PM, Sanket Sharma <sanketsha...@gmail.com>wrote:

> I battled with the same issue yesterday - injection happens after the
> constructor has run!
>
>
> On Sun, Apr 6, 2014 at 9:26 PM, John <j...@quivinco.com> wrote:
>
> > problem solved, the Logger was of course not yet injected! Duh. I wish
> the
> > IoC stack dump were more helpful.
> >
> > J
> >   ----- Original Message -----
> >   From: John
> >   To: users@tapestry.apache.org
> >   Sent: Sunday, April 06, 2014 6:35 PM
> >   Subject: NullPointer for service constructor
> >
> >
> >   I'm trying to constuct a service implimentation that receives
> references
> > to a couple of other services that registers the objects into a super
> > class. But I get a null pointer exception.
> >
> >   What am I doing wrong here?
> >
> >
> >   public class AppModule {
> >
> >       public static void bind(ServiceBinder binder) {
> >           // bind low-level services and utilities
> >           binder.bind(CommonsEmailNotificationSender.class); //
> implements
> > NotificationSender to send email
> >           binder.bind(TextMarketerNotificationSender.class); //
> implements
> > NotificationSender to send sms
> >
> >    ...
> >           // bind facades
> >    ...
> >           binder.bind(NotificationSenderFacade.class,
> > DefaultNotificationSenderFacade.class); // combines the
> NotificationSender
> > instances above
> >
> >       }
> >
> >
> >
> >   public interface NotificationSender {
> >
> >       boolean sendNotification(String toUsername, String fromUsername,
> > String replyto, String to, String messageText, boolean admin);
> >
> >       boolean sendNotificationLater(String toUsername, String
> > fromUsername, String replyto, String to, String messageText, boolean
> admin);
> >
> >
> >
> >   public interface NotificationSenderFacade {
> >
> >       NotificationSender getNotificationSender(String classnme);
> >
> >       boolean sendNotification(String toUsername, String fromUsername,
> > List<NotificationRouting>routes, String messageText, boolean admin);
> >
> >       boolean sendNotificationLater(String toUsername, String
> > fromUsername, List<NotificationRouting>routes, String messageText,
> boolean
> > admin);
> >
> >
> >
> >   public abstract class AbstractNotificationSenderFacade implements
> > NotificationSenderFacade {
> >
> >       /**
> >        * The log.
> >        */
> >       @Inject
> >       private Logger log;
> >
> >       private Map<String, NotificationSender> notificationSenders
> >               = new HashMap<String, NotificationSender>();
> >
> >       // put the notification senders into a map during subclass
> > construction
> >       protected void registerNotificationSender(NotificationSender
> > notificationSender) {
> >           String classname =
> notificationSender.getClass().getSimpleName();
> >           notificationSenders.put(classname, notificationSender);
> >           log.info("registered " + classname);
> >       }
> >
> >
> >
> >   @EagerLoad
> >   public class DefaultNotificationSenderFacade extends
> > AbstractNotificationSenderFacade {
> >
> >       /**
> >        * The log.
> >        */
> >       @Inject
> >       private Logger log;
> >
> >       /**
> >        * Create the facade for the given the notification senders.
> >        *
> >        * @param emailNotificationSender
> >        * @param smsNotificationSender
> >        */
> >       public
> > DefaultNotificationSenderFacade(CommonsEmailNotificationSender
> > emailNotificationSender,
> >               TextMarketerNotificationSender smsNotificationSender) {
> >           super();
> >           super.registerNotificationSender(emailNotificationSender);
> >           super.registerNotificationSender(smsNotificationSender);
> >           log.debug("registered notification senders");
> >       }
> >
> >       @Override
> >       public boolean sendNotification(String toUsername, String
> > fromUsername, List<NotificationRouting> routes, String messageText,
> boolean
> > admin) {
> >          boolean success = true;
> >           for (NotificationRouting route : routes) {
> >               if (route.getTo().contains("@")) {
> >                   boolean done =
> > super.getNotificationSender("CommonsEmailNotificationSender")
> >                           .sendNotification(toUsername, fromUsername,
> > route.getReplyto(), route.getTo(), messageText, admin);
> >
> >
> >
> >   AppModule.NotificationSenderFacade Loading class
> > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade.
> >   AppModule.NotificationSenderFacade Marking class
> > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade to be
> > (re-)loaded
> >   AppModule.NotificationSenderFacade BEGIN Analyzing
> > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade
> >   AppModule.NotificationSenderFacade Marking class
> > com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade to be
> > (re-)loaded
> >   AppModule.NotificationSenderFacade BEGIN Analyzing
> > com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade
> >   AppModule.NotificationSenderFacade   END Analyzing
> > com.quivinco.webapps.tbs.utils.AbstractNotificationSenderFacade
> >   AppModule.NotificationSenderFacade   END Analyzing
> > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade
> >   AppModule.CommonsEmailNotificationSender Creating non-proxied instance
> > of service CommonsEmailNotificationSender
> >   AppModule.CommonsEmailNotificationSender Invoking constructor
> > com.quivinco.webapps.tbs.utils.CommonsEmailNotificationSender() (at
> > CommonsEmailNotificationSender.java:23) via
> > com.quivinco.webapps.tbs.services.AppModule.bind(ServiceBinder) (at
> > AppModule.java:75) (for service 'CommonsEmailNotificationSender')
> >   AppModule.TextMarketerNotificationSender Creating non-proxied instance
> > of service TextMarketerNotificationSender
> >   AppModule.TextMarketerNotificationSender Invoking constructor
> > com.quivinco.webapps.tbs.utils.TextMarketerNotificationSender() (at
> > TextMarketerNotificationSender.java:16) via
> > com.quivinco.webapps.tbs.services.AppModule.bind(ServiceBinder) (at
> > AppModule.java:75) (for service 'TextMarketerNotificationSender')
> >   AppModule.NotificationSenderFacade Invoking constructor public
> >
> com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade(com.quivinco.webapps.tbs.utils.CommonsEmailNotificationSender,com.quivinco.webapps.tbs.utils.TextMarketerNotificationSender)
> > (for service 'NotificationSenderFacade')
> >   ioc.Registry Error invoking constructor public
> >
> com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade(com.quivinco.webapps.tbs.utils.CommonsEmailNotificationSender,com.quivinco.webapps.tbs.utils.TextMarketerNotificationSender):
> > java.lang.NullPointerException
> >   ioc.Registry Operations trace:
> >   ioc.Registry [ 1] Realizing service NotificationSenderFacade
> >   ioc.Registry [ 2] Instantiating service NotificationSenderFacade
> > implementation via
> > com.quivinco.webapps.tbs.services.AppModule.bind(ServiceBinder) (at
> > AppModule.java:75)
> >   ioc.Registry [ 3] Reloading class
> > com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade.
> >   ioc.Registry [ 4] Invoking constructor public
> >
> com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade(com.quivinco.webapps.tbs.utils.CommonsEmailNotificationSender,com.quivinco.webapps.tbs.utils.TextMarketerNotificationSender)
> > (for service 'NotificationSenderFacade')
> >   AppModule.NotificationSenderFacade Construction of service
> > NotificationSenderFacade failed: Error invoking constructor public
> >
> com.quivinco.webapps.tbs.utils.DefaultNotificationSenderFacade(com.quivinco.webapps.tbs.utils.CommonsEmailNotificationSender,com.quivinco.webapps.tbs.utils.TextMarketerNotificationSender):
> > java.lang.NullPointerException
> >
> >
> >
> >
> >   ---
> >   This email is free from viruses and malware because avast! Antivirus
> > protection is active.
> >   http://www.avast.com
> >
> >
> > ---
> > This email is free from viruses and malware because avast! Antivirus
> > protection is active.
> > http://www.avast.com
> >
>

Reply via email to