Back Forum Reply New

Replacing the injected bean from IOC Framework

We have a spring framework for our core services that supports multiple front end applications. Some applications need to have cacheable version of DAO and others don't. In the spring configuration files we are injecting cacheable version of DAO by default. Hence, my question is as follows,

After the spring framework has wired up and started is it possible to replace the injected cacheable version by non-cacheable version of DAO?

Any help is highly appreciated.

Thanks,
Harry

Lots of ways to quot;swapquot; a bean implementation using Spring configuration only.

You could simply use an lt;aliasgt; definition to point a given name at differing beans depending only on configuration.

Another option is quot;BeanReferenceFactoryBeanquot; which will direct a name at bean reference given as a property:
lt;bean id=quot;pageProducerquot; class=quot;org..beans.factory.config.Be  anReferenceFactoryBeanquot;gt;     lt;property name=quot;targetBeanNamequot; value=quot;viewCollectorquot;/gt; lt;/beangt;

So the name quot;pageProducerquot; in this case will in fact refer to the bean quot;viewCollectorquot;.

In yet more advanced usages, you may quot;auto-wrapquot; beans (by rules for example based on their names) by use of a org..beans.factory.config.BeanPostP  rocessor inserted into your context. A classic use of this technique constructs Spring-AOP proxies which replace the beans by quot;wrappedquot; versions implementing the same interface - this might be appropriate for your case if cacheability can be configured by means of pre- and post-actions taken before making a call to your DAO.

This entry from Colin's blog describes this classic usage:

colin/archives/...ons-spring-11/

(somewhat old, but still valid).

I hope at least one of these options will be appropriate for you

Cheers,
Boz.PS - on re-reading your post, you seem to want swapping *dynamically* after the framework has started. In which case you might be happy with a dynamic version of BeanReferenceFactoryBean, or else a dynamic Spring-AOP proxy - note that the Spring quot;TargetSourcequot; interface provides the run-time flexibility you are looking for, since each call routed through it may be resolved to a different bean.

Hey Bosman,

Those were excellent options you gave. I tried them all and they all worked. We have decided to go with alias tag option.

Thanks for your help.
Harry
¥
Back Forum Reply New