RootBeanDefinition.java 13.2 KB
Newer Older
1
/*
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory.support;

19
import java.lang.reflect.AnnotatedElement;
20
import java.lang.reflect.Executable;
21
import java.lang.reflect.Member;
22
import java.lang.reflect.Method;
23
import java.util.HashSet;
24
import java.util.Set;
25
import java.util.function.Supplier;
26 27 28

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
29
import org.springframework.beans.factory.config.BeanDefinitionHolder;
30
import org.springframework.beans.factory.config.ConstructorArgumentValues;
31
import org.springframework.core.ResolvableType;
32
import org.springframework.util.Assert;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/**
 * A root bean definition represents the merged bean definition that backs
 * a specific bean in a Spring BeanFactory at runtime. It might have been created
 * from multiple original bean definitions that inherit from each other,
 * typically registered as {@link GenericBeanDefinition GenericBeanDefinitions}.
 * A root bean definition is essentially the 'unified' bean definition view at runtime.
 *
 * <p>Root bean definitions may also be used for registering individual bean definitions
 * in the configuration phase. However, since Spring 2.5, the preferred way to register
 * bean definitions programmatically is the {@link GenericBeanDefinition} class.
 * GenericBeanDefinition has the advantage that it allows to dynamically define
 * parent dependencies, not 'hard-coding' the role as a root bean definition.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see GenericBeanDefinition
 * @see ChildBeanDefinition
 */
P
Phillip Webb 已提交
52
@SuppressWarnings("serial")
53 54
public class RootBeanDefinition extends AbstractBeanDefinition {

55 56
	private BeanDefinitionHolder decoratedDefinition;

57
	private AnnotatedElement qualifiedElement;
58

59
	boolean allowCaching = true;
60

61
	boolean isFactoryMethodUnique = false;
62

63
	volatile ResolvableType targetType;
64

65 66 67 68
	/** Package-visible field for caching the determined Class of a given bean definition */
	volatile Class<?> resolvedTargetType;

	/** Package-visible field for caching the return type of a generically typed factory method */
69
	volatile ResolvableType factoryMethodReturnType;
70 71

	/** Common lock for the four constructor fields below */
72 73
	final Object constructorArgumentLock = new Object();

74
	/** Package-visible field for caching the resolved constructor or factory method */
75
	Executable resolvedConstructorOrFactoryMethod;
76 77 78

	/** Package-visible field that marks the constructor arguments as resolved */
	boolean constructorArgumentsResolved = false;
79 80

	/** Package-visible field for caching fully resolved constructor arguments */
81
	Object[] resolvedConstructorArguments;
82 83

	/** Package-visible field for caching partly prepared constructor arguments */
84
	Object[] preparedConstructorArguments;
85

86
	/** Common lock for the two post-processing fields below */
87
	final Object postProcessingLock = new Object();
88 89 90 91

	/** Package-visible field that indicates MergedBeanDefinitionPostProcessor having been applied */
	boolean postProcessed = false;

92 93
	/** Package-visible field that indicates a before-instantiation post-processor having kicked in */
	volatile Boolean beforeInstantiationResolved;
94

95 96 97 98 99 100
	private Set<Member> externallyManagedConfigMembers;

	private Set<String> externallyManagedInitMethods;

	private Set<String> externallyManagedDestroyMethods;

101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	/**
	 * Create a new RootBeanDefinition, to be configured through its bean
	 * properties and configuration methods.
	 * @see #setBeanClass
	 * @see #setBeanClassName
	 * @see #setScope
	 * @see #setAutowireMode
	 * @see #setDependencyCheck
	 * @see #setConstructorArgumentValues
	 * @see #setPropertyValues
	 */
	public RootBeanDefinition() {
		super();
	}

	/**
	 * Create a new RootBeanDefinition for a singleton.
	 * @param beanClass the class of the bean to instantiate
120
	 * @see #setBeanClass
121
	 */
122
	public RootBeanDefinition(Class<?> beanClass) {
123 124 125 126
		super();
		setBeanClass(beanClass);
	}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	/**
	 * Create a new RootBeanDefinition for a singleton bean, constructing each instance
	 * through calling the given supplier (possibly a lambda or method reference).
	 * @param beanClass the class of the bean to instantiate
	 * @param instanceSupplier the supplier to construct a bean instance,
	 * as an alternative to a declaratively specified factory method
	 * @since 5.0
	 * @see #setInstanceSupplier(Supplier)
	 */
	public <T> RootBeanDefinition(Class<T> beanClass, Supplier<T> instanceSupplier) {
		super();
		setBeanClass(beanClass);
		setInstanceSupplier(instanceSupplier);
	}

