3 Replies Latest reply on Jul 18, 2010 2:53 AM by Stuart Douglas

    Normal-scoped beans and constructors

    Wujek Srujek Newbie
      Hi. I have the following application:

      public class Main {

          public static void main(String[] args) {
              new StartMain(null).go();

      class StartObserver {

          void printHello(@Observes ContainerInitialized event, SomeBean bean) {

      class SomeBean {

          public SomeBean() {
              System.out.println(getClass().getName() + "() executing a very expensive operation");

          public void method() {
              System.out.println(getClass().getName() + ".m()");

      When it executes (Weld-se) I get the following output:

      com.test.SomeBean_$$_javassist_2() executing a very expensive operation
      com.test.SomeBean() executing a very expensive operation

      As you can see, the proxy generated for the bean calls the superclass's constructor. It may happen that the constructor does something that has to be done only once, in which case the whole application fails (like some write to the database that fails with an exception when such entry already exists).
      Agreed, according to most (if not all) DI gurus no work should be done in the constructor, but what if? It is not a law, and this apparently would not work for Weld and its proxies. Is it the desired behavior? Or am I doing things wrong?