revised constructor argument caching for highly concurrent creation scenarios (follow-up to SPR-7423)

This commit is contained in:
Juergen Hoeller 2010-08-18 09:08:55 +00:00
parent 9a088b8128
commit 9857ba077b
5 changed files with 101 additions and 63 deletions

View File

@ -880,8 +880,18 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
// Shortcut when re-creating the same bean...
if (mbd.resolvedConstructorOrFactoryMethod != null && args == null) {
if (mbd.constructorArgumentsResolved) {
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
if (resolved) {
if (autowireNecessary) {
return autowireConstructor(beanName, mbd, null, null);
}
else {

View File

@ -117,15 +117,21 @@ class ConstructorResolver {
argsToUse = explicitArgs;
}
else {
Object[] argsToResolve = null;
synchronized (mbd.constructorArgumentLock) {
constructorToUse = (Constructor) mbd.resolvedConstructorOrFactoryMethod;
if (constructorToUse != null) {
if (constructorToUse != null && mbd.constructorArgumentsResolved) {
// Found a cached constructor...
argsToUse = mbd.resolvedConstructorArguments;
if (argsToUse == null) {
argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse);
argsToResolve = mbd.preparedConstructorArguments;
}
}
}
if (argsToResolve != null) {
argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve);
}
}
if (constructorToUse == null) {
// Need to resolve the constructor.
@ -254,8 +260,7 @@ class ConstructorResolver {
}
if (explicitArgs == null) {
mbd.resolvedConstructorOrFactoryMethod = constructorToUse;
argsHolderToUse.storeCache(mbd);
argsHolderToUse.storeCache(mbd, constructorToUse);
}
}
@ -312,8 +317,10 @@ class ConstructorResolver {
}
}
}
synchronized (mbd.constructorArgumentLock) {
mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
}
}
/**
* Instantiate the bean using a named factory method. The method may be static, if the
@ -371,15 +378,21 @@ class ConstructorResolver {
argsToUse = explicitArgs;
}
else {
Object[] argsToResolve = null;
synchronized (mbd.constructorArgumentLock) {
factoryMethodToUse = (Method) mbd.resolvedConstructorOrFactoryMethod;
if (factoryMethodToUse != null) {
if (factoryMethodToUse != null && mbd.constructorArgumentsResolved) {
// Found a cached factory method...
argsToUse = mbd.resolvedConstructorArguments;
if (argsToUse == null && mbd.preparedConstructorArguments != null) {
argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse);
if (argsToUse == null) {
argsToResolve = mbd.preparedConstructorArguments;
}
}
}
if (argsToResolve != null) {
argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve);
}
}
if (factoryMethodToUse == null || argsToUse == null) {
// Need to determine the factory method...
@ -536,8 +549,7 @@ class ConstructorResolver {
}
if (explicitArgs == null) {
mbd.resolvedConstructorOrFactoryMethod = factoryMethodToUse;
argsHolderToUse.storeCache(mbd);
argsHolderToUse.storeCache(mbd, factoryMethodToUse);
}
}
@ -734,11 +746,10 @@ class ConstructorResolver {
* Resolve the prepared arguments stored in the given bean definition.
*/
private Object[] resolvePreparedArguments(
String beanName, RootBeanDefinition mbd, BeanWrapper bw, Member methodOrCtor) {
String beanName, RootBeanDefinition mbd, BeanWrapper bw, Member methodOrCtor, Object[] argsToResolve) {
Class[] paramTypes = (methodOrCtor instanceof Method ?
((Method) methodOrCtor).getParameterTypes() : ((Constructor) methodOrCtor).getParameterTypes());
Object[] argsToResolve = mbd.preparedConstructorArguments;
TypeConverter converter = (this.beanFactory.getCustomTypeConverter() != null ?
this.beanFactory.getCustomTypeConverter() : bw);
BeanDefinitionValueResolver valueResolver =
@ -789,11 +800,11 @@ class ConstructorResolver {
*/
private static class ArgumentsHolder {
public Object rawArguments[];
public final Object rawArguments[];
public Object arguments[];
public final Object arguments[];
public Object preparedArguments[];
public final Object preparedArguments[];
public boolean resolveNecessary = false;
@ -833,14 +844,17 @@ class ConstructorResolver {
return Integer.MAX_VALUE - 1024;
}
public void storeCache(RootBeanDefinition mbd) {
public void storeCache(RootBeanDefinition mbd, Object constructorOrFactoryMethod) {
synchronized (mbd.constructorArgumentLock) {
mbd.resolvedConstructorOrFactoryMethod = constructorOrFactoryMethod;
mbd.constructorArgumentsResolved = true;
if (this.resolveNecessary) {
mbd.preparedConstructorArguments = this.preparedArguments;
}
else {
mbd.resolvedConstructorArguments = this.arguments;
}
mbd.constructorArgumentsResolved = true;
}
}
}

View File

@ -504,9 +504,15 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
*/
protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd, DependencyDescriptor descriptor) {
resolveBeanClass(mbd, beanName);
if (mbd.isFactoryMethodUnique && mbd.resolvedConstructorOrFactoryMethod == null) {
if (mbd.isFactoryMethodUnique) {
boolean resolve;
synchronized (mbd.constructorArgumentLock) {
resolve = (mbd.resolvedConstructorOrFactoryMethod == null);
}
if (resolve) {
new ConstructorResolver(this).resolveFactoryMethodIfPossible(mbd);
}
}
return getAutowireCandidateResolver().isAutowireCandidate(
new BeanDefinitionHolder(mbd, beanName, getAliases(beanName)), descriptor);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -59,16 +59,18 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
boolean isFactoryMethodUnique;
/** Package-visible field for caching the resolved constructor or factory method */
volatile Object resolvedConstructorOrFactoryMethod;
/** Package-visible field for caching fully resolved constructor arguments */
volatile Object[] resolvedConstructorArguments;
/** Package-visible field for caching partly prepared constructor arguments */
volatile Object[] preparedConstructorArguments;
Object resolvedConstructorOrFactoryMethod;
/** Package-visible field that marks the constructor arguments as resolved */
volatile boolean constructorArgumentsResolved = false;
boolean constructorArgumentsResolved = false;
/** Package-visible field for caching fully resolved constructor arguments */
Object[] resolvedConstructorArguments;
/** Package-visible field for caching partly prepared constructor arguments */
Object[] preparedConstructorArguments;
final Object constructorArgumentLock = new Object();
/** Package-visible field that indicates a before-instantiation post-processor having kicked in */
volatile Boolean beforeInstantiationResolved;
@ -78,6 +80,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
final Object postProcessingLock = new Object();
/**
* Create a new RootBeanDefinition, to be configured through its bean
* properties and configuration methods.
@ -264,9 +267,11 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @return the factory method, or <code>null</code> if not found or not resolved yet
*/
public Method getResolvedFactoryMethod() {
synchronized (this.constructorArgumentLock) {
Object candidate = this.resolvedConstructorOrFactoryMethod;
return (candidate instanceof Method ? (Method) candidate : null);
}
}
public void registerExternallyManagedConfigMember(Member configMember) {

View File

@ -45,7 +45,9 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy {
public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {
// Don't override the class with CGLIB if no overrides.
if (beanDefinition.getMethodOverrides().isEmpty()) {
Constructor<?> constructorToUse = (Constructor<?>) beanDefinition.resolvedConstructorOrFactoryMethod;
Constructor<?> constructorToUse;
synchronized (beanDefinition.constructorArgumentLock) {
constructorToUse = (Constructor<?>) beanDefinition.resolvedConstructorOrFactoryMethod;
if (constructorToUse == null) {
final Class clazz = beanDefinition.getBeanClass();
if (clazz.isInterface()) {
@ -68,6 +70,7 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy {
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
}
}
}
return BeanUtils.instantiateClass(constructorToUse);
}
else {