1 Reply Latest reply on May 2, 2010 7:58 AM by alesj

    How do I inject a static field when a constructor is private?

    saltnlight5

      Hi,

       

      If I have a simple POJO like this:

       

      package mypkg;
      import java.io.PrintStream;
      class Foo {
           private PrintStream printStream;
           public void setPrintStream(PrintStream out) {
                this.printStream = out;
           }
           public void run() {
                printStream.println("test");
           }
      }
      

       

      Now how would I inject the System.out into the bean in deployment? If do this, it gives me error on creating

      JavaLangSystem bean, which make sense since it's constructor is private.

       

         <bean name="JavaLangSystem" class="java.lang.System" access-mode="FIELDS" />
         <bean name="Foo" class="mypkg.Foo">
           <property name="printStream"><inject bean="JavaLangSystem" property="out"/></property>
           <start method="run" />
         </bean>
      

       

      So is there way I can quickly inject a static field in a class that has private constructor?

       

      I do see some example that use a value-factory and a provider class like here http://anonsvn.jboss.org/repos/jbossas/projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/access-mode-beans.xml, but it seems too involved to get a simple thing like this to be  injected. Did I missed something?

       

      Thanks,

      Z

        • 1. Re: How do I inject a static field when a constructor is private?
          alesj
          So is there way I can quickly inject a static field in a class that has private constructor?

          Hmmmm, yes, this might be a problem aka not possible with current IoC support.

           

          This looks a lot like <constructor> element's support for static factory method.

          I'll have a look on how much work would it be to support this.

          e.g. factoryClass + (public) factoryField