use of org.apache.wicket.spring.SpringBeanLocator in project wicket by apache.
the class AnnotProxyFieldValueFactoryTest method testFactory.
/**
* Test the factory
*
* @throws Exception
*/
@Test
public void testFactory() throws Exception {
SpringBeanLocator locator;
Object proxy;
Field field = obj.getClass().getDeclaredField("nobean");
proxy = factory.getFieldValue(field, obj);
assertNull(proxy);
field = obj.getClass().getDeclaredField("beanByClass");
proxy = factory.getFieldValue(field, obj);
locator = (SpringBeanLocator) ((ILazyInitProxy) proxy).getObjectLocator();
assertTrue(locator.getBeanType().equals(Bean.class));
assertTrue(locator.getSpringContextLocator() == mockCtxLocator);
assertThat(factory.getFieldValue(field, obj), instanceOf(ILazyInitProxy.class));
field = obj.getClass().getDeclaredField("beanByName");
proxy = factory.getFieldValue(field, obj);
locator = (SpringBeanLocator) ((ILazyInitProxy) proxy).getObjectLocator();
assertTrue(locator.getBeanName().equals("somebean"));
assertTrue(locator.getBeanType().equals(Bean2.class));
assertTrue(locator.getSpringContextLocator() == mockCtxLocator);
assertThat(factory.getFieldValue(field, obj), instanceOf(ILazyInitProxy.class));
}
use of org.apache.wicket.spring.SpringBeanLocator in project wicket by apache.
the class AnnotatedFieldInBehaviorPage method beanExistsDifferentName.
/**
* @throws Exception
*/
@Test
public void beanExistsDifferentName() throws Exception {
// add dependency beans of the same type
ctx.putBean("mrBean", new Bean());
ctx.putBean("theBean", new Bean());
// with no name specified we get IllegalStateException
try {
tester.startPage(new AnnotatedBeanRequired());
fail();
} catch (IllegalStateException e) {
}
// we must inject bean with name "mrBean"
AnnotatedBeanNotRequiredDifferentName page;
tester.startPage(page = new AnnotatedBeanNotRequiredDifferentName());
SpringBeanLocator locator = (SpringBeanLocator) ((ILazyInitProxy) page.getBean()).getObjectLocator();
assertTrue(locator.getBeanName().equals("mrBean"));
}
use of org.apache.wicket.spring.SpringBeanLocator in project wicket by apache.
the class AnnotProxyFieldValueFactory method getFieldValue.
@Override
public Object getFieldValue(final Field field, final Object fieldOwner) {
if (supportsField(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 = true;
}
Class<?> generic = ResolvableType.forField(field).resolveGeneric(0);
String beanName = getBeanName(field, name, required, generic);
SpringBeanLocator locator = new SpringBeanLocator(beanName, field.getType(), field, contextLocator);
// only check the cache if the bean is a singleton
Object cachedValue = cache.get(locator);
if (cachedValue != null) {
return cachedValue;
}
Object target;
try {
// check whether there is a bean with the provided properties
target = locator.locateProxyTarget();
} catch (IllegalStateException isx) {
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
if (locator.isSingletonBean()) {
Object tmpTarget = cache.putIfAbsent(locator, target);
if (tmpTarget != null) {
target = tmpTarget;
}
}
return target;
}
return null;
}
Aggregations