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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:59:09+00:00 2026-05-16T07:59:09+00:00

I am declaring a Spring bean for a Java class that is used as

  • 0

I am declaring a Spring bean for a Java class that is used as a factory to create objects. I want to use this factory from different threads, the problem I am experienced is that threads are blocked when they try to create an object using the factory.

As far as I know spring beans are singletons by default, and this is what I want. I want the factory to be a singleton but I would like to create object using this factory from different threads. The method createObject() in the factory is not synchronized, therefore I do not understand very well why I’m having this synchronization issue.

Any suggestions about which is the best approach to achieve this?

This is the java code for the factory:

public class SomeFactory implements BeanFactoryAware {

private BeanFactory beanFactory;

public List<ConfigurableObjects> createObjects() {
    List<ConfigurableObjects> objects = new ArrayList<ConfigurableObjects>();
    objects.add((SomeObject)beanFactory.getBean(SomeObject.class.getName()));

    return objects;
}

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.beanFactory = beanFactory;
}

}

  • 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-16T07:59:10+00:00Added an answer on May 16, 2026 at 7:59 am

    As written, it doesn’t appear that there’s anything in this class that needs to be thread safe. You create a new ConfigurableObjects List each time you call createObjects. To that List you add a single SomeObject bean and then return it.

    One question:is the SomeObject instance supposed to be a singleton itself? If so, then you need to save it and only call getBean if it’s null like so.

    private SomeObject someObjectInstance = null;
    
    public synchronized List<ConfigurableObjects> createObjects() {
      List<ConfigurableObjects> objects = new ArrayList<ConfigurableObjects>();
      if (someObjectInstance = null)
      {
        someObjectInstance = (SomeObject)beanFactory.getBean(SomeObject.class.getName());        
      }
    
      objects.add(someObjectInstance);
      return objects;
    }
    

    In this case, you would need to synchronize access to CreateObjects as I’ve shown.

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

Sidebar

Related Questions

I'm having trouble declaring a const field in an abstract class. Why is this?
It seems that there are two different ways of declaring sorted associations in Grails
connecting to a mysql database from java using jdbc. declaring a query String query
I am using Spring WS and declaring a DefaultWsdl11Definition bean to generate the WSDL
I'm declaring a class like this: #include BindableInterface.h #include ../LuaHelper.h #include <map> #include <string>
I have an application that I am currently writing that will use Spring and
I see some Spring/Hibernate code having different policies regarding declaring DataAccessException in DAO interface
I know that I can create a javascript replace like this: str = str.replace(/mytarget/g,
Is it possible to instantiate a class from a string, without declaring another variable
Im declaring from a big list of custom, multiple level, objects (here called venueCounter)

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.