Closed
Description
Marcin Piela opened SPR-14269 and commented
When creating a context and then calling getBean
on it from multiple threads we sometimes get a BeanInitializationException
from RequiredAnnotationBeanPostProcessor.postProcessPropertyValues
.
Attached is a simple maven project to reproduce. In a loop it:
- creates a fresh spring context
- attempts to fetch some beans from it in parallel.
The number of threads and max number of loop iterations can be passed as the first and second argument to the Main class.
Usage:
mvn clean package && java -jar target/spring-bug-1.0-SNAPSHOT-jar-with-dependencies.jar
We never get any errors when the getBean method is called from a single thread, so:
java -jar target/spring-bug-1.0-SNAPSHOT-jar-with-dependencies.jar 1
always works.
We don't get any errors for spring version 4.1.9, but as long as we switch to 4.2.0 or later the errors start occuring.
Example exception:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service-2' defined in class path resource [example-context.xml]: Cannot resolve reference to bean 'bean_0_1' while setting bean property 'bean_0_1'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean_0_1' defined in URL [jar:file:/home/mpiela/devel/repo/spring-bug/target/spring-bug-1.0-SNAPSHOT-jar-with-dependencies.jar!/test/Bean_0_1.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'bean_1_0' and 'bean_1_1' are required for bean 'bean_0_1'
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:325)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054)
at test.Main.lambda$tryToGetBean$0(Main.java:75)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean_0_1' defined in URL [jar:file:/home/mpiela/devel/repo/spring-bug/target/spring-bug-1.0-SNAPSHOT-jar-with-dependencies.jar!/test/Bean_0_1.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'bean_1_0' and 'bean_1_1' are required for bean 'bean_0_1'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:325)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
... 13 more
Caused by: org.springframework.beans.factory.BeanInitializationException: Properties 'bean_1_0' and 'bean_1_1' are required for bean 'bean_0_1'
at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
... 17 more
Affects: 4.2.6
Attachments:
- spring-bug.tar.gz (2.25 kB)
Issue Links:
- Reduce BeanDefinition cloning in AbstractBeanFactory.getMergedBeanDefinition [SPR-12236] #16851 Reduce BeanDefinition cloning in AbstractBeanFactory.getMergedBeanDefinition
Referenced from: commits 9064d38, 71463fb, 933bbf2, 6efa058
Backported to: 4.2.7