	/**
	 * Create a new RootBeanDefinition for a scoped bean, constructing each instance
	 * through calling the given supplier (possibly a lambda or method reference).
	 * @param beanClass the class of the bean to instantiate
	 * @param scope the name of the corresponding scope
	 * @param instanceSupplier the supplier to construct a bean instance,
	 * as an alternative to a declaratively specified factory method
	 * @since 5.0
	 * @see #setInstanceSupplier(Supplier)
	 */
	public <T> RootBeanDefinition(Class<T> beanClass, String scope, Supplier<T> instanceSupplier) {
		super();
		setBeanClass(beanClass);
		setScope(scope);
		setInstanceSupplier(instanceSupplier);
	}

159 160 161 162 163 164 165 166
	/**
	 * Create a new RootBeanDefinition for a singleton,
	 * using the given autowire mode.
	 * @param beanClass the class of the bean to instantiate
	 * @param autowireMode by name or type, using the constants in this interface
	 * @param dependencyCheck whether to perform a dependency check for objects
	 * (not applicable to autowiring a constructor, thus ignored there)
	 */
167
	public RootBeanDefinition(Class<?> beanClass, int autowireMode, boolean dependencyCheck) {
168 169 170 171
		super();
		setBeanClass(beanClass);
		setAutowireMode(autowireMode);
		if (dependencyCheck && getResolvedAutowireMode() != AUTOWIRE_CONSTRUCTOR) {
172
			setDependencyCheck(DEPENDENCY_CHECK_OBJECTS);
173 174 175 176 177 178 179 180 181 182
		}
	}

	/**
	 * Create a new RootBeanDefinition for a singleton,
	 * providing constructor arguments and property values.
	 * @param beanClass the class of the bean to instantiate
	 * @param cargs the constructor argument values to apply
	 * @param pvs the property values to apply
	 */
183
	public RootBeanDefinition(Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
184 185 186 187
		super(cargs, pvs);
		setBeanClass(beanClass);
	}

188 189 190 191 192 193 194 195 196 197
	/**
	 * Create a new RootBeanDefinition for a singleton,
	 * providing constructor arguments and property values.
	 * <p>Takes a bean class name to avoid eager loading of the bean class.
	 * @param beanClassName the name of the class to instantiate
	 */
	public RootBeanDefinition(String beanClassName) {
		setBeanClassName(beanClassName);
	}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	/**
	 * Create a new RootBeanDefinition for a singleton,
	 * providing constructor arguments and property values.
	 * <p>Takes a bean class name to avoid eager loading of the bean class.
	 * @param beanClassName the name of the class to instantiate
	 * @param cargs the constructor argument values to apply
	 * @param pvs the property values to apply
	 */
	public RootBeanDefinition(String beanClassName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
		super(cargs, pvs);
		setBeanClassName(beanClassName);
	}

	/**
	 * Create a new RootBeanDefinition as deep copy of the given
	 * bean definition.
	 * @param original the original bean definition to copy from
	 */
	public RootBeanDefinition(RootBeanDefinition original) {
217
		super(original);
218
		this.decoratedDefinition = original.decoratedDefinition;
219
		this.qualifiedElement = original.qualifiedElement;
220
		this.allowCaching = original.allowCaching;
221
		this.isFactoryMethodUnique = original.isFactoryMethodUnique;
222
		this.targetType = original.targetType;
223 224 225 226 227 228 229 230 231 232 233 234
	}

	/**
	 * Create a new RootBeanDefinition as deep copy of the given
	 * bean definition.
	 * @param original the original bean definition to copy from
	 */
	RootBeanDefinition(BeanDefinition original) {
		super(original);
	}


235
	@Override
236 237 238 239
	public String getParentName() {
		return null;
	}

240
	@Override
241 242 243 244 245 246
	public void setParentName(String parentName) {
		if (parentName != null) {
			throw new IllegalArgumentException("Root bean cannot be changed into a child bean with parent reference");
		}
	}

247 248 249 250 251 252 253 254 255 256 257 258 259 260
	/**
	 * Register a target definition that is being decorated by this bean definition.
	 */
	public void setDecoratedDefinition(BeanDefinitionHolder decoratedDefinition) {
		this.decoratedDefinition = decoratedDefinition;
	}

	/**
	 * Return the target definition that is being decorated by this bean definition, if any.
	 */
	public BeanDefinitionHolder getDecoratedDefinition() {
		return this.decoratedDefinition;
	}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	/**
	 * Specify the {@link AnnotatedElement} defining qualifiers,
	 * to be used instead of the target class or factory method.
	 * @since 4.3.3
	 * @see #setTargetType(ResolvableType)
	 * @see #getResolvedFactoryMethod()
	 */
	public void setQualifiedElement(AnnotatedElement qualifiedElement) {
		this.qualifiedElement = qualifiedElement;
	}

	/**
	 * Return the {@link AnnotatedElement} defining qualifiers, if any.
	 * Otherwise, the factory method and target class will be checked.
	 * @since 4.3.3
	 */
	public AnnotatedElement getQualifiedElement() {
		return this.qualifiedElement;
	}

281 282 283 284 285 286 287 288
	/**
	 * Specify a generics-containing target type of this bean definition, if known in advance.
	 * @since 4.3.3
	 */
	public void setTargetType(ResolvableType targetType) {
		this.targetType = targetType;
	}

289 290
	/**
	 * Specify the target type of this bean definition, if known in advance.
291
	 * @since 3.2.2
292 293
	 */
	public void setTargetType(Class<?> targetType) {
294
		this.targetType = (targetType != null ? ResolvableType.forClass(targetType) : null);
295 296 297 298 299
	}

	/**
	 * Return the target type of this bean definition, if known
	 * (either specified in advance or resolved on first instantiation).
300
	 * @since 3.2.2
301 302
	 */
	public Class<?> getTargetType() {
303 304 305 306
		if (this.resolvedTargetType != null) {
			return this.resolvedTargetType;
		}
		return (this.targetType != null ? this.targetType.resolve() : null);
307 308
	}

309 310 311 312 313 314 315 316 317
	/**
	 * Specify a factory method name that refers to a non-overloaded method.
	 */
	public void setUniqueFactoryMethodName(String name) {
		Assert.hasText(name, "Factory method name must not be empty");
		setFactoryMethodName(name);
		this.isFactoryMethodUnique = true;
	}

318 319 320
	/**
	 * Check whether the given candidate qualifies as a factory method.
	 */
321 322 323 324 325 326
	public boolean isFactoryMethod(Method candidate) {
		return (candidate != null && candidate.getName().equals(getFactoryMethodName()));
	}

	/**
	 * Return the resolved factory method as a Java Method object, if available.
327
	 * @return the factory method, or {@code null} if not found or not resolved yet
328 329
	 */
	public Method getResolvedFactoryMethod() {
330
		synchronized (this.constructorArgumentLock) {
331
			Executable candidate = this.resolvedConstructorOrFactoryMethod;
332 333
			return (candidate instanceof Method ? (Method) candidate : null);
		}
334 335
	}

336
	public void registerExternallyManagedConfigMember(Member configMember) {
337 338
		synchronized (this.postProcessingLock) {
			if (this.externallyManagedConfigMembers == null) {
339
				this.externallyManagedConfigMembers = new HashSet<>(1);
340 341 342
			}
			this.externallyManagedConfigMembers.add(configMember);
		}
343 344 345
	}

	public boolean isExternallyManagedConfigMember(Member configMember) {
346 347 348 349
		synchronized (this.postProcessingLock) {
			return (this.externallyManagedConfigMembers != null &&
					this.externallyManagedConfigMembers.contains(configMember));
		}
350 351 352
	}

	public void registerExternallyManagedInitMethod(String initMethod) {
353 354
		synchronized (this.postProcessingLock) {
			if (this.externallyManagedInitMethods == null) {
355
				this.externallyManagedInitMethods = new HashSet<>(1);
356 357 358
			}
			this.externallyManagedInitMethods.add(initMethod);
		}
359 360 361
	}

	public boolean isExternallyManagedInitMethod(String initMethod) {
362 363 364 365
		synchronized (this.postProcessingLock) {
			return (this.externallyManagedInitMethods != null &&
					this.externallyManagedInitMethods.contains(initMethod));
		}
366 367 368
	}

	public void registerExternallyManagedDestroyMethod(String destroyMethod) {
369 370
		synchronized (this.postProcessingLock) {
			if (this.externallyManagedDestroyMethods == null) {
371
				this.externallyManagedDestroyMethods = new HashSet<>(1);
372 373 374
			}
			this.externallyManagedDestroyMethods.add(destroyMethod);
		}
375 376 377
	}

	public boolean isExternallyManagedDestroyMethod(String destroyMethod) {
378 379 380 381
		synchronized (this.postProcessingLock) {
			return (this.externallyManagedDestroyMethods != null &&
					this.externallyManagedDestroyMethods.contains(destroyMethod));
		}
382 383
	}

384

385
	@Override
386
	public RootBeanDefinition cloneBeanDefinition() {
387 388 389
		return new RootBeanDefinition(this);
	}

390
	@Override
391 392 393 394
	public boolean equals(Object other) {
		return (this == other || (other instanceof RootBeanDefinition && super.equals(other)));
	}

395
	@Override
396 397 398 399 400
	public String toString() {
		return "Root bean: " + super.toString();
	}

}