Browse Source

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

Issue: SPR-14333
pull/1074/head
Juergen Hoeller 10 years ago
parent
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

11
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * Return information about methods to be overridden by the IoC
* container. This will be empty if there are no method overrides. * container. This will be empty if there are no method overrides.
* Never returns null. * Never returns {@code null}.
*/ */
public MethodOverrides getMethodOverrides() { public MethodOverrides getMethodOverrides() {
return this.methodOverrides; return this.methodOverrides;
@ -934,8 +934,11 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
// Check that lookup methods exists. // Check that lookup methods exists.
MethodOverrides methodOverrides = getMethodOverrides(); MethodOverrides methodOverrides = getMethodOverrides();
if (!methodOverrides.isEmpty()) { if (!methodOverrides.isEmpty()) {
for (MethodOverride mo : methodOverrides.getOverrides()) { Set<MethodOverride> overrides = methodOverrides.getOverrides();
prepareMethodOverride(mo); synchronized (overrides) {
for (MethodOverride mo : overrides) {
prepareMethodOverride(mo);
}
} }
} }
} }

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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.beans.factory.support; package org.springframework.beans.factory.support;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -34,7 +35,10 @@ import java.util.Set;
*/ */
public class MethodOverrides { 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) { public void addOverrides(MethodOverrides other) {
if (other != null) { 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. * Add the given method override.
*/ */
public void addOverride(MethodOverride override) { public void addOverride(MethodOverride override) {
this.modified = true;
this.overrides.add(override); this.overrides.add(override);
} }
@ -73,6 +79,7 @@ public class MethodOverrides {
* @see MethodOverride * @see MethodOverride
*/ */
public Set<MethodOverride> getOverrides() { public Set<MethodOverride> getOverrides() {
this.modified = true;
return this.overrides; return this.overrides;
} }
@ -80,7 +87,7 @@ public class MethodOverrides {
* Return whether the set of method overrides is empty. * Return whether the set of method overrides is empty.
*/ */
public boolean isEmpty() { 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 * @return the method override, or {@code null} if none
*/ */
public MethodOverride getOverride(Method method) { public MethodOverride getOverride(Method method) {
MethodOverride match = null; if (!this.modified) {
for (MethodOverride candidate : this.overrides) { return null;
if (candidate.matches(method)) { }
match = candidate; synchronized (this.overrides) {
MethodOverride match = null;
for (MethodOverride candidate : this.overrides) {
if (candidate.matches(method)) {
match = candidate;
}
} }
return match;
} }
return match;
} }

Loading…
Cancel
Save