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

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

When declaring a template, I am used to having this kind of code: template
In Java, there is a practice of declaring every variable (local or class), parameter
Are declaring custom .NET event such as this , more common in a winform
Say I am declaring a class C and a few of the declarations are
I have just discovered this feature. Declaring an interface using the @interface syntax allows
What are differences between declaring a method in a base type virtual and then
I am declaring an array of void pointers. Each of which points to a
A quick question: When declaring the DLLImport Attribute in .Net, where does the runtime
C# has syntax for declaring and using properties. For example, one can declare a
C++ seems to be rather grouchy when declaring templates across multiple files. More specifically,

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.