(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
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).
Y y = (Y)beanFactory.getBean("bean", Y.class);
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:
public void setBean( Y bean );
- 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.