Results 1 to 4 of 4

Thread: BeanFactory reference in config?

  1. #1
    Join Date
    Aug 2004
    Location
    Irvine, CA
    Posts
    17

    Default BeanFactory reference in config?

    Is there a way to pass the beanfactory as a reference into an existing object?

    Let's say I have a class that I want to use as a bean within Spring, but don't have the ability to modify it to implement BeanFactoryAware. Is there a special token I can use during configuration to pass a setter an instance of the bean factory?

    For example:

    <bean id="someBean" class="com.foo.ComeClass">
    <property name="object"><ref local="&beanfactory"/></property>
    </bean>

    Where this would call setObject(Object obj) on the class and pass it the bean factory reference?

    Thanks...

  2. #2
    Join Date
    Aug 2004
    Posts
    109

    Default

    Not sure if this is what you are looking for but you can implement BeanFactoryAware interface on the bean itself and factory will be set.

    HTH
    Thanks,
    Alex.

  3. #3
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    if you really, really, really don't have the ability to modify your bean to implement BeanFactoryAware, you can create a bean that implements BeanFactoryAware and then injects the BeanFactory it receive into your bean (lot of indirections!!!)
    Code:
      <-- MySetterBean implements BeanFactoryAware -->
      <bean name="mySetter" class="MySetterBean">
        <property name="myBean">
          <ref local="myBean" />
        </property>
      </bean>
    
      <-- My initial Bean -->
      <bean name="myBean" class="MyBean"/>
    
    
    
      public class MySessterBean implements BeanFactoryAware &#123;
      ...
      public void setBeanFactory&#40;BeanFactory beanFactory&#41; throws BeansException &#123;
        myBean.setSomeProperty&#40;beanFactory&#41;;
      &#125;
    hhmm, one last thing, If setBeanFactory is fired before setMyBean (I am not sure of the order), you will need to implement InitializingBean and use afterPropertiesSet to inject the BeanFactory into your bean.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  4. #4
    Join Date
    Aug 2004
    Location
    Irvine, CA
    Posts
    17

    Default

    I was lazy and subclassed my object and had the subclass implement BeanFactoryAware and call its superclass to do the appropriate set.

    I was a bit lucky since it was a 3rd-party library, and its possible that the class could have been final (although improbable since hardly anyone does this) and was wondering if there was a special token I didn't know about.

    The "factory injector" solution is clever though, it could just use metadata and reflection to inject the application context/bean factory into any bean regardless of it's setup.

Similar Threads

  1. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  2. Cannot reference my spring config files from another project
    By lorelia in forum SpringSource Tool Suite
    Replies: 4
    Last Post: Aug 30th, 2005, 01:52 AM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. Config files with imports
    By p_d_austin in forum Container
    Replies: 3
    Last Post: Oct 25th, 2004, 11:33 AM
  5. Replies: 12
    Last Post: Sep 25th, 2004, 04:24 PM

Posting Permissions

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