Skip to content

Commit 31cda09

Browse files
committed
Avoid full synchronization in refreshable getBeanFactory() implementation
Closes spring-projectsgh-25219
1 parent 16d4e0e commit 31cda09

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableApplicationContext.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,10 +68,7 @@ public abstract class AbstractRefreshableApplicationContext extends AbstractAppl
6868
private Boolean allowCircularReferences;
6969

7070
/** Bean factory for this context */
71-
private DefaultListableBeanFactory beanFactory;
72-
73-
/** Synchronization monitor for the internal BeanFactory */
74-
private final Object beanFactoryMonitor = new Object();
71+
private volatile DefaultListableBeanFactory beanFactory;
7572

7673

7774
/**
@@ -127,9 +124,7 @@ protected final void refreshBeanFactory() throws BeansException {
127124
beanFactory.setSerializationId(getId());
128125
customizeBeanFactory(beanFactory);
129126
loadBeanDefinitions(beanFactory);
130-
synchronized (this.beanFactoryMonitor) {
131-
this.beanFactory = beanFactory;
132-
}
127+
this.beanFactory = beanFactory;
133128
}
134129
catch (IOException ex) {
135130
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
@@ -138,17 +133,18 @@ protected final void refreshBeanFactory() throws BeansException {
138133

139134
@Override
140135
protected void cancelRefresh(BeansException ex) {
141-
synchronized (this.beanFactoryMonitor) {
142-
if (this.beanFactory != null)
143-
this.beanFactory.setSerializationId(null);
136+
DefaultListableBeanFactory beanFactory = this.beanFactory;
137+
if (beanFactory != null) {
138+
beanFactory.setSerializationId(null);
144139
}
145140
super.cancelRefresh(ex);
146141
}
147142

148143
@Override
149144
protected final void closeBeanFactory() {
150-
synchronized (this.beanFactoryMonitor) {
151-
this.beanFactory.setSerializationId(null);
145+
DefaultListableBeanFactory beanFactory = this.beanFactory;
146+
if (beanFactory != null) {
147+
beanFactory.setSerializationId(null);
152148
this.beanFactory = null;
153149
}
154150
}
@@ -158,20 +154,17 @@ protected final void closeBeanFactory() {
158154
* i.e. has been refreshed at least once and not been closed yet.
159155
*/
160156
protected final boolean hasBeanFactory() {
161-
synchronized (this.beanFactoryMonitor) {
162-
return (this.beanFactory != null);
163-
}
157+
return (this.beanFactory != null);
164158
}
165159

166160
@Override
167161
public final ConfigurableListableBeanFactory getBeanFactory() {
168-
synchronized (this.beanFactoryMonitor) {
169-
if (this.beanFactory == null) {
170-
throw new IllegalStateException("BeanFactory not initialized or already closed - " +
171-
"call 'refresh' before accessing beans via the ApplicationContext");
172-
}
173-
return this.beanFactory;
162+
DefaultListableBeanFactory beanFactory = this.beanFactory;
163+
if (beanFactory == null) {
164+
throw new IllegalStateException("BeanFactory not initialized or already closed - " +
165+
"call 'refresh' before accessing beans via the ApplicationContext");
174166
}
167+
return beanFactory;
175168
}
176169

177170
/**

0 commit comments

Comments
 (0)