Announcement Announcement Module
Collapse
No announcement yet.
can Beanfactory adapt types? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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,

    Comment

    Working...
    X