Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamically picking Spring-Application-Context file

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a requirement that needs mapping certain bean to different class based on a flag value.

The flag value is read form DB and if flag is ON , we want Spring to use
Spring-Application-Context-A file
If flag is OFF , we want spring to use
Spring-Application-Context-B file.

Both will have same beanIDs but class names mapped to it will be different.

How can we do this?

Thanks!
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One approach you might consider is subclassing the ContextLoaderListener class and override the contextInitialized method. The standard ContextLoaderListener method basically looks like:


Your typical configuration in a web application would be to set the web application ContextParams for contextConfigLocation to point to additional bean files to load. This value is available in the contextInitialized() method via the event object passed in. You could probably simply write this value to be whatever bean files you wanted your contextConfigLocation to reference based on the value in the database.

Keep in mind your beans won't be initialized at that point so you'll have to find some alternative way to query the database.
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark.

Can BeanFactoryPostProcessors / BeanPostProcessors be used for this purpose?
If so Which approach do you think is better.

Thanks!

 
Mark Secrist
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is related to the IOC container lifecycle. BeanFactoryPostProcessor gets run after the context (and bean definitions) are already loaded but before any beans get intantiated. If you are wanting to switch out entire bean definitions based on the database switch then it's already too late by the time postProcessBeanFactory gets run.

The BeanPostProcessor allows customization of a particular bean after instantiation but either before or after initialization. If it were a matter of using the same bean in either case but different configuration, this might be the place to do it. If you are wanting a completely different bean, you might be able to do it here but it would be a completely custom solution and would likely be difficult to maintain.

 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic