Skip to content

ConcurrentModificationException in AbstractApplicationContext.getBeansOfType() [SPR-259] #4990

Closed

Description

@spring-projects-issues

Torsten Juergeleit opened SPR-259 and commented

Concurrent calls from within different threads (servlets) to AbstractApplicationContext.getBeansOfType() leads to the following exception:

java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:782)
at java.util.HashMap$KeyIterator.next(HashMap.java:818)
at org.springframework.beans.factory.support.AbstractBeanFactory.getSingletonNames(AbstractBeanFactory.java:481)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:1
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:468)
at de.deka.techarch.spring.bean.BeanUtils.getFirstInterceptedBeanOfType(BeanUtils.java:67)
at de.deka.dekanet.service.context.BusinessServiceFactory.getService(BusinessServiceFactory.java:86)

Our code which calls ListableBeanFactory.getBeansOfType() is as follows:

/**
 * Returns the first bean from the {@link Map} returned by
 * <code>ListableBeanFactory.getBeansOfType(type, true, true)</code>
 * which is an instances of {@link java.lang.reflect.Proxy}</code>.
 */
public static final Object getFirstProxiedBeanOfType(
								ListableBeanFactory factory, Class type) {
	Map names = factory.getBeansOfType(type, true, true);
	Iterator iter = names.keySet().iterator();
	while (iter.hasNext()) {
		String name = (String)iter.next();
		Object bean = factory.getBean(name);
		if (bean instanceof Proxy) {
			return bean;
		}
	}
	return null;
}

Affects: 1.1 RC1

Activity

  1. spring-projects-issues commented on Aug 12, 2004

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Thanks for reporting this - I've just fixed AbstractBeanFactory's "getSingletonNames" implementation to properly synchronize on the singletonCache Map.

    BTW, regarding your code example: You don't need to call getBean for each name returned by getBeansOfType. The latter returns a Map with bean names as keys and bean instances as values, so I recommend to simply fetch the bean instances from the Map.

    Juergen

  2. added
    type: bug
    in: core
    on Jan 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions