Results 1 to 2 of 2

Thread: can Beanfactory adapt types?

  1. #1
    Join Date
    Aug 2004
    Posts
    1

    Default can Beanfactory adapt types?

    Hi,
    (I'm new to Spring, so appollogies if I'm missing existing an feature of Spring that could help me)

    Does the BeanFactory or ApplicationContext provide any support for adapting types?

    For example say I have configured a bean 'bean' of type X, but call
    Code:
    Y y = (Y)beanFactory.getBean("bean", Y.class);
    This will fail, but say in my application X and Y are two different APIs to the same concept (they are not related by inheritance).
    I'd like to be able to configure the concept once, and then use it different APIs as appropriate.

    The example above is 'service locator' style, of course I would like to use same adaption for dependency injection.
    e.g. so 'bean' cofigured as X can be injected into:
    Code:
    public void setBean( Y bean );
    Motivation:
    • Reduce bloat of bean namespace.

    • Reduce need to explicitly configure seperate APIs/views to same conceptual entity, so can configure teh concept and not have to track exactly how it is used - which is a kind of coupling.

    Spring configurations tend to have a lot of 'internal' beans that seem to be explicitly doing this kind of adaption.

    To give some more realistic examples:
    • I write XML messaging applications, I want to configure an XML message, but different users might want DOM Document, JDOM Document, String Byte array or some other custom API.

    • I configure a JDBC data source and then want to be able to access: config information; JDBC DataSource, or Connection; Database helper API like Spring JDBC template; persistence session. See all these as APIs on same database concept. Further complicated because have custom config caters for things backup servers, to give failover and failback.


    I'm guessing Spring's dependency injection provide some support for adaption, because it maps string values to types via PropertSetters chose based on destination type. But I think this would just be at configuration time, and doesn't lok ike ot works for setBean(Y) example, nor for service locator style use of BeanFactory.

    If Spring doesn't support type adapters:
    • What should I be doing instead?

    • Can you give me any pointers if I wanted to extend Spring to support type adapters.


    Thanks,
    Mark.

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    You can always use something like a ProxyFactoryBean to adapt one interface/class to another. What such an adaptation consists of is very very specific to the classes and use-cases in question, so I think it's out of scope (or impossible) for Spring to try to do more out of the box...

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. BeanFactory, FactoryBean and &
    By cherrick in forum Architecture
    Replies: 1
    Last Post: Aug 23rd, 2005, 07:42 PM
  2. Replies: 4
    Last Post: May 4th, 2005, 07:16 AM
  3. JNDI BeanFactory lookup
    By edalquist in forum Container
    Replies: 4
    Last Post: Dec 16th, 2004, 02:18 PM
  4. Root of BeanFactory hierarchy
    By wangdongzjk in forum Data
    Replies: 3
    Last Post: Nov 28th, 2004, 10:33 PM
  5. Replies: 2
    Last Post: Aug 27th, 2004, 01:41 AM

Posting Permissions

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