3 Replies Latest reply on Jan 9, 2007 9:49 AM by jweidner

    POJOs instantiated twice

    jweidner

      org.jboss.seam.Component.instantiateJavaBean is constructing two instances of all my POJOs. Is that suppose to happen? The first one gets instantiated by the first line of instantiateJavaBean. The second instance is created by org.jboss.seam.Component.wrap. (I'm new to JSF, Seam, and Facelets, so excuse me if this is a stupid question.) Here's my constructor:

      public Location() {
      System.out.println( "Location constructed. " + this );
      Thread.dumpStack();
      }


      Below is the line printed and the start of the two stack traces.



      Location constructed. com.aaa.travel.Location@61a2e7

      java.lang.Exception: Stack trace
      at java.lang.Thread.dumpStack(Thread.java:1158)
      at com.aaa.travel.Location.(Location.java:22)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
      at java.lang.Class.newInstance0(Class.java:350)
      at java.lang.Class.newInstance(Class.java:303)
      at org.jboss.seam.Component.instantiateJavaBean(Component.java:1045)
      at org.jboss.seam.Component.instantiate(Component.java:1008)
      at org.jboss.seam.Component.newInstance(Component.java:1718)
      at org.jboss.seam.Component.getInstance(Component.java:1625)
      at org.jboss.seam.Component.getInstance(Component.java:1592)
      at org.jboss.seam.jsf.SeamVariableResolver.resolveVariable(SeamVariableResolver.java:46)
      at org.apache.myfaces.config.LastVariableResolverInChain.resolveVariable(LastVariableResolverInChain.java:42
      )
      at org.apache.myfaces.el.ValueBindingImpl$ELVariableResolver.resolveVariable(ValueBindingImpl.java:574)
      at org.apache.commons.el.NamedValue.evaluate(NamedValue.java:124)
      at org.apache.myfaces.el.ValueBindingImpl.resolveToBaseAndProperty(ValueBindingImpl.java:455)
      at org.apache.myfaces.el.ValueBindingImpl.getType(ValueBindingImpl.java:172)
      at org.jboss.seam.core.Expressions$1.getType(Expressions.java:46)
      at org.jboss.seam.core.Page$PageParameter.getConverter(Page.java:48)
      at org.jboss.seam.core.Pages.applyRequestParameterValues(Pages.java:523)
      at org.jboss.seam.jsf.AbstractSeamPhaseListener.beforeRender(AbstractSeamPhaseListener.java:186)
      at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:51)
      )



      Location constructed. com.aaa.travel.Location$$EnhancerByCGLIB$$771353c0@abf658

      java.lang.Exception: Stack trace
      at java.lang.Thread.dumpStack(Thread.java:1158)
      at com.aaa.travel.Location.(Location.java:22)
      at com.aaa.travel.Location$$EnhancerByCGLIB$$771353c0.()
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
      at java.lang.Class.newInstance0(Class.java:350)
      at java.lang.Class.newInstance(Class.java:303)
      at org.jboss.seam.Component.wrap(Component.java:1065)
      at org.jboss.seam.Component.instantiateJavaBean(Component.java:1054)
      at org.jboss.seam.Component.instantiate(Component.java:1008)
      at org.jboss.seam.Component.newInstance(Component.java:1718)
      at org.jboss.seam.Component.getInstance(Component.java:1625)
      at org.jboss.seam.Component.getInstance(Component.java:1592)
      at org.jboss.seam.jsf.SeamVariableResolver.resolveVariable(SeamVariableResolver.java:46)
      at org.apache.myfaces.config.LastVariableResolverInChain.resolveVariable(LastVariableResolverInChain.java:42
      )

        • 1. Re: POJOs instantiated twice

          One of them is the proxy instance. I guess there is no way for the generated subclass to avoid calling the superclass constructor. That's an interesting side-effect.

          • 2. Re: POJOs instantiated twice
            jweidner

            Maybe SEAM could recognize another annotation that indicated that the POJO is implementing a certain interface. Then the proxy instance wouldn't need to subclass the POJO but could just implement the interface.

            • 3. Re: POJOs instantiated twice
              jweidner

              So I suppose it's best then that the POJO's constructor doesn't do anything. Any required initialization should go in a method annotated with @Create, since Seam will call this method after it instantiates and injects the object.