Announcement Announcement Module
No announcement yet.
Ensuring single instance of a bean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ensuring single instance of a bean

    Sorry for asking this weird question but is there any way to ensure that there is only one instance of bean running? By default all beans are singleton but if i specify two bean declarations for a class in the xml file, it will create two instances of the bean. Is there any mechanism so that Spring will through an error if there are multiple declaration of same bean while loading bean definitions?

  • #2
    well, spring-IDE will complain if you have 2++ definitions of a bean with the same id. Though in the runtime, the last one wins.
    If you define 2 beans of the same class but with different id's, then that's a perfectly valid case for spring, so no exceptions or warnigs will be thrown or logged.

    If you want to perform a check for the in-container instances of the same class, you would have to subclass appContext...


    • #3
      In Spring beans don't "run". The Spring container isn't like a J2EE container (where objects actually do run, since a J2EE container is an execution environment). Spring is an object store. I make this distinction because there is sometimes confusion as to what Spring does.

      AFAIK It doesn't have a mechanism for making sure you've only created one. But there is nothing to stop you from creating a class which keeps track of how many instances there are and throwing an exception if you create more than one. But I'd recommend against that.

      Why you feel this is necessary? When I have converted applications which use a static getInstance method to construct only one version, I still use a class which COULD create more than one. But I architect my class so that it doesn't really matter. Each instance of the class has its own collaborators. I leave details about how many it makes sense to create to the infrastructure portion of my system (which includes the application context definition). So if the system integrator decides the system needs only one, it creates only one.


      • #4
        I would try employing the standard Singleton pattern for the class (APress Pro Spring, p.80), so even if you have 47 beans pointing to it, you should have only one instance of the object.


        public class Critter {

        private static Critter critter;

        public static Critter getCritter() {
        return critter;

        static {
        critter = new Critter();




        • #5
          You could either have all 47 beans use the same bean id. Or if they are in separate application contexts, you could consider using an application context with common objects that you use as a parent to the other contexts. You would need to use a SingleBeanFactoryLocator.

          If you wanted to get this up and running quickly, you could do what I've done in places where I haven't made a complete Spring cut over: use the singleton pattern and call getCritter from each place you need this bean:

          <bean id="critter" class="Critter" factory-method="getCritter"/>


          • #6
            Thanks for your replies guys. I think the last solution given by wpoitras seems to solve my problem.