diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java index 85b5de4a50..a0d7dc21dd 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java +++ b/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"); * 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 overrides = methodOverrides.getOverrides(); + synchronized (overrides) { + for (MethodOverride mo : overrides) { + prepareMethodOverride(mo); + } } } } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java index 05159bd78d..11b98753e9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java +++ b/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. @@ -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 overrides = new LinkedHashSet(0); + private final Set overrides = + Collections.synchronizedSet(new LinkedHashSet(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 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; }