Results 1 to 7 of 7

Thread: Injection confusion

  1. #1

    Default 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. #2

    Default

    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>

  3. #3

    Default

    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

  4. #4

    Default

    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 at 07:47 PM.

  5. #5

    Default

    <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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •