1 Reply Latest reply on Mar 9, 2010 11:34 AM by Pete Muir

    Initialization data not available until after scope begins

    Matthieu Chase Heimer Newbie
      I'm wondering what is the best (or standard) practice for the following scenario:

      A servlet with a URL and request parameter of http://localhost/war/servlet?ssn=111-222-3333

      Within the servlet I need a Person object in order to make the response. There would be some JPA work to get the person's details but a separate PersonDao (not shown) would function as the entity class.  I've come up with four possible ways but I'd like to know peoples preferred method and if there are others.

      Option 1) "The manager object"

      Servlet has:
      @Inject PersonManager pMgr;
      doGet() {
        String ssn = request.getParameter("ssn");
        Person person = pMgr.get(ssn);
        String name = person.getName();
      }

      Option 2) "The init method"

      Servlet has:
      @Inject Person person; // injects generic person
      doGet() {
        String ssn = request.getParameter("ssn");
        person.init(ssn);
        String name = person.getName();
      }

      Option 3) "The design violator"
      Servlet has:
      @Inject Person person; //request scope
      doGet() {
        String name = person.getName();
      }

      A PersonProducer has:
      @Produces @RequestScoped
      public Person getPerson(HttpServletRequest request) {
        //requires extension to make the request injectable
        //web api bleeds into model
      }

      Option 4) "The lazy producer"
      Servlet has:
      @Inject SSN ssn; //request scope
      @Inject Person person; //request scope
      do get() {
        ssn.setValue(request.getParameter("ssn"));
        String name = person.getName();
      }

      A PersonProducer has:
      @Produces @RequestScoped
      public Person getPerson(SSN ssn) {
        //works because the person producer is not called until the stub is used
        //not sure if this would be portable
        //almost wish there was a @Inject(produce=LAZY) person;
      }