fixed potential race condition through additional synchronization (SPR-5658)
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1069 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
6c3dd342fe
commit
d7836b9fcc
|
|
@ -396,6 +396,23 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the specified cached method argument or field value.
|
||||||
|
*/
|
||||||
|
private Object resolvedCachedArgument(String beanName, Object cachedArgument) {
|
||||||
|
if (cachedArgument instanceof DependencyDescriptor) {
|
||||||
|
DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument;
|
||||||
|
TypeConverter typeConverter = beanFactory.getTypeConverter();
|
||||||
|
return beanFactory.resolveDependency(descriptor, beanName, null, typeConverter);
|
||||||
|
}
|
||||||
|
else if (cachedArgument instanceof RuntimeBeanReference) {
|
||||||
|
return beanFactory.getBean(((RuntimeBeanReference) cachedArgument).getBeanName());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return cachedArgument;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class representing injection information about an annotated field.
|
* Class representing injection information about an annotated field.
|
||||||
|
|
@ -419,19 +436,11 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
try {
|
try {
|
||||||
Object value;
|
Object value;
|
||||||
if (this.cached) {
|
if (this.cached) {
|
||||||
if (this.cachedFieldValue instanceof DependencyDescriptor) {
|
value = resolvedCachedArgument(beanName, this.cachedFieldValue);
|
||||||
DependencyDescriptor descriptor = (DependencyDescriptor) this.cachedFieldValue;
|
|
||||||
TypeConverter typeConverter = beanFactory.getTypeConverter();
|
|
||||||
value = beanFactory.resolveDependency(descriptor, beanName, null, typeConverter);
|
|
||||||
}
|
|
||||||
else if (this.cachedFieldValue instanceof RuntimeBeanReference) {
|
|
||||||
value = beanFactory.getBean(((RuntimeBeanReference) this.cachedFieldValue).getBeanName());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
value = this.cachedFieldValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
synchronized (this) {
|
||||||
|
if (!this.cached) {
|
||||||
Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
|
Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
|
||||||
TypeConverter typeConverter = beanFactory.getTypeConverter();
|
TypeConverter typeConverter = beanFactory.getTypeConverter();
|
||||||
DependencyDescriptor descriptor = new DependencyDescriptor(field, this.required);
|
DependencyDescriptor descriptor = new DependencyDescriptor(field, this.required);
|
||||||
|
|
@ -453,6 +462,12 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
}
|
}
|
||||||
this.cached = true;
|
this.cached = true;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// Already cached in the meantime...
|
||||||
|
value = resolvedCachedArgument(beanName, this.cachedFieldValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
ReflectionUtils.makeAccessible(field);
|
ReflectionUtils.makeAccessible(field);
|
||||||
field.set(bean, value);
|
field.set(bean, value);
|
||||||
|
|
@ -492,27 +507,14 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
}
|
}
|
||||||
Method method = (Method) this.member;
|
Method method = (Method) this.member;
|
||||||
try {
|
try {
|
||||||
Object[] arguments = null;
|
Object[] arguments;
|
||||||
if (this.cached) {
|
if (this.cached) {
|
||||||
if (this.cachedMethodArguments != null) {
|
// Shortcut for avoiding synchronization...
|
||||||
arguments = new Object[this.cachedMethodArguments.length];
|
arguments = resolveCachedArguments(beanName);
|
||||||
for (int i = 0; i < arguments.length; i++) {
|
|
||||||
Object cachedArg = this.cachedMethodArguments[i];
|
|
||||||
if (cachedArg instanceof DependencyDescriptor) {
|
|
||||||
DependencyDescriptor descriptor = (DependencyDescriptor) cachedArg;
|
|
||||||
TypeConverter typeConverter = beanFactory.getTypeConverter();
|
|
||||||
arguments[i] = beanFactory.resolveDependency(descriptor, beanName, null, typeConverter);
|
|
||||||
}
|
|
||||||
else if (cachedArg instanceof RuntimeBeanReference) {
|
|
||||||
arguments[i] = beanFactory.getBean(((RuntimeBeanReference) cachedArg).getBeanName());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
arguments[i] = cachedArg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
synchronized (this) {
|
||||||
|
if (!this.cached) {
|
||||||
Class[] paramTypes = method.getParameterTypes();
|
Class[] paramTypes = method.getParameterTypes();
|
||||||
arguments = new Object[paramTypes.length];
|
arguments = new Object[paramTypes.length];
|
||||||
Set<String> autowiredBeanNames = new LinkedHashSet<String>(arguments.length);
|
Set<String> autowiredBeanNames = new LinkedHashSet<String>(arguments.length);
|
||||||
|
|
@ -552,6 +554,12 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
}
|
}
|
||||||
this.cached = true;
|
this.cached = true;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// Already cached in the meantime...
|
||||||
|
arguments = resolveCachedArguments(beanName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (this.skip == null) {
|
if (this.skip == null) {
|
||||||
if (this.pd != null && pvs instanceof MutablePropertyValues) {
|
if (this.pd != null && pvs instanceof MutablePropertyValues) {
|
||||||
((MutablePropertyValues) pvs).registerProcessedProperty(this.pd.getName());
|
((MutablePropertyValues) pvs).registerProcessedProperty(this.pd.getName());
|
||||||
|
|
@ -570,6 +578,17 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
throw new BeanCreationException("Could not autowire method: " + method, ex);
|
throw new BeanCreationException("Could not autowire method: " + method, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object[] resolveCachedArguments(String beanName) {
|
||||||
|
if (this.cachedMethodArguments == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Object[] arguments = new Object[this.cachedMethodArguments.length];
|
||||||
|
for (int i = 0; i < arguments.length; i++) {
|
||||||
|
arguments[i] = resolvedCachedArgument(beanName, this.cachedMethodArguments[i]);
|
||||||
|
}
|
||||||
|
return arguments;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue