|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -60,7 +60,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSour
|
60 | 60 | private String targetBeanName;
|
61 | 61 |
|
62 | 62 | /** Class of the target */
|
63 |
| - private Class<?> targetClass; |
| 63 | + private volatile Class<?> targetClass; |
64 | 64 |
|
65 | 65 | /**
|
66 | 66 | * BeanFactory that owns this TargetSource. We need to hold onto this
|
@@ -120,19 +120,28 @@ public BeanFactory getBeanFactory() {
|
120 | 120 |
|
121 | 121 |
|
122 | 122 | @Override
|
123 |
| - public synchronized Class<?> getTargetClass() { |
124 |
| - if (this.targetClass == null && this.beanFactory != null) { |
125 |
| - // Determine type of the target bean. |
126 |
| - this.targetClass = this.beanFactory.getType(this.targetBeanName); |
127 |
| - if (this.targetClass == null) { |
128 |
| - if (logger.isTraceEnabled()) { |
129 |
| - logger.trace("Getting bean with name '" + this.targetBeanName + "' in order to determine type"); |
| 123 | + public Class<?> getTargetClass() { |
| 124 | + Class<?> targetClass = this.targetClass; |
| 125 | + if (targetClass != null) { |
| 126 | + return targetClass; |
| 127 | + } |
| 128 | + synchronized (this) { |
| 129 | + // Full check within synchronization, entering the BeanFactory interaction algorithm only once... |
| 130 | + targetClass = this.targetClass; |
| 131 | + if (targetClass == null && this.beanFactory != null) { |
| 132 | + // Determine type of the target bean. |
| 133 | + targetClass = this.beanFactory.getType(this.targetBeanName); |
| 134 | + if (targetClass == null) { |
| 135 | + if (logger.isTraceEnabled()) { |
| 136 | + logger.trace("Getting bean with name '" + this.targetBeanName + "' for type determination"); |
| 137 | + } |
| 138 | + Object beanInstance = this.beanFactory.getBean(this.targetBeanName); |
| 139 | + targetClass = beanInstance.getClass(); |
130 | 140 | }
|
131 |
| - Object beanInstance = this.beanFactory.getBean(this.targetBeanName); |
132 |
| - this.targetClass = beanInstance.getClass(); |
| 141 | + this.targetClass = targetClass; |
133 | 142 | }
|
| 143 | + return targetClass; |
134 | 144 | }
|
135 |
| - return this.targetClass; |
136 | 145 | }
|
137 | 146 |
|
138 | 147 | @Override
|
|
0 commit comments