You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ConcurrentModificationException in AbstractApplicationContext.getBeansOfType() [SPR-259] #4990
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;
}
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.
Activity
spring-projects-issues commentedon Aug 12, 2004
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