Example usage for org.springframework.beans.factory FactoryBean FactoryBean

List of usage examples for org.springframework.beans.factory FactoryBean FactoryBean

Introduction

In this page you can find the example usage for org.springframework.beans.factory FactoryBean FactoryBean.

Prototype

FactoryBean

Source Link

Usage

From source file:org.apache.james.container.spring.lifecycle.osgi.AbstractOSGIAnnotationBeanPostProcessor.java

@SuppressWarnings("rawtypes")
private FactoryBean getServiceImporter(final A s, final Method writeMethod, String beanName) throws Exception {
    // Invocations will block here, so although the ApplicationContext is
    // created nothing will
    // proceed until all the dependencies are satisfied.
    Class<?>[] params = writeMethod.getParameterTypes();
    if (params.length != 1) {
        throw new IllegalArgumentException("Setter for [" + beanName + "] must have only one argument");
    }//  w w  w  .ja  va2 s .c o m

    if (lookupBeanFactory) {
        if (logger.isDebugEnabled())
            logger.debug("Lookup the bean via the BeanFactory");

        final Class<?> clazz = writeMethod.getParameterTypes()[0];
        Object bean;
        try {
            bean = getBeanFromFactory(s, clazz);
        } catch (NoSuchBeanDefinitionException e) {
            // We was not able to find the bean in the factory so fallback to the osgi registry
            bean = null;
        }

        if (bean != null) {
            final Object fBean = bean;

            // Create a new FactoryBean which just return the found beab
            return new FactoryBean() {

                @Override
                public Object getObject() throws Exception {
                    return fBean;
                }

                @Override
                public Class getObjectType() {
                    return fBean.getClass();
                }

                @Override
                public boolean isSingleton() {
                    return true;
                }
            };
        }
    }
    // The bean was not found in the BeanFactory. Its time to lookup it via the OSGI-Registry
    return getResourceProperty(new OsgiServiceProxyFactoryBean(), getFilter(s), writeMethod, beanName);
}