Synchronized access to method overrides (in particular for @Lookup)

Issue: SPR-14333
master
Juergen Hoeller 8 years ago
parent e7561b1d7c
commit 9131ebbea6
  1. 11
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
  2. 30
      spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -731,7 +731,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/**
* Return information about methods to be overridden by the IoC
* container. This will be empty if there are no method overrides.
* Never returns null.
* Never returns {@code null}.
*/
public MethodOverrides getMethodOverrides() {
return this.methodOverrides;
@ -934,8 +934,11 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
// Check that lookup methods exists.
MethodOverrides methodOverrides = getMethodOverrides();
if (!methodOverrides.isEmpty()) {
for (MethodOverride mo : methodOverrides.getOverrides()) {
prepareMethodOverride(mo);
Set<MethodOverride> overrides = methodOverrides.getOverrides();
synchronized (overrides) {
for (MethodOverride mo : overrides) {
prepareMethodOverride(mo);
}
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -17,6 +17,7 @@
package org.springframework.beans.factory.support;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
@ -34,7 +35,10 @@ import java.util.Set;
*/
public class MethodOverrides {
private final Set<MethodOverride> overrides = new LinkedHashSet<MethodOverride>(0);
private final Set<MethodOverride> overrides =
Collections.synchronizedSet(new LinkedHashSet<MethodOverride>(0));
private volatile boolean modified = false;
/**
@ -56,7 +60,8 @@ public class MethodOverrides {
*/
public void addOverrides(MethodOverrides other) {
if (other != null) {
this.overrides.addAll(other.getOverrides());
this.modified = true;
this.overrides.addAll(other.overrides);
}
}
@ -64,6 +69,7 @@ public class MethodOverrides {
* Add the given method override.
*/
public void addOverride(MethodOverride override) {
this.modified = true;
this.overrides.add(override);
}
@ -73,6 +79,7 @@ public class MethodOverrides {
* @see MethodOverride
*/
public Set<MethodOverride> getOverrides() {
this.modified = true;
return this.overrides;
}
@ -80,7 +87,7 @@ public class MethodOverrides {
* Return whether the set of method overrides is empty.
*/
public boolean isEmpty() {
return this.overrides.isEmpty();
return (!this.modified || this.overrides.isEmpty());
}
/**
@ -89,13 +96,18 @@ public class MethodOverrides {
* @return the method override, or {@code null} if none
*/
public MethodOverride getOverride(Method method) {
MethodOverride match = null;
for (MethodOverride candidate : this.overrides) {
if (candidate.matches(method)) {
match = candidate;
if (!this.modified) {
return null;
}
synchronized (this.overrides) {
MethodOverride match = null;
for (MethodOverride candidate : this.overrides) {
if (candidate.matches(method)) {
match = candidate;
}
}
return match;
}
return match;
}

Loading…
Cancel
Save