Browse Source

Add convenience factory method for Managed[List|Set|Map]

Closes gh-28026
pull/28119/head
Stephane Nicoll 4 years ago
parent
commit
d2c7dfb79e
  1. 17
      spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedList.java
  2. 22
      spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedMap.java
  3. 17
      spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedSet.java
  4. 8
      spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
  5. 21
      spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java
  6. 26
      spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java
  7. 24
      spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java
  8. 25
      spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java
  9. 6
      spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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 @@ @@ -17,6 +17,7 @@
package org.springframework.beans.factory.support;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.BeanMetadataElement;
@ -53,6 +54,20 @@ public class ManagedList<E> extends ArrayList<E> implements Mergeable, BeanMetad @@ -53,6 +54,20 @@ public class ManagedList<E> extends ArrayList<E> implements Mergeable, BeanMetad
}
/**
* Return a new instance containing an arbitrary number of elements.
* @param elements the elements to be contained in the list
* @param <E> the {@code List}'s element type
* @return a {@code List} containing the specified elements
* @since 5.3.16
*/
@SuppressWarnings("unchecked")
public static <E> ManagedList<E> of(E... elements) {
ManagedList<E> list = new ManagedList<>();
list.addAll(Arrays.asList(elements));
return list;
}
/**
* Set the configuration source {@code Object} for this metadata element.
* <p>The exact type of the object will depend on the configuration mechanism used.

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@ -18,6 +18,7 @@ package org.springframework.beans.factory.support; @@ -18,6 +18,7 @@ package org.springframework.beans.factory.support;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.Mergeable;
@ -56,6 +57,25 @@ public class ManagedMap<K, V> extends LinkedHashMap<K, V> implements Mergeable, @@ -56,6 +57,25 @@ public class ManagedMap<K, V> extends LinkedHashMap<K, V> implements Mergeable,
}
/**
* Return a new instance containing keys and values extracted from the
* given entries. The entries themselves are not stored in the map.
* @param entries {@code Map.Entry}s containing the keys and values
* from which the map is populated
* @param <K> the {@code Map}'s key type
* @param <V> the {@code Map}'s value type
* @return a {@code Map} containing the specified mappings
* @since 5.3.16
*/
@SuppressWarnings("unchecked")
public static <K,V> ManagedMap<K,V> ofEntries(Entry<? extends K, ? extends V>... entries) {
ManagedMap<K,V > map = new ManagedMap<>();
for (Entry<? extends K, ? extends V> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
/**
* Set the configuration source {@code Object} for this metadata element.
* <p>The exact type of the object will depend on the configuration mechanism used.

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.beans.factory.support;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
@ -52,6 +53,20 @@ public class ManagedSet<E> extends LinkedHashSet<E> implements Mergeable, BeanMe @@ -52,6 +53,20 @@ public class ManagedSet<E> extends LinkedHashSet<E> implements Mergeable, BeanMe
}
/**
* Return a new instance containing an arbitrary number of elements.
* @param elements the elements to be contained in the set
* @param <E> the {@code Set}'s element type
* @return a {@code Set} containing the specified elements
* @since 5.3.16
*/
@SuppressWarnings("unchecked")
public static <E> ManagedSet<E> of(E... elements) {
ManagedSet<E> set = new ManagedSet<>();
set.addAll(Arrays.asList(elements));
return set;
}
/**
* Set the configuration source {@code Object} for this metadata element.
* <p>The exact type of the object will depend on the configuration mechanism used.

8
spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

@ -2298,9 +2298,7 @@ class DefaultListableBeanFactoryTests { @@ -2298,9 +2298,7 @@ class DefaultListableBeanFactoryTests {
@Test
void prototypeWithArrayConversionForConstructor() {
List<String> list = new ManagedList<>();
list.add("myName");
list.add("myBeanName");
List<String> list = ManagedList.of("myName", "myBeanName");
RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bd.getConstructorArgumentValues().addGenericArgumentValue(list);
@ -2316,9 +2314,7 @@ class DefaultListableBeanFactoryTests { @@ -2316,9 +2314,7 @@ class DefaultListableBeanFactoryTests {
@Test
void prototypeWithArrayConversionForFactoryMethod() {
List<String> list = new ManagedList<>();
list.add("myName");
list.add("myBeanName");
List<String> list = ManagedList.of("myName", "myBeanName");
RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bd.setFactoryMethodName("create");

21
spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.beans.factory.config;
import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -357,22 +358,18 @@ public class PropertyResourceConfigurerTests { @@ -357,22 +358,18 @@ public class PropertyResourceConfigurerTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("stringArray", new String[] {"${os.name}", "${age}"});
List<Object> friends = new ManagedList<>();
friends.add("na${age}me");
friends.add(new RuntimeBeanReference("${ref}"));
List<Object> friends = ManagedList.of("na${age}me", new RuntimeBeanReference("${ref}"));
pvs.add("friends", friends);
Set<Object> someSet = new ManagedSet<>();
someSet.add("na${age}me");
someSet.add(new RuntimeBeanReference("${ref}"));
someSet.add(new TypedStringValue("${age}", Integer.class));
Set<Object> someSet = ManagedSet.of("na${age}me",
new RuntimeBeanReference("${ref}"), new TypedStringValue("${age}", Integer.class));
pvs.add("someSet", someSet);
Map<Object, Object> someMap = new ManagedMap<>();
someMap.put(new TypedStringValue("key${age}"), new TypedStringValue("${age}"));
someMap.put(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}"));
someMap.put("key1", new RuntimeBeanReference("${ref}"));
someMap.put("key2", "${age}name");
Map<Object, Object> someMap = ManagedMap.ofEntries(
new SimpleEntry<>(new TypedStringValue("key${age}"), new TypedStringValue("${age}")),
new SimpleEntry<>(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")),
new SimpleEntry<>("key1", new RuntimeBeanReference("${ref}")),
new SimpleEntry<>("key2", "${age}name"));
MutablePropertyValues innerPvs = new MutablePropertyValues();
innerPvs.add("country", "${os.name}");
RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);

26
spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -34,11 +34,8 @@ public class ManagedListTests { @@ -34,11 +34,8 @@ public class ManagedListTests {
@Test
public void mergeSunnyDay() {
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList child = new ManagedList();
child.add("three");
ManagedList parent = ManagedList.of("one", "two");
ManagedList child = ManagedList.of("three");
child.setMergeEnabled(true);
List mergedList = child.merge(parent);
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);
@ -46,8 +43,7 @@ public class ManagedListTests { @@ -46,8 +43,7 @@ public class ManagedListTests {
@Test
public void mergeWithNullParent() {
ManagedList child = new ManagedList();
child.add("one");
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child);
}
@ -61,8 +57,7 @@ public class ManagedListTests { @@ -61,8 +57,7 @@ public class ManagedListTests {
@Test
public void mergeWithNonCompatibleParentType() {
ManagedList child = new ManagedList();
child.add("one");
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() ->
child.merge("hello"));
@ -70,9 +65,7 @@ public class ManagedListTests { @@ -70,9 +65,7 @@ public class ManagedListTests {
@Test
public void mergeEmptyChild() {
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList parent = ManagedList.of("one", "two");
ManagedList child = new ManagedList();
child.setMergeEnabled(true);
List mergedList = child.merge(parent);
@ -82,11 +75,8 @@ public class ManagedListTests { @@ -82,11 +75,8 @@ public class ManagedListTests {
@Test
public void mergeChildValuesOverrideTheParents() {
// doesn't make much sense in the context of a list...
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList child = new ManagedList();
child.add("one");
ManagedList parent = ManagedList.of("one", "two");
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
List mergedList = child.merge(parent);
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);

24
spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.beans.factory.support;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import org.junit.jupiter.api.Test;
@ -34,11 +35,9 @@ public class ManagedMapTests { @@ -34,11 +35,9 @@ public class ManagedMapTests {
@Test
public void mergeSunnyDay() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap child = new ManagedMap();
child.put("three", "three");
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
new SimpleEntry<>("two", "two"));
ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("tree", "three"));
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(3);
@ -67,9 +66,8 @@ public class ManagedMapTests { @@ -67,9 +66,8 @@ public class ManagedMapTests {
@Test
public void mergeEmptyChild() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
new SimpleEntry<>("two", "two"));
ManagedMap child = new ManagedMap();
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
@ -78,11 +76,9 @@ public class ManagedMapTests { @@ -78,11 +76,9 @@ public class ManagedMapTests {
@Test
public void mergeChildValuesOverrideTheParents() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap child = new ManagedMap();
child.put("one", "fork");
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
new SimpleEntry<>("two", "two"));
ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("one", "fork"));
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
// child value for 'one' must override parent value...

25
spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -34,10 +34,8 @@ public class ManagedSetTests { @@ -34,10 +34,8 @@ public class ManagedSetTests {
@Test
public void mergeSunnyDay() {
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet child = new ManagedSet();
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = ManagedSet.of("three");
child.add("three");
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
@ -46,8 +44,7 @@ public class ManagedSetTests { @@ -46,8 +44,7 @@ public class ManagedSetTests {
@Test
public void mergeWithNullParent() {
ManagedSet child = new ManagedSet();
child.add("one");
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child);
}
@ -60,8 +57,7 @@ public class ManagedSetTests { @@ -60,8 +57,7 @@ public class ManagedSetTests {
@Test
public void mergeWithNonCompatibleParentType() {
ManagedSet child = new ManagedSet();
child.add("one");
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() ->
child.merge("hello"));
@ -69,9 +65,7 @@ public class ManagedSetTests { @@ -69,9 +65,7 @@ public class ManagedSetTests {
@Test
public void mergeEmptyChild() {
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = new ManagedSet();
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
@ -81,11 +75,8 @@ public class ManagedSetTests { @@ -81,11 +75,8 @@ public class ManagedSetTests {
@Test
public void mergeChildValuesOverrideTheParents() {
// asserts that the set contract is not violated during a merge() operation...
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet child = new ManagedSet();
child.add("one");
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2);

6
spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
@ -158,9 +158,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext { @@ -158,9 +158,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext {
pvs = new MutablePropertyValues();
pvs.add("order", "0");
pvs.add("exceptionMappings", "java.lang.Exception=failed1");
List<RuntimeBeanReference> mappedHandlers = new ManagedList<>();
mappedHandlers.add(new RuntimeBeanReference("anotherLocaleHandler"));
pvs.add("mappedHandlers", mappedHandlers);
pvs.add("mappedHandlers", ManagedList.of(new RuntimeBeanReference("anotherLocaleHandler")));
pvs.add("defaultStatusCode", "500");
pvs.add("defaultErrorView", "failed2");
registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);

Loading…
Cancel
Save