|
40 | 40 | import java.util.Optional;
|
41 | 41 | import java.util.Set;
|
42 | 42 | import java.util.concurrent.ConcurrentHashMap;
|
| 43 | +import java.util.function.Consumer; |
| 44 | +import java.util.function.Predicate; |
43 | 45 | import java.util.stream.Stream;
|
44 | 46 | import javax.inject.Provider;
|
45 | 47 |
|
@@ -921,18 +923,14 @@ else if (!beanDefinition.equals(existingDefinition)) {
|
921 | 923 | updatedDefinitions.addAll(this.beanDefinitionNames);
|
922 | 924 | updatedDefinitions.add(beanName);
|
923 | 925 | this.beanDefinitionNames = updatedDefinitions;
|
924 |
| - if (this.manualSingletonNames.contains(beanName)) { |
925 |
| - Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames); |
926 |
| - updatedSingletons.remove(beanName); |
927 |
| - this.manualSingletonNames = updatedSingletons; |
928 |
| - } |
| 926 | + removeManualSingletonName(beanName); |
929 | 927 | }
|
930 | 928 | }
|
931 | 929 | else {
|
932 | 930 | // Still in startup registration phase
|
933 | 931 | this.beanDefinitionMap.put(beanName, beanDefinition);
|
934 | 932 | this.beanDefinitionNames.add(beanName);
|
935 |
| - this.manualSingletonNames.remove(beanName); |
| 933 | + removeManualSingletonName(beanName); |
936 | 934 | }
|
937 | 935 | this.frozenBeanDefinitionNames = null;
|
938 | 936 | }
|
@@ -1020,40 +1018,51 @@ protected boolean allowAliasOverriding() {
|
1020 | 1018 | @Override
|
1021 | 1019 | public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
|
1022 | 1020 | super.registerSingleton(beanName, singletonObject);
|
| 1021 | + updateManualSingletonNames(set -> set.add(beanName), set -> !this.beanDefinitionMap.containsKey(beanName)); |
| 1022 | + clearByTypeCache(); |
| 1023 | + } |
| 1024 | + |
| 1025 | + @Override |
| 1026 | + public void destroySingletons() { |
| 1027 | + super.destroySingletons(); |
| 1028 | + updateManualSingletonNames(Set::clear, set -> !set.isEmpty()); |
| 1029 | + clearByTypeCache(); |
| 1030 | + } |
| 1031 | + |
| 1032 | + @Override |
| 1033 | + public void destroySingleton(String beanName) { |
| 1034 | + super.destroySingleton(beanName); |
| 1035 | + removeManualSingletonName(beanName); |
| 1036 | + clearByTypeCache(); |
| 1037 | + } |
| 1038 | + |
| 1039 | + private void removeManualSingletonName(String beanName) { |
| 1040 | + updateManualSingletonNames(set -> set.remove(beanName), set -> set.contains(beanName)); |
| 1041 | + } |
1023 | 1042 |
|
| 1043 | + /** |
| 1044 | + * Update the factory's internal set of manual singleton names. |
| 1045 | + * @param action the modification action |
| 1046 | + * @param condition a precondition for the modification action |
| 1047 | + * (if this condition does not apply, the action can be skipped) |
| 1048 | + */ |
| 1049 | + private void updateManualSingletonNames(Consumer<Set<String>> action, Predicate<Set<String>> condition) { |
1024 | 1050 | if (hasBeanCreationStarted()) {
|
1025 | 1051 | // Cannot modify startup-time collection elements anymore (for stable iteration)
|
1026 | 1052 | synchronized (this.beanDefinitionMap) {
|
1027 |
| - if (!this.beanDefinitionMap.containsKey(beanName)) { |
1028 |
| - Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames.size() + 1); |
1029 |
| - updatedSingletons.addAll(this.manualSingletonNames); |
1030 |
| - updatedSingletons.add(beanName); |
| 1053 | + if (condition.test(this.manualSingletonNames)) { |
| 1054 | + Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames); |
| 1055 | + action.accept(updatedSingletons); |
1031 | 1056 | this.manualSingletonNames = updatedSingletons;
|
1032 | 1057 | }
|
1033 | 1058 | }
|
1034 | 1059 | }
|
1035 | 1060 | else {
|
1036 | 1061 | // Still in startup registration phase
|
1037 |
| - if (!this.beanDefinitionMap.containsKey(beanName)) { |
1038 |
| - this.manualSingletonNames.add(beanName); |
| 1062 | + if (condition.test(this.manualSingletonNames)) { |
| 1063 | + action.accept(this.manualSingletonNames); |
1039 | 1064 | }
|
1040 | 1065 | }
|
1041 |
| - |
1042 |
| - clearByTypeCache(); |
1043 |
| - } |
1044 |
| - |
1045 |
| - @Override |
1046 |
| - public void destroySingleton(String beanName) { |
1047 |
| - super.destroySingleton(beanName); |
1048 |
| - this.manualSingletonNames.remove(beanName); |
1049 |
| - clearByTypeCache(); |
1050 |
| - } |
1051 |
| - |
1052 |
| - @Override |
1053 |
| - public void destroySingletons() { |
1054 |
| - super.destroySingletons(); |
1055 |
| - this.manualSingletonNames.clear(); |
1056 |
| - clearByTypeCache(); |
1057 | 1066 | }
|
1058 | 1067 |
|
1059 | 1068 | /**
|
|
0 commit comments