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 3362006
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:13:16+00:00 2026-05-18T03:13:16+00:00

How i can define one ApplicationContext as prototype spring bean in other application context.

  • 0

How i can define one ApplicationContext as prototype spring bean in other application context. Also i need pass current context as parent to new application context.

Details:

I have Bean, that represent one user sessions in rich client application. This class manage lifetime of application context, and few other objects (like database connection). This session beans itself configured by special “start-up application context” .

Now i’m want unit test this session beans, but have trouble because session specific application context created inside session bean, and has many depend to “start-up context”;

Example code:

public class UserDBAminSession implements ApplicationContextAware, UserSession {
    ApplicationContext startupContext;
    ApplicationContext sessionContext;

    public void setApplicationContext(ApplicationContext startupContext) {...}

    public void start() {
        createSessionContext() ;
    }

    private void createSessionContext() {
        sessionContext = new ClassPathXmlApplicationContext("admin-session.xml", startupContext);
    }
}

For testing purpose i want relapse createSessionContext function code with something like this:

sessionContext = startupContext.getBean("adminContext", ApplicationContext.class);

Then i can create mock of startupContext, what return some stub. Or even DI “session context” to bean by spring, in some future. But, i don’t know how pass parent context parameter to ClassPathXmlApplicationContext constructor. I’m try something like this, but it seems not work:

<bean id="adminContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"
        scope="prototype" autowire="constructor">
    <constructor-arg type="java.lang.String">
        <value>admin-session.xml</value>
    </constructor-arg>
</bean>

Also I’m think about create application context on top level and pass it by setter, but:

  1. This just move problem to above level, not solve. In fact it already done (UserSession – are this “top level”).
  2. This broke RAII pattern.
  3. This need huge code refactoring.

Or make special “context factory” objects, but it harder already not best code.

What look stupid, I can’t IoC objects from IoC framework itself. May be i’m misread some spring documentation?

Any other idea, how unit-test this class?

  • 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-05-18T03:13:17+00:00Added an answer on May 18, 2026 at 3:13 am

    Use FactoryBean and ApplicationContextAware interfaces.

    public class ChildApplicationContextFactoryBean implements FactoryBean, ApplicationContextAware {
    
        protected String[] configLocations;
    
        protected ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        @Override
        public Object getObject() throws Exception {
            return new ClassPathXmlApplicationContext(configLocations, applicationContext);
        }
    
        @Override
        public Class getObjectType() {
            return ClassPathXmlApplicationContext.class;
        }
    
        @Override
        public boolean isSingleton() {
            return true;
        }
    
        public void setConfigLocations(String[] configLocations) {
            this.configLocations = configLocations;
        }
    
    }
    

    Usage:

    <bean class="com.skg.ecg.portal.operation.transit.ChildApplicationContextFactoryBean">
        <property name="configLocations">
            <list>
                <value>applicationContext.xml</value>
            </list>
        </property>
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As you know in C# classes we can define an indexr with more than
I have a complicated combined model for which I can define a likelihood in
In my application, I want to load some methods during run time. that is,
i'm reading the android developer docs on creating custom components and one thing that's
I want to define a set of error messages so that when validation errors
I have to implement a strange requirement that goes as follows: Users can hover
I am looking for PHP framework to build a Workflow Management system. While searching
I'm trying to display an HTML table of values with about 20 columns where
I wrote my own settings manager, serialization is provided by BinaryFormatter. It is cool

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.