Take the 2-minute tour ×

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>();

    return objects;

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


share|improve this question
I found this thread: forum.springsource.org/showthread.php?t=46101 and it seems that the methods of the Java class referenced by the bean are not synchronized unless you write them as synchronized. –  yeforriak Aug 13 '10 at 15:14
Of course not. Spring is still Java; it won't read your mind. Singletons must be written in a thread safe manner. –  duffymo Aug 13 '10 at 15:49
you need to post some code. usually, a factory should not carry much state, so the factory methods should usually not block. and if you didn't sync anything, nothing should block (state may be messed up though). What are you doing in your createObject method? –  Sean Patrick Floyd Aug 14 '10 at 21:23
@seanizer I have edited the question added the code I am using in the factory. Maybe is the beanFactory the one is giving synchronization problems? –  yeforriak Aug 16 '10 at 10:42
add comment

1 Answer

up vote 1 down vote accepted

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() {
  if (someObjectInstance = null)
    someObjectInstance = (SomeObject)beanFactory.getBean(SomeObject.class.getName());        

  return objects;

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

share|improve this answer
I do not want the createObjects() method to be synchronized since I want to call it from different threads concurrently. I was using a profiler and it seems that there are some blocking when the threads invoke the createObjects(the line with the beanFactory). Answering your question, SomeObject is a prototype. –  yeforriak Aug 24 '10 at 15:59
add comment

Your Answer


