2 Replies Latest reply on Mar 16, 2011 3:50 PM by infinity2heaven

    best practice to fill preconfigured values

    nimo22

      With Seam, I used the @Factory method to fill the values of selectItems which relates to a JSF-View component like selectManyMenu.


      With Weld, I have different ways to fill the values of selectItems before the view is rendered:
      1. use a @Producer-Method
      2. use @Inject with constructor (injectable constructor) in which the values are filled
      3. use @PostConstruct


      Which way is should I go? Is there a better (simpler) way?

        • 1. Re: best practice to fill preconfigured values
          infinity2heaven

          @PostConstruct and @Produces are the same. They perform complementary functions. For a complex usecase, a combincation of @Produces with a Qualifier, Scope, and depending on whether you want data initialization or not -- @PostConstruct will be required. For ex, look at the following Memberlisting bean:


          @SessionScoped
          public class MemberListProducer implements Serializable {
          
               private static final long serialVersionUID = 1L;
          
               @PersistenceContext
               private EntityManager em;
          
               private List< Member > members;
          
               /**
                * @Produces creates a session scoped member list that can be injected in any bean 
                * @return
                */
               @Produces
               @Named
               @CurrentMembers
               public List< Member > getMembers() {
                       return members;
               } 
          
               public void onMemberListChanged(@Observes( notifyObserver = Reception.IF_EXISTS ) final Member member ) {
                    retrieveAllMembersOrderedByName();
               }
          
               @PostConstruct
               public void retrieveAllMembersOrderedByName() {
                    CriteriaBuilder cb = em.getCriteriaBuilder();
          
                    CriteriaQuery< Member > criteria = cb.createQuery( Member.class );
                    Root< Member > member = criteria.from( Member.class );
                    criteria.select( member ).orderBy( cb.asc( member.get( "firstName" ) ) );
                    members = em.createQuery( criteria ).getResultList();
               }
          }
          



          The above SessionScoped bean maintains a list of members for a session. If the list needs to be accessed via a search screen then the data must be initialized, typically with @PostConstruct. It's job ends there. From this point, @Produces ensures that #{members} or @CurrentMembers retrieves the same list as long as the call is in the same session (unless you want a different scope). Seam 2's prior example of Factory resolves to something like this


          @Produces
          @ApplicationScoped
          @Named
          @CurrentMembers
          public List< Member > getMembers() {
                 return members;
          } 
          



          Hope that makes sense.





          • 2. Re: best practice to fill preconfigured values
            infinity2heaven

            Typo in the first line above: @PostConstruct and @Produces are not the same