Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7707737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:26:10+00:00 2026-06-01T00:26:10+00:00

How to transfer beans from one ClassPathXMLApplicationContext to another? I create one context like

  • 0

How to transfer beans from one ClassPathXMLApplicationContext to another?

I create one context like this

ClassPathXMLApplicationContext myOneContext = new ClassPathXMLApplicationContext("path to my  xml bean definitions"); // It loads 10 beans which probably refer each other

ClassPathXMLApplicationContext my2ndContext = new ClassPathXMLApplicationContext("path to my  xml bean definitions"); // It loads 2 beans, which probably refer each other

is it possible to transfer all beans from my2ndContext to myOneContext?

I have been getting beanFactory from myOneContext and getting its internal bean factory as DefaultSingletonBeanRegistry and invoking registerSingleton

Object beanObject = my2ndContext.getBean("beanName");
DefaultSingletonBeanRegistry lbf =(DefaultSingletonBeanRegistry)myOneContext.getBeanFactory();
lbf.registerSingleton("beanName", beanObject);

is this ok? i feel i am doing a kind of hack. also not sure what else i am missing.

Another option is to retain the beans in the same contexts and add a parent relation among the AppContexts and its bean factories.

myOneContext.setParent(my2ndContext);
DefaultListableBeanFactory lbf = (DefaultListableBeanFactory)myOneContext.getBeanFactory();
lbf.setParentBeanFactory(my2ndContext.getBeanFactory());

this way all the beans from both context are visible in the myOneContext

But a problem arises when i have to destroy the my2ndContext and i set the parent of the BeanFactory to null.

DefaultListableBeanFactory lbf = (DefaultListableBeanFactory)myOneContext.getBeanFactory();
lbf.setParentBeanFactory(null); <<< throws exception

because it doesnt allow the bean factory to be changed.

From Spring Sourse: AbstractBeanFactory.java
public void setParentBeanFactory(BeanFactory parentBeanFactory) {
    if (this.parentBeanFactory != null && this.parentBeanFactory != parentBeanFactory) {
        throw new IllegalStateException("Already associated with parent BeanFactory: " + this.parentBeanFactory);
    }
    this.parentBeanFactory = parentBeanFactory;
}

which way should i choose? transfer the beans OR make a parent relation. which is preferable?

Thanks,

Regards,

Vimal

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:26:12+00:00Added an answer on June 1, 2026 at 12:26 am

    I recommend setting up a dedicated parent context with all shared infrastructure inside. The application specific contexts would use this one as their parent. It would be possible to refresh the dynamic context while leaving shared part in parent intact.

    It is best to keep parent-child hierarchy flat – one parent context and multiple child contexts. Cross-dependencies between sibling application context while tempting at first, would contribute to chaos later on and supporting such dependencies would require some non-trivial hacks or workarounds.

    If this kind of sibling dependencies is a must, have a look at OSGI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are methods to transfer a CGPoint from one UIView to another and from
How do I transfer a file from one BlackBerry device to another over WiFi
In order to transfer text from one textbox to another, I have created a
My goal is to transfer entries from one ldap server to another - preserving
I want to transfer one asp.net page form values to another page when i
I transfer a fair amount of XML from the server to the client, in
I am passing an object from richface datatable like: <rich:column> <a4j:commandLink value=Transfer inside Group
i want to transfer the div's from one parent div to other on click
I'm looking to transfer some data from a multi-million row table into another table.
I want to transfer a domain name to a new registrar, but this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.