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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:42:53+00:00 2026-06-18T02:42:53+00:00

I have a class that looks like so: public class Configurator { private static

  • 0

I have a class that looks like so:

public class Configurator {
    private static Configurator INSTANCE = null;

    private int maxRange = 1;

    // many other properties; each property has a default value

    private static synchronized Configurator getInstance() {
        if(INSTANCE == null)
            return new Configurator();

        return INSTANCE;
    }

    public static int getMaxRange() {
        getInstance().maxRange;
    }

    public static void setMaxRange(int range) {
        getInstance().maxRange = range;
    }

    // Getters and setters for all properties follow this pattern
}

It serves as a global configuration object that can be set on app startup, and then is used by dozens of classes throughout the project:

// Called at app startup to configure everything
public class AppRunner {
    Configurator.setMaxRange(30);
}

// Example of Configurator being used by another class
public class WidgetFactory {
    public void doSomething() {
        if(Configurator.getMaxRange() < 50)
            // do A
        else
            // do B
    }
}

I’m now importing this code into a Spring project, and am trying to configure my Sprinig XML (beans). My guess is that I could define a lone Configurator bean like so (or something similar):

<bean id="configurator" class="com.me.myapp.Configurator" scope="singleton">
    <property name="maxRange" value="30"/>
    <!-- etc., for all properties -->
</bean>

That way, when WidgetFactory#doSomething executes, Spring will have already loaded the Configurator class and configured it ahead of time.

Is it correct for me to set the scope="singleton", or does this not matter? Am I setting the static properties correctly? Is there anything else I need to do or consider here? Thanks in advance.

  • 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-18T02:42:54+00:00Added an answer on June 18, 2026 at 2:42 am

    There are some difference between Singleton as a design pattern and Spring’s singleton facility. Singleton as a design pattern will ensure you have one object of class defined per Class Loader. Spring’s singleton facility (and approach), in contrast, will define one instance per Spring Context.

    In this case you can utilize your getInstance() method to be used by Spring to grab your object instance:

    <bean id="configurator" class="com.me.myapp.Configurator" factory-method="getInstance">
    </bean>
    

    With Spring the singleton bean scope is the default therefore you don’t need to define it.

    If you want to use configurator as a Spring bean you will have to inject it in other objects, not use getInstance() to grab it. So in other Spring beans use @Autowired or define reference to bean through xml file. If you don’t reorganize usage of configurator in other classes, there will no be difference, Spring will instantiate your class, but you will use it as before.

    Also I saw that you have an error in designing your singleton. Your getInstance() method should be public, and other methods should not be static. In the example you’ve used you should use Singleton like this:

    Configurator.getInstance().someMethod()
    

    In this case you actually just use the Singleton class, without instantiating any objects! See wikipedia on Singleton (with Java example) for more information on the Singleton design pattern and how to use it.


    NOTE: It’s worth knowing and attempting to use Configurator as a Singleton and make use Spring’s singleton facility. If you do this the benefits will be that you can

    • Remove the getInstance() method
    • Make your constructor public
    • Let the Spring instantiate that single object.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that looks like the following: public class MyClass { private
I have a class with a constructor that looks like this: public TimedWorker(int timerInterval,
I have a class that looks like this: public class BasePadWI { public WorkItem
Currently I have a class that looks like this: public class MyClass : IMyClass
I have class that looks like this: class A { public: class variables_map vm
I have a class that roughly looks like this: public class ViewModel { public
We have a class a class that looks something like the following: public class
Assume I have a class that looks like this: class Sample { public string
I have a Handler class that looks like this: class Handler{ public $group; public
I have generic type that looks like: public class GenericClass<T, U> where T :

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.