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

  • Home
  • SEARCH
  • 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 8079297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:03:43+00:00 2026-06-05T16:03:43+00:00

Suppose I am using an ApplicationContext implementation in Spring. ApplicationContext is an interface in

  • 0

Suppose I am using an ApplicationContext implementation in Spring.

ApplicationContext is an interface in the Java Spring Framework and I cannot change it.

How do I ensure that there can be only one instance of this implementation?

For eg. I have the following code –

public class ApplicationContextSingleton 
{
     private static ApplicationContext context;
     private static int numberOfInstances = 0;

     public static ApplicationContext getApplicationContext()
     {
         if(numberOfInstances == 0)
         {
             context = new ClassPathXmlApplicationContext("spring.xml");
             numberOfInstances++;
         }
         return context;
     }
}

This way, I can ensure that there is only one instance of ApplicationContext, provided it is obtained as follows –

ApplicationContext context = ApplicationContextSingleton.getApplicationContext();

But that doesn’t stop another programmer from saying –

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 

thereby creating a new ApplicationContext. How to prevent this from occuring?

  • 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-05T16:03:44+00:00Added an answer on June 5, 2026 at 4:03 pm

    You want to make ApplicationContext a singleton, so I’d override the class with my own custom class, make sure it appears first on the class path, and make it have a private constructor.

    That is, if you’re dead set on making it a singleton. There are better ways to solve your problem, as pointed out by other answers and comments.

    Should note that it’s usually a bad idea to override pieces of libraries, as it can be the cause of headaches later, especially when you try upgrading your framework version.

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

Sidebar

Related Questions

Suppose I let the user to write a condition using Javascript, the user can
How can we get details from twitter and face book using PHP suppose think
Suppose I am using an MVC framework, and my views require Javascript files located
I am building an application using Spring Framework 3.1 I am having my controllers
Suppose using .wrap() can wrap a tag on a element, however I want to
Suppose I am only using a few of the jQuery functions say show, hide,
Suppose I am using Google's Gson library to parse JSON into Java data structures.
Hi anybody please tell me the difference between IDE and a FRAMEWORK ,suppose that
I'm trying to inject Spring beans into an EJB using @Interceptors(SpringBeanAutowiringInterceptor.class) but I cannot
suppose I implemented my site using Zend Framework and then after some time scalability

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.