Repository: wicket Updated Branches: refs/heads/master dd54298ce -> 99f0fdc37
WICKET-5808 SpringBean, support generic beans Improved code tests and fixed bean lookup. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/99f0fdc3 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/99f0fdc3 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/99f0fdc3 Branch: refs/heads/master Commit: 99f0fdc37f664f252baf8c6df91392c7e47c8d34 Parents: dd54298 Author: Andrea Del Bene <[email protected]> Authored: Sat Jan 24 17:43:50 2015 +0100 Committer: Andrea Del Bene <[email protected]> Committed: Sat Jan 24 17:43:50 2015 +0100 ---------------------------------------------------------------------- .../wicket/spring/FieldBeansCollector.java | 22 ++++++++++++++ .../annot/AnnotProxyFieldValueFactory.java | 14 +++------ .../annot/SpringBeanWithGenericsTest.java | 31 ++++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/99f0fdc3/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java b/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java index 5ceb8b3..f0f51dc 100644 --- a/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java +++ b/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java @@ -26,6 +26,13 @@ import java.util.Set; import org.springframework.core.ResolvableType; +/** + * Convenience class to extract information about the type and generics of a field to inject. + * The field is a List, a Map or a Set and the generic type of its elements is extracted as well + * + * @author Tobias Soloschenko + * @author Andrea Del Bene + */ public class FieldBeansCollector { private final FieldType fieldType; @@ -72,6 +79,12 @@ public class FieldBeansCollector } } + /** + * Returns an instance containing all the beans collected for the field and + * compatible with the type of the field. + * + * @return the instance to inject into the field. + */ public Object getBeansToInject() { if (beansToInjectMap != null && beansToInjectMap.size() > 0) @@ -87,6 +100,15 @@ public class FieldBeansCollector return null; } + /** + * Adds compatible bean to the field. This means that the field type is Map, a List or a Set + * and that the given bean is compatible with its elements type. + * + * @param beanName + * the name of the bean to inject + * @param bean + * the bean to inject + */ public void addBean(String beanName, Object bean) { switch (fieldType) http://git-wip-us.apache.org/repos/asf/wicket/blob/99f0fdc3/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java b/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java index b508ebb..27c4008 100644 --- a/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java +++ b/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java @@ -247,15 +247,7 @@ public class AnnotProxyFieldValueFactory implements IFieldValueFactory } } - if (names.isEmpty()) - { - if (required) - { - throw new IllegalStateException("bean of type [" + clazz.getName() + "] not found"); - } - return null; - } - else if (names.size() > 1) + if (names.size() > 1) { if (ctx instanceof AbstractApplicationContext) { @@ -292,10 +284,12 @@ public class AnnotProxyFieldValueFactory implements IFieldValueFactory msg.append(Strings.join(",", names.toArray(new String[names.size()]))); throw new IllegalStateException(msg.toString()); } - else + else if(!names.isEmpty()) { return names.get(0); } + + return null; } public BeanDefinition getBeanDefinition(final ConfigurableListableBeanFactory beanFactory, http://git-wip-us.apache.org/repos/asf/wicket/blob/99f0fdc3/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanWithGenericsTest.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanWithGenericsTest.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanWithGenericsTest.java index ed105e9..782dc00 100644 --- a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanWithGenericsTest.java +++ b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanWithGenericsTest.java @@ -19,6 +19,7 @@ package org.apache.wicket.spring.injection.annot; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.wicket.spring.BeanWithGeneric; import org.apache.wicket.util.tester.DummyHomePage; @@ -30,6 +31,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +/** + * @author Andrea Del Bene + * + */ public class SpringBeanWithGenericsTest extends Assert { private WicketTester tester; @@ -94,6 +99,21 @@ public class SpringBeanWithGenericsTest extends Assert } @Test + public void setOfGenerics() throws Exception + { + AnnotatedSetOfBeanGenericQualifier page = + tester.startPage(new AnnotatedSetOfBeanGenericQualifier()); + + Set<BeanWithGeneric<?>> beans = page.getBeans(); + + assertNotNull(beans); + assertEquals(2, beans.size()); + + assertTrue(beans.contains(ctx.getBean("stringBean"))); + assertTrue(beans.contains(ctx.getBean("integerBean"))); + } + + @Test public void listField() throws Exception { AnnotatedListField page = @@ -145,6 +165,17 @@ public class SpringBeanWithGenericsTest extends Assert return beans; } } + + class AnnotatedSetOfBeanGenericQualifier extends DummyHomePage + { + @SpringBean + private Set<BeanWithGeneric<?>> beans; + + public Set<BeanWithGeneric<?>> getBeans() + { + return beans; + } + } class AnnotatedListOfBeanTypeQualifier extends DummyHomePage {
