Announcement Announcement Module
Collapse
No announcement yet.
How can a bean refer to itself? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Giri
    started a topic How can a bean refer to itself?

    How can a bean refer to itself?

    How can I set a property of a bean to the bean itself? The bean is not a singleton so I can't refer to its id. Is there an equivalent of "this" in spring XML? Extending this thought, how can I set a property to a containing bean?

    <bean id="parent" singleton="false">
    <property name="child">
    <bean class="com.my.example.child">
    <property name="referenceToParent" ref="parent"/> doesnt work because parent is not a singleton
    </bean>
    </property>
    </bean>

    Thanks

  • Rober2D2
    replied
    Originally posted by Giri View Post
    If I make a reference to a non-singleton bean won't it create a new instance of that bean instead of referring to the already created instance?
    I haven't used Spring 1.x, but if singleton="false" means the same as scope="prototype" in Spring 2.x, then I thing it will happen as you say. The injected bean will be a different instance.

    Probaly you must do that in code.

    Leave a comment:


  • Giri
    replied
    If I make a reference to a non-singleton bean won't it create a new instance of that bean instead of referring to the already created instance?

    Leave a comment:


  • Durden
    replied
    Could nested bean configuration help?

    See 3.3.1.3. Some examples from http://static.springsource.org/sprin...nce/beans.html


    Code:
    <bean id="exampleBean" class="examples.ExampleBean">
    
      <!-- setter injection using the nested <ref/> element -->
      <property name="beanOne"><ref bean="anotherExampleBean"/></property>
    
      <!-- setter injection using the neater 'ref' attribute -->
      <property name="beanTwo" ref="yetAnotherBean"/>
      <property name="integerProperty" value="1"/>
    </bean>
    
    <bean id="anotherExampleBean" class="examples.AnotherBean"/>
    <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

    Leave a comment:

Working...
X