Search in sources :

Example 1 with SpringBeanLocator

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));
}
Also used : Field(java.lang.reflect.Field) SpringBeanLocator(org.apache.wicket.spring.SpringBeanLocator) Bean2(org.apache.wicket.spring.injection.util.Bean2) ILazyInitProxy(org.apache.wicket.proxy.ILazyInitProxy) Bean(org.apache.wicket.spring.injection.util.Bean) Test(org.junit.Test)

Example 2 with SpringBeanLocator

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"));
}
Also used : SpringBeanLocator(org.apache.wicket.spring.SpringBeanLocator) Bean(org.apache.wicket.spring.Bean) Test(org.junit.Test)

Example 3 with SpringBeanLocator

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;
}
Also used : Named(javax.inject.Named) SpringBeanLocator(org.apache.wicket.spring.SpringBeanLocator)

Aggregations

SpringBeanLocator (org.apache.wicket.spring.SpringBeanLocator)3 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 Named (javax.inject.Named)1 ILazyInitProxy (org.apache.wicket.proxy.ILazyInitProxy)1 Bean (org.apache.wicket.spring.Bean)1 Bean (org.apache.wicket.spring.injection.util.Bean)1 Bean2 (org.apache.wicket.spring.injection.util.Bean2)1