You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2014/08/05 09:50:35 UTC
[2/2] git commit: WICKET-5662 @SpringBean(name="something",
required=false) still throws
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean
named 'something' is defined
WICKET-5662 @SpringBean(name="something", required=false) still throws org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'something' is defined
(cherry picked from commit 9aec4f332aae9ec380d09dab1c68b0cd15927644)
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/b05f1727
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/b05f1727
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/b05f1727
Branch: refs/heads/wicket-6.x
Commit: b05f17278bdf2bda64e578baa24c224ca0a2a131
Parents: d3c429e
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Tue Aug 5 09:47:09 2014 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Tue Aug 5 09:48:05 2014 +0200
----------------------------------------------------------------------
.../apache/wicket/spring/SpringBeanLocator.java | 21 ++-----
.../annot/AnnotProxyFieldValueFactory.java | 61 +++++++++++++-------
.../spring/injection/annot/SpringBeanTest.java | 20 ++++++-
3 files changed, 63 insertions(+), 39 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/b05f1727/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
----------------------------------------------------------------------
diff --git a/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java b/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
index a022792..a279126 100644
--- a/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
+++ b/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
@@ -19,6 +19,7 @@ package org.apache.wicket.spring;
import java.lang.ref.WeakReference;
import org.apache.wicket.proxy.IProxyTargetLocator;
+import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.lang.Objects;
import org.apache.wicket.core.util.lang.WicketObjects;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -73,14 +74,8 @@ public class SpringBeanLocator implements IProxyTargetLocator
public SpringBeanLocator(final String beanName, final Class<?> beanType,
final ISpringContextLocator locator)
{
- if (locator == null)
- {
- throw new IllegalArgumentException("[locator] argument cannot be null");
- }
- if (beanType == null)
- {
- throw new IllegalArgumentException("[beanType] argument cannot be null");
- }
+ Args.notNull(locator, "locator");
+ Args.notNull(beanType, "beanType");
beanTypeCache = new WeakReference<Class<?>>(beanType);
beanTypeName = beanType.getName();
@@ -122,9 +117,6 @@ public class SpringBeanLocator implements IProxyTargetLocator
return clazz;
}
- /**
- * @see org.apache.wicket.proxy.IProxyTargetLocator#locateProxyTarget()
- */
@Override
public Object locateProxyTarget()
{
@@ -174,11 +166,10 @@ public class SpringBeanLocator implements IProxyTargetLocator
* bean name
* @param clazz
* bean class
- * @throws IllegalStateException
+ * @throws java.lang.IllegalStateException
* @return found bean
*/
- private static Object lookupSpringBean(final ApplicationContext ctx, final String name,
- final Class<?> clazz)
+ private Object lookupSpringBean(ApplicationContext ctx, String name, Class<?> clazz)
{
try
{
@@ -194,7 +185,7 @@ public class SpringBeanLocator implements IProxyTargetLocator
catch (NoSuchBeanDefinitionException e)
{
throw new IllegalStateException("bean with name [" + name + "] and class [" +
- clazz.getName() + "] not found");
+ clazz.getName() + "] not found", e);
}
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/b05f1727/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 3d219da..a81e2fa 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
@@ -110,7 +110,23 @@ public class AnnotProxyFieldValueFactory implements IFieldValueFactory
{
if (supportsField(field))
{
- String beanName = getBeanName(field);
+ SpringBean annot = field.getAnnotation(SpringBean.class);
+
+ String name;
+ boolean required;
+ if (annot != null)
+ {
+ name = annot.name();
+ required = annot.required();
+ }
+ else
+ {
+ Named named = field.getAnnotation(Named.class);
+ name = named != null ? named.value() : "";
+ required = false;
+ }
+
+ String beanName = getBeanName(field, name, required);
if (beanName == null)
{
@@ -128,13 +144,26 @@ public class AnnotProxyFieldValueFactory implements IFieldValueFactory
}
Object target;
- if (wrapInProxies)
+ try
{
- target = LazyInitProxyFactory.createProxy(field.getType(), locator);
+ // check whether there is a bean with the provided properties
+ target = locator.locateProxyTarget();
}
- else
+ catch (IllegalStateException isx)
{
- target = locator.locateProxyTarget();
+ if (required)
+ {
+ throw isx;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ if (wrapInProxies)
+ {
+ target = LazyInitProxyFactory.createProxy(field.getType(), locator);
}
// only put the proxy into the cache if the bean is a singleton
@@ -156,31 +185,20 @@ public class AnnotProxyFieldValueFactory implements IFieldValueFactory
* @param field
* @return bean name
*/
- private String getBeanName(final Field field)
+ private String getBeanName(final Field field, String name, boolean required)
{
- SpringBean annot = field.getAnnotation(SpringBean.class);
-
- String name;
- boolean required;
- if (annot != null) {
- name = annot.name();
- required = annot.required();
- } else {
- Named named = field.getAnnotation(Named.class);
- name = named != null ? named.value() : "";
- required = false;
- }
if (Strings.isEmpty(name))
{
- name = beanNameCache.get(field.getType());
+ Class<?> fieldType = field.getType();
+ name = beanNameCache.get(fieldType);
if (name == null)
{
- name = getBeanNameOfClass(contextLocator.getSpringContext(), field.getType(), required);
+ name = getBeanNameOfClass(contextLocator.getSpringContext(), fieldType, required);
if (name != null)
{
- String tmpName = beanNameCache.putIfAbsent(field.getType(), name);
+ String tmpName = beanNameCache.putIfAbsent(fieldType, name);
if (tmpName != null)
{
name = tmpName;
@@ -188,6 +206,7 @@ public class AnnotProxyFieldValueFactory implements IFieldValueFactory
}
}
}
+
return name;
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/b05f1727/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java
----------------------------------------------------------------------
diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java
index 663aa4c..cc55ea2 100644
--- a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java
+++ b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java
@@ -30,11 +30,9 @@ import org.junit.Test;
/**
* Test for SpringBean.
- *
- *
+ *
* @author Andrea Del Bene
*/
-
public class SpringBeanTest extends Assert
{
private WicketTester tester;
@@ -98,6 +96,11 @@ public class SpringBeanTest extends Assert
AnnotatedBeanNotRequired page;
tester.startPage(page = new AnnotatedBeanNotRequired());
assertNull(page.getBean());
+
+ // with name = something, required = false everything is fine
+ AnnotatedBeanWithSameNameRequired page2;
+ tester.startPage(page2 = new AnnotatedBeanWithSameNameRequired());
+ assertNull(page2.getBean());
}
/**
@@ -154,6 +157,17 @@ class AnnotatedBeanRequired extends DummyHomePage
}
}
+class AnnotatedBeanWithSameNameRequired extends DummyHomePage
+{
+ @SpringBean(name = "bean", required = false)
+ private Bean bean;
+
+ public Bean getBean()
+ {
+ return bean;
+ }
+}
+
class AnnotatedBeanNotRequired extends DummyHomePage
{
@SpringBean(required = false)