Skip to content

Commit 7fc1629

Browse files
committed
Fixed potential race condition in concurrent calling of autowired methods on a prototype bean
Autowired methods might have been skipped on subsequent creation of further bean instances due to the 'skip' flag set to false outside of the synchronized block, with another thread entering the block and setting the flag to true in the meantime. Issue: SPR-9806
1 parent e0bec67 commit 7fc1629

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public InjectionMetadata(Class targetClass, Collection<InjectedElement> elements
6969
}
7070

7171
public void checkConfigMembers(RootBeanDefinition beanDefinition) {
72-
synchronized(this.injectedElements) {
72+
synchronized (this.injectedElements) {
7373
for (Iterator<InjectedElement> it = this.injectedElements.iterator(); it.hasNext();) {
7474
Member member = it.next().getMember();
7575
if (!beanDefinition.isExternallyManagedConfigMember(member)) {
@@ -175,26 +175,30 @@ protected void inject(Object target, String requestingBeanName, PropertyValues p
175175
* affected property as processed for other processors to ignore it.
176176
*/
177177
protected boolean checkPropertySkipping(PropertyValues pvs) {
178-
if (this.skip == null) {
179-
if (pvs != null) {
180-
synchronized (pvs) {
181-
if (this.skip == null) {
182-
if (this.pd != null) {
183-
if (pvs.contains(this.pd.getName())) {
184-
// Explicit value provided as part of the bean definition.
185-
this.skip = true;
186-
return true;
187-
}
188-
else if (pvs instanceof MutablePropertyValues) {
189-
((MutablePropertyValues) pvs).registerProcessedProperty(this.pd.getName());
190-
}
191-
}
192-
}
178+
if (this.skip != null) {
179+
return this.skip;
180+
}
181+
if (pvs == null) {
182+
this.skip = false;
183+
return false;
184+
}
185+
synchronized (pvs) {
186+
if (this.skip != null) {
187+
return this.skip;
188+
}
189+
if (this.pd != null) {
190+
if (pvs.contains(this.pd.getName())) {
191+
// Explicit value provided as part of the bean definition.
192+
this.skip = true;
193+
return true;
194+
}
195+
else if (pvs instanceof MutablePropertyValues) {
196+
((MutablePropertyValues) pvs).registerProcessedProperty(this.pd.getName());
193197
}
194198
}
195199
this.skip = false;
200+
return false;
196201
}
197-
return this.skip;
198202
}
199203

200204
/**

0 commit comments

Comments
 (0)