Announcement Announcement Module
Collapse
No announcement yet.
Add existing instance of bean to application context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add existing instance of bean to application context

    In my app I create multiple application contexts at runtime (to support multiple databases). I want to add an exting bean instance to the application context so spring created beans can access this instance. Is this possible?

  • #2
    Found the solution myself:
    Code:
    applicationContext.getBeanFactory().registerSingleton("theBean", theBean);

    Comment


    • #3
      Originally posted by Kees de Kooter
      Found the solution myself:
      Code:
      applicationContext.getBeanFactory().registerSingleton("theBean", theBean);

      Hmmmm. But, before that gets executed, no existing beans in the context could have referenced the "theBean" object. So, 'theBean' could not have been active in configuration.

      I know, not part of your scenario, but just thinking if there is a way to use a "placeholder" kind of bean. Swappable factory use again?

      Comment


      • #4
        Yeah, the context definition could possible use a proxy with a HotSwappableTargetSource to wrap a bean. Then it can be swapped out and replaced at will. As long as the target doesn't actually get used until the 'real' target is dropped in there (via HotSwappableTargetSource's swap() method), the target used during init could be a mock or something of that nature...

        Comment

        Working...
        X