Skip to content

Commit 9131ebb

Browse files
committed
Synchronized access to method overrides (in particular for @lookup)
Issue: SPR-14333
1 parent e7561b1 commit 9131ebb

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -731,7 +731,7 @@ public void setMethodOverrides(MethodOverrides methodOverrides) {
731731
/**
732732
* Return information about methods to be overridden by the IoC
733733
* container. This will be empty if there are no method overrides.
734-
* Never returns null.
734+
* Never returns {@code null}.
735735
*/
736736
public MethodOverrides getMethodOverrides() {
737737
return this.methodOverrides;
@@ -934,8 +934,11 @@ public void prepareMethodOverrides() throws BeanDefinitionValidationException {
934934
// Check that lookup methods exists.
935935
MethodOverrides methodOverrides = getMethodOverrides();
936936
if (!methodOverrides.isEmpty()) {
937-
for (MethodOverride mo : methodOverrides.getOverrides()) {
938-
prepareMethodOverride(mo);
937+
Set<MethodOverride> overrides = methodOverrides.getOverrides();
938+
synchronized (overrides) {
939+
for (MethodOverride mo : overrides) {
940+
prepareMethodOverride(mo);
941+
}
939942
}
940943
}
941944
}

spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.beans.factory.support;
1818

1919
import java.lang.reflect.Method;
20+
import java.util.Collections;
2021
import java.util.LinkedHashSet;
2122
import java.util.Set;
2223

@@ -34,7 +35,10 @@
3435
*/
3536
public class MethodOverrides {
3637

37-
private final Set<MethodOverride> overrides = new LinkedHashSet<MethodOverride>(0);
38+
private final Set<MethodOverride> overrides =
39+
Collections.synchronizedSet(new LinkedHashSet<MethodOverride>(0));
40+
41+
private volatile boolean modified = false;
3842

3943

4044
/**
@@ -56,14 +60,16 @@ public MethodOverrides(MethodOverrides other) {
5660
*/
5761
public void addOverrides(MethodOverrides other) {
5862
if (other != null) {
59-
this.overrides.addAll(other.getOverrides());
63+
this.modified = true;
64+
this.overrides.addAll(other.overrides);
6065
}
6166
}
6267

6368
/**
6469
* Add the given method override.
6570
*/
6671
public void addOverride(MethodOverride override) {
72+
this.modified = true;
6773
this.overrides.add(override);
6874
}
6975

@@ -73,14 +79,15 @@ public void addOverride(MethodOverride override) {
7379
* @see MethodOverride
7480
*/
7581
public Set<MethodOverride> getOverrides() {
82+
this.modified = true;
7683
return this.overrides;
7784
}
7885

7986
/**
8087
* Return whether the set of method overrides is empty.
8188
*/
8289
public boolean isEmpty() {
83-
return this.overrides.isEmpty();
90+
return (!this.modified || this.overrides.isEmpty());
8491
}
8592

8693
/**
@@ -89,13 +96,18 @@ public boolean isEmpty() {
8996
* @return the method override, or {@code null} if none
9097
*/
9198
public MethodOverride getOverride(Method method) {
92-
MethodOverride match = null;
93-
for (MethodOverride candidate : this.overrides) {
94-
if (candidate.matches(method)) {
95-
match = candidate;
99+
if (!this.modified) {
100+
return null;
101+
}
102+
synchronized (this.overrides) {
103+
MethodOverride match = null;
104+
for (MethodOverride candidate : this.overrides) {
105+
if (candidate.matches(method)) {
106+
match = candidate;
107+
}
96108
}
109+
return match;
97110
}
98-
return match;
99111
}
100112

101113

0 commit comments

Comments
 (0)