[weld-dev] Bean created twice
Mark Struberg
struberg at yahoo.de
Sun Jul 25 14:52:55 EDT 2010
Hi Arun!
There is nothing to worry. This usually happens at the time some proxy instance
gets created for your class.
It is generally considered a _very_ bad idea to do initialisation and stuff in
class construction code.
The reason is that Weld, OWB, various EJB containers, etc not only use interface
proxying (as java.lang.reflect.Proxy does) but also provide class proxying. This
is basically a kind of 'extends' of the class you proxy, and while initialising
the proxy it therefor also needs to initialise all the superclass chain. Thus
your constructor will get called here also.
It's basically always a good idea with CDI managed beans (same is true for EJBs
or objects managed by lots of other DI containers) to perform initialisation in
a @PostConstruct or @Inject annotated method.
E.g.: it's not a good idea to have something like
private Map x = new ConcurrentHashMap();
because creating a ConcurrentHashMap is not a cheap operation!
It would be much better to
private Map x;
@PostConstruct
protected void init() {
x = new ConcurrentHashMap();
}
LieGrue,
strub
----- Original Message ----
> From: Arun Gupta <arun.p.gupta at oracle.com>
> To: Pete Muir <pmuir at redhat.com>
> Cc: weld-dev at lists.jboss.org
> Sent: Thu, July 22, 2010 8:39:03 PM
> Subject: Re: [weld-dev] Bean created twice
>
> Thanks posted http://www.seamframework.org/Community/BeanCreatedTwice
>
> -Arun
>
> On 7/22/10 3:35 PM, Pete Muir wrote:
> > Hi Arun
> >
> > Please post on the forum at http://www.seamframework.org/Community/WeldUsers
>- this list for development discussions.
> >
> > Thanks
> >
> > On 22 Jul 2010, at 19:26, Arun Gupta wrote:
> >
> >> For the attached simple application (WAR and source), accessing
>http://localhost:8080/HelloConversation/faces/index.xhtml creates ShoppingCart
>twice. Any idea ?
> >>
> >> Utility class is ...
> >>
> >> @Named("util")
> >> @ConversationScoped
> >> public class ConvUtil implements Serializable {
> >> @Inject ShoppingCart cart;
> >>
> >> public ShoppingCart getCart() {
> >> return cart;
> >> }
> >> }
> >>
> >> index.xhtml has ...
> >> <h:inputText value="#{util.cart}" title="name" id="name"
> >> required="true" />
> >>
> >>
> >> ShoppingCart is ...
> >>
> >> @Named
> >> @ConversationScoped
> >> public class ShoppingCart implements Serializable {
> >> public ShoppingCart() {
> >> System.out.println("Creating shopping cart ..." +
>Calendar.getInstance().get(Calendar.MILLISECOND));
> >> }
> >>
> >> }
> >>
> >> At least the log messages like:
> >>
> >> INFO: Creating shopping cart ...264
> >> INFO: Creating shopping cart ...265
> >>
> >> are printed.
> >>
> >> Tried changing the scope of ShoppingCart to RequestScoped but still the
>same. Changed @Named to @javax.faces.bean.ManagedBean on ShoppingCart.
> >>
> >> What am I not doing correctly ?
> >>
> >> -Arun
> >> --
> >> Blog: http://blogs.sun.com/arungupta
> >> Twitter: http://twitter.com/arungupta
> >>
> >>
><HelloConversation.zip><HelloConversation.war>_______________________________________________
>
> >> weld-dev mailing list
> >> weld-dev at lists.jboss.org
> >> https://lists.jboss.org/mailman/listinfo/weld-dev
> >
>
> --
> Blog: http://blogs.sun.com/arungupta
> Twitter: http://twitter.com/arungupta
> _______________________________________________
> weld-dev mailing list
> weld-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/weld-dev
>
More information about the weld-dev
mailing list