Announcement Announcement Module
Collapse
No announcement yet.
Injection confusion Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injection confusion

    I was running a simple example, which worked, but am confused by something. It injects a string value into a simple class:

    /**
    * Created on Sep 21, 2011
    */
    package com.apress.prospring3.ch4;

    /**
    * @author Clarence
    *
    */
    public class SimpleTarget {

    private String val;

    public void setVal(String val) {
    this.val = val;
    }

    public String getVal() {
    return val;
    }

    }

    The xml that injects the string value is:

    <bean id="injectBean" class="java.lang.String">
    <constructor-arg>
    <value>Bean In Child</value>
    </constructor-arg>
    </bean>

    My question is since the class defines no constructor method with a string variable, but defines a setter function with a String variable, why does this work? I would think setter notation in the xml would be used.

    Thanks

  • #2
    In your xml, you are not even defining a bean of type SimpleTarget, you are defining a bean of Java String type instead.

    Try using this to see if that works:

    <bean id="injectBean" class="mypackage.SimpleTarget ">
    <constructor-arg>
    <value>Bean In Child</value>
    </constructor-arg>
    </bean>

    Comment


    • #3
      As I said, the code does work and give the expected results, the string gets into where it is supposed to. This isn't my code, my question is why the author used constructor injection rather than setter injection since no constructor with a String parameter is defined in the class. I don't understand why this does work.

      Thanks

      Comment


      • #4
        I don't think you followed me.

        I believe that you posted only part of the xml configuration and missed some other bean config, there is NO reason that the code you posed can work as you said. To make it working, you should have something like following in your xml. Please check.

        Code:
        <bean id="simpleTarget" class="mypackage.SimpleTarget"> 
          <property name="val">  
            <ref bean="injectBean"/> 
          </property> 
        </bean>
        BTW, this is the setter injection which you were looking for.
        Last edited by tannoy; Jul 27th, 2012, 07:47 PM.

        Comment


        • #5
          <bean id="simpleTarget" class="mypackage.SimpleTarget">
          <property name="val">
          <ref bean="injectBean"/>
          </property>
          </bean>


          Your correct, I left that part out. But still, should this not be setter injection? I'm not sure why constructor works.

          Comment


          • #6
            It isn't constructor injection it is setter injection! The string you are injecting (injectBean) is created with a constructor BUT java.lang.String does have such a constructor...

            Comment


            • #7
              Thank you.

              Comment

              Working...
              X