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 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 509k
  • Answers 509k
  • 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 you can't return the results to sendRequest, because sendRequest can't… May 16, 2026 at 4:48 pm
  • Editorial Team
    Editorial Team added an answer If you can lose duplicate keys: scala> val map =… May 16, 2026 at 4:48 pm
  • Editorial Team
    Editorial Team added an answer Please read this article from the SDK docs to get… May 16, 2026 at 4:48 pm

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 class that inherits from a specific class: class C(dict): added_attribute =
consider a JSF web application with a managed bean FooBean.java. I've declared this FooBean
I have an application that I am currently writing that will use Spring and
What's the difference between declaring the TestController with the Spring Controller stereotype like this:
the following is part of a socket class that im working on. what i
I was trying out spring-security for a sample web application. When declaring the LDAPAuthenticationProvider,
I come from PHP world, where declaring a function in the middle of a
I'm declaring a string variable this way in classic ASP: Dim i As String
I have a declaration like this #include Output/PtPathWriter.h // class PtPathWriter // I've also
I am trying to create a simple regex kind of thing for my own

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.