4 Replies Latest reply on Mar 17, 2006 6:49 AM by Adrian Brock

    Behavior of interface implementation or subclasses injection

    Viet Master

      Using microcontainer (the one that is in JBoss-4.0.4RC1) I have the following behavior :

      - bean A, B and interface C such as A implements C and B wants C to be injected.

      - bean A, B and C such as A extends C and B wants C to be injected.

      The microcontainer wants the exact class to be injected in the bean. Is it what I should expect or not ?

      thanks

        • 1. Re: Behavior of interface implementation or subclasses injec
          Scott Stark Master

          I believe most injection context allow you to override the type to use. What is the example configuration that illustrates this A/B/C relationship?

          • 2. Re: Behavior of interface implementation or subclasses injec
            Chris Laprun Master

            We have a class A which extends C.

            We then have the following beans:

             <bean name="BBean" class="B">
             <property name="myC"><inject bean="ABean"/></property>
             </bean>
            
             <bean name="ABean" class="A">
             </bean>
            


            At runtime, validation reports an error though the injection works fine:

            BBean -> java.lang.IllegalArgumentException: Wrong arguments. setMyC expected=[C] actual=[A]
            at org.jboss.kernel.plugins.deployment.AbstractKernelDeployer.internalValidate(AbstractKernelDeployer.java:241)
            at org.jboss.kernel.plugins.deployment.AbstractKernelDeployer.validate(AbstractKernelDeployer.java:173)
            


            • 3. Re: Behavior of interface implementation or subclasses injec
              chung chung Newbie

              wondering ...I haven't error

              <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
               xmlns="urn:jboss:bean-deployer:2.0">
               <bean name="ABean" class="org.jboss.example.microcontainer.constructor.A" />
               <bean name="BBean" class="org.jboss.example.microcontainer.constructor.B" >
               <property name="myc"><inject bean="ABean"/></property>
               </bean>
              </deployment>
              
              public class A extends C{
              
              }
              public class B {
               public void setMyc(C o){
               System.out.println("inject C");
               }
              }
              public class C {
              
              }
              public class ConstructorStandaloneMain {
              
               public static void main(String[] args) {
               try{
               StandaloneBootstrap bootstrap = new StandaloneBootstrap(args);
               bootstrap.run();
               ......
              }
              

              ps:usage
              * BasicKernelDeployer.
              *
              * An extension to the abstract kernel deployer that keeps track
              * of deployments and adds a simple shutdown method.
              *
              * @author Adrian Brock
              * @version $Revision: 1.2 $
              */

              * An Kernel deployer.
              *
              * @author Adrian Brock
              * @author Les A. Hazlewood
              * @version $Revision: 1.11 $


              result as below
              (AbstractController.java:220) - Dependencies for BBean: [AbstractDependencyItem@ff057f{name=BBean dependsOn=ABean whenRequired=Configured dependentState=Installed resolved=false}]
              plugins.AbstractController (AbstractController.java:436) - Dependencies resolved BBean for Described
              plugins.AbstractController (AbstractController.java:442) - BBean Described
              plugins.AbstractController (AbstractController.java:436) - Dependencies resolved BBean for Instantiated
              config.Configurator (Configurator.java:112) - Get constructor joinpoint info=AbstractBeanInfo@1342ba4{name=org.jboss.example.microcontainer.constructor.B classInfo= properties=[class, myc] methods=[hashCode, finalize, getClass, setMyc, toString, clone, notifyAll, equals, wait, wait, wait, registerNatives, notify] constructors=[] events=} constructor=null
              plugins.AbstractController (AbstractController.java:442) - BBean Instantiated
              plugins.AbstractController (AbstractController.java:436) - Dependencies resolved BBean for Configured
              config.Configurator (Configurator.java:491) - Get property setter join point info=AbstractPropertyInfo@1d332b{name=myc getter=null setter=ReflectMethodInfoImpl@10a6ae2{name=setMyc[ReflectClassInfoImpl@ef2c60{name=org.jboss.example.microcontainer.constructor.C}] return=void}} metaData=AbstractDependencyValueMetaData@dc6a77{value=ABean dependentState=Installed}
              config.Config (Config.java:217) - Get method Joinpoint jpf=ReflectJoinpointFactory@4a6cbf{classInfo=org.jboss.example.microcontainer.constructor.B} target=null name=setMyc paramTypes=[org.jboss.example.microcontainer.constructor.C]
              inject C
              plugins.AbstractController (AbstractController.java:442) - BBean Configured
              plugins.AbstractController (AbstractController.java:436) - Dependencies resolved BBean for Create
              dependency.KernelControllerContextActions$LifecycleAction (KernelControllerContextActions.java:564) - No create method for AbstractKernelControllerContext@c1f10e{ metadata=AbstractBeanMetaData@1d64c37{name=BBean bean=org.jboss.example.microcontainer.constructor.B properties=[myc] constructor=null}name=BBean target=org.jboss.example.microcontainer.constructor.B@1b000e7 state=Configured depends=AbstractDependencyInfo@b76fa{idependOn=[AbstractDependencyItem@ff057f{name=BBean dependsOn=ABean whenRequired=Configured dependentState=Installed resolved=true}] unresolved=[AbstractDependencyItem@ff057f{name=BBean dependsOn=ABean whenRequired=Configured dependentState=Installed resolved=true}]}}
              plugins.AbstractController (AbstractController.java:442) - BBean Create
              plugins.AbstractController (AbstractController.java:436) - Dependencies resolved BBean for Start
              dependency.KernelControllerContextActions$LifecycleAction (KernelControllerContextActions.java:564) - No start method for AbstractKernelControllerContext@c1f10e{ metadata=AbstractBeanMetaData@1d64c37{name=BBean bean=org.jboss.example.microcontainer.constructor.B properties=[myc] constructor=null}name=BBean target=org.jboss.example.microcontainer.constructor.B@1b000e7 state=Create depends=AbstractDependencyInfo@b76fa{idependOn=[AbstractDependencyItem@ff057f{name=BBean dependsOn=ABean whenRequired=Configured dependentState=Installed resolved=true}] unresolved=[AbstractDependencyItem@ff057f{name=BBean dependsOn=ABean whenRequired=Configured dependentState=Installed resolved=true}]}}
              plugins.AbstractController (AbstractController.java:442) - BBean Start
              


              • 4. Re: Behavior of interface implementation or subclasses injec
                Adrian Brock Master

                 

                "julien@jboss.com" wrote:

                The microcontainer wants the exact class to be injected in the bean. Is it what I should expect or not ?


                No it doesn't. That is just the error message telling you that
                A doesn't implement C.
                Actually, I trap IllegalArgumentException from reflection
                and print a more readable error message.

                Maybe you have a different cause of IllegalArgumentException
                (from reflection not your setter) that I am misinterpreting?