|
30 | 30 | import java.util.Map.Entry; |
31 | 31 | import java.util.Properties; |
32 | 32 | import java.util.Set; |
| 33 | +import java.util.concurrent.locks.ReentrantLock; |
33 | 34 | import java.util.stream.Stream; |
34 | 35 |
|
35 | 36 | import org.apache.commons.logging.Log; |
@@ -98,6 +99,8 @@ public class DefaultBinderFactory implements BinderFactory, DisposableBean, Appl |
98 | 99 |
|
99 | 100 | private volatile String defaultBinder; |
100 | 101 |
|
| 102 | + private static final ReentrantLock lock = new ReentrantLock(); |
| 103 | + |
101 | 104 | public DefaultBinderFactory(Map<String, BinderConfiguration> binderConfigurations, |
102 | 105 | BinderTypeRegistry binderTypeRegistry, BinderCustomizer binderCustomizer) { |
103 | 106 | this.binderConfigurations = new HashMap<>(binderConfigurations); |
@@ -142,34 +145,39 @@ public void destroy() { |
142 | 145 |
|
143 | 146 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
144 | 147 | @Override |
| 148 | + public <T> Binder<T, ?, ?> getBinder(String name, Class<? extends T> bindingTargetType) { |
| 149 | + lock.lock(); |
| 150 | + try { |
| 151 | + String binderName = StringUtils.hasText(name) ? name : this.defaultBinder; |
145 | 152 |
|
146 | | - public synchronized <T> Binder<T, ?, ?> getBinder(String name, Class<? extends T> bindingTargetType) { |
147 | | - String binderName = StringUtils.hasText(name) ? name : this.defaultBinder; |
148 | | - |
149 | | - Map<String, Binder> binders = this.context == null ? Collections.emptyMap() : this.context.getBeansOfType(Binder.class); |
150 | | - Binder<T, ConsumerProperties, ProducerProperties> binder; |
151 | | - if (StringUtils.hasText(binderName) && binders.containsKey(binderName)) { |
152 | | - binder = (Binder<T, ConsumerProperties, ProducerProperties>) this.context.getBean(binderName); |
153 | | - } |
154 | | - else if (binders.size() == 1) { |
155 | | - binder = binders.values().iterator().next(); |
156 | | - } |
157 | | - else if (binders.size() > 1) { |
158 | | - throw new IllegalStateException( |
| 153 | + Map<String, Binder> binders = this.context == null ? Collections.emptyMap() : this.context.getBeansOfType(Binder.class); |
| 154 | + Binder<T, ConsumerProperties, ProducerProperties> binder; |
| 155 | + if (StringUtils.hasText(binderName) && binders.containsKey(binderName)) { |
| 156 | + binder = (Binder<T, ConsumerProperties, ProducerProperties>) this.context.getBean(binderName); |
| 157 | + } |
| 158 | + else if (binders.size() == 1) { |
| 159 | + binder = binders.values().iterator().next(); |
| 160 | + } |
| 161 | + else if (binders.size() > 1) { |
| 162 | + throw new IllegalStateException( |
159 | 163 | "Multiple binders are available, however neither default nor " |
160 | | - + "per-destination binder name is provided. Available binders are " |
161 | | - + binders.keySet()); |
162 | | - } |
163 | | - else { |
164 | | - /* |
165 | | - * This is the fallback to the old bootstrap that relies on spring.binders. |
166 | | - */ |
167 | | - binder = this.doGetBinder(binderName, bindingTargetType); |
| 164 | + + "per-destination binder name is provided. Available binders are " |
| 165 | + + binders.keySet()); |
| 166 | + } |
| 167 | + else { |
| 168 | + /* |
| 169 | + * This is the fallback to the old bootstrap that relies on spring.binders. |
| 170 | + */ |
| 171 | + binder = this.doGetBinder(binderName, bindingTargetType); |
| 172 | + } |
| 173 | + if (this.binderCustomizer != null) { |
| 174 | + this.binderCustomizer.customize(binder, binderName); |
| 175 | + } |
| 176 | + return binder; |
168 | 177 | } |
169 | | - if (this.binderCustomizer != null) { |
170 | | - this.binderCustomizer.customize(binder, binderName); |
| 178 | + finally { |
| 179 | + lock.unlock(); |
171 | 180 | } |
172 | | - return binder; |
173 | 181 | } |
174 | 182 |
|
175 | 183 | private <T> Binder<T, ConsumerProperties, ProducerProperties> doGetBinder(String name, Class<? extends T> bindingTargetType) { |
|
0 commit comments