Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Prototype bean constructor/factory runtime args

  1. #1
    Join Date
    Dec 2004
    Location
    Kansas City, Mo
    Posts
    4

    Default Prototype bean constructor/factory runtime args

    I would like to send a runtime argument to a prototype bean. This appears to be in the box for the XMLBeanFactory class, having a getBean(String, Object[] args) method, but that method does not exist for an ApplicationContext.

    The reason is that I'm needing to create Hibernate SessionFactories at runtime and bind them to a schema that is only known at runtime. So, I would like to have my own implementation of LocalSessionFactoryBean that would essentially function the same, but instead create prototype SessionFactories that are bound to the appropriate schema, that is not known until runtime. Basically, this is to support the fact that we have multiple identical schemas, one for each of our customers, and my DAOs will accept the customer ID as a parameter, at which point they will request the appropriate SessionFactory, which will probalby be stored in a map keyed by customer ID, or created and bound to the appropriate schema if they're not in the map yet.

    I've been bouncing this around for a few days, and just can't think of a clean "Spring-erific" way to do it. I could always just make local method calls, bypassing the framework, but then I would have the ability to add interceptors to the intermediate objects.

    Any advice would be much appreciated.

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

    Default

    getBean(String, Object[] args) is implemented by class AbstractBeanFactory. If you need to use this method from an ApplicationContext, consider using ClassPathXmlApplicationContext / FileSystemXmlApplicationContext for standalone applications or XmlWebApplicationContext for web applications. ContextLoaderListener, actually, registers a XmlWebApplicationContext in the servlet context.
    HTH
    Omar Irbouh

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

  3. #3
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    BTW, is related to the SPR-334 'Dynamic Bean Arguments'
    issue in Spring's Jira?

    I looked at the source for AbstractBeanFactory for getBean, but could not quickly determine the relevance.

  4. #4
    Join Date
    Aug 2004
    Location
    Auburn, AL, USA.
    Posts
    106

    Default Using Prototypes.

    I'm not sure exactly what you're trying to do; but I am using prototype beans in an application. What I am doing is:

    • implement BeanFactoryAware
      MyClass obj = (MyClass)beanFactory.getBean("beanName");


    At this point you can use the retrieved bean as a normal bean/object.

    Depending on what you mean by "send a runtime argument" you might need to look into either using Spring's AOP to wrap the calls; or extending one of Spring's ApplicationContexts and/or creating a BeanFactoryPostProcessor to add the functionality you want - take a look at the docs/source for the PropertyResourceConfigurer(s) to see this.

  5. #5
    Join Date
    Dec 2004
    Location
    Kansas City, Mo
    Posts
    4

    Default ClassPath

    Quote Originally Posted by irbouho
    getBean(String, Object[] args) is implemented by class AbstractBeanFactory. If you need to use this method from an ApplicationContext, consider using ClassPathXmlApplicationContext / FileSystemXmlApplicationContext for standalone applications or XmlWebApplicationContext for web applications. ContextLoaderListener, actually, registers a XmlWebApplicationContext in the servlet context.
    HTH
    Regarding irbouho's suggestion, I don't see an implementation of getBean(String, Object[] args) in anything but AbstractBeanFactory, which is only available via XMLBeanFactory. I don't see a way to access a getBean method that takes an argument array via any of the application contexts. I also tried checking out the BeanFactory that is available via an ApplicationContext, but the returned object is not based upon AbstractBeanFactory, so the method is still not available.

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

    Default

    If your context is a ClassPathXmlApplicationContext / FileSystemXmlApplicationContext / XmlWebApplicationContext... use
    Code:
    AbstractBeanFactory beanFactory = (AbstractBeanFactory) context.getBeanFactory();
    MyBean bean = (MyBean) beanFactory.getBean(name, args);
    HTH
    Omar Irbouh

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

  7. #7
    Join Date
    Dec 2004
    Location
    Kansas City, Mo
    Posts
    4

    Default Yep, that's it

    Quote Originally Posted by jbetancourt
    BTW, is related to the SPR-334 'Dynamic Bean Arguments'
    issue in Spring's Jira?

    I looked at the source for AbstractBeanFactory for getBean, but could not quickly determine the relevance.
    Yep, SPR-334 is definitely what I was hoping for. Right now, all I can come up with is an ugly hack of ThreadLocal objects and hot swap beans. I don't know why, but using ThreadLocals just smells bad to me, and I'm not even sure yet if it will work.

  8. #8
    Join Date
    Dec 2004
    Location
    Kansas City, Mo
    Posts
    4

    Default Guaranteed?

    Quote Originally Posted by irbouho
    If your context is a ClassPathXmlApplicationContext / FileSystemXmlApplicationContext / XmlWebApplicationContext... use
    Code:
    AbstractBeanFactory beanFactory = (AbstractBeanFactory) context.getBeanFactory();
    MyBean bean = (MyBean) beanFactory.getBean(name, args);
    HTH
    Okay, that could work, but am I guaranteed that the returned object is always going to be based upon AbstractBeanFactory?

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

    Default

    AFAIS from spring API, ClassPathXmlApplicationContext / FileSystemXmlApplicationContext / XmlWebApplicationContext extend AbstractRefreshableApplicationContext. The later has a DefaultListableBeanFactory beanFactoray property. Also DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory that extends AbstractBeanFactory. So, as of Spring 1.1.3, getBeanFactory, for these classes, should always return a AbstractBeanFactory subclass.
    HTH
    Omar Irbouh

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

  10. #10
    Join Date
    Mar 2005
    Posts
    1

    Default

    Quote Originally Posted by irbouho
    If your context is a ClassPathXmlApplicationContext / FileSystemXmlApplicationContext / XmlWebApplicationContext... use
    Code:
    AbstractBeanFactory beanFactory = (AbstractBeanFactory) context.getBeanFactory();
    MyBean bean = (MyBean) beanFactory.getBean(name, args);
    HTH
    How would look these parameters in applicationConext.xml file? I looked at spring reference but not found any example.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 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
  •