Announcement Announcement Module
Collapse
No announcement yet.
Dependency Inject of nested beans without a setter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dependency Inject of nested beans without a setter

    I have a class structure like this:

    Code:
    public class Test {
        private InnerTest innerTest;
        public InnerTest getInnerTest() {
            if(innerTest == null) {
                innerTest = new InnerTest();
            }
            return innerTest;
        }
    }
    
    public class InnerTest {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    Note that Test does not have a setter for InnerTest

    Is there any way to inject a value into: test.innerTest.name?

    And if so does the solution involve not having to know the type of test.innerTest?

    Regards,
    Mike

  • #2
    It seems that the bean wrapper implementation is able to cope with absence of setter and access the private property by reflection (just tested it on an app with spring 1.1.1).
    So, in your case, you can do the following
    Code:
    <bean id="outer" class="Test">
      <property name="innerTest">
        <bean class="InnerTest" >
          <property name="name">
            <value>My name</value>
          </property>
        </bean>
      </property>
    </bean>
    You would have noted that you need to know the inner bean class.

    HTH

    Comment


    • #3
      Originally posted by ojolly
      It seems that the bean wrapper implementation is able to cope with absence of setter and access the private property by reflection (just tested it on an app with spring 1.1.1).
      Sorry I was wrong about this. You'll have to provide a setter to see spring able to set the inner bean.

      Comment


      • #4
        Well, I posted an enhancement request I think this functionality would be nice to support using spring bean definitions to create some legacy classes I have to work with.

        Mike

        Comment


        • #5
          Well, my enhancement was closed because this functionaity is supported using syntax like:

          Code:
          <bean id="test" class="Test">
            <property name="innerTest.name">
              <value>Test Rate Schedule</value>
            </property>
          </bean>
          In case anyone cares to know.

          Mike

          Comment


          • #6
            Right, I feel dumb, I should have told you this the first time.
            Sorry

            Comment


            • #7
              Well, I'm having another problem now with readonly properties that contain Sets, Lists, and Maps. You don't happen to have any ideas on that front do you?

              Mike

              Comment

              Working...
              X