Example usage for org.springframework.beans.factory.support RootBeanDefinition addQualifier

List of usage examples for org.springframework.beans.factory.support RootBeanDefinition addQualifier

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support RootBeanDefinition addQualifier.

Prototype

public void addQualifier(AutowireCandidateQualifier qualifier) 

Source Link

Document

Register a qualifier to be used for autowire candidate resolution, keyed by the qualifier's type name.

Usage

From source file:org.exoplatform.container.spring.SpringContainer.java

/**
 * {@inheritDoc}//www.  j a  v a  2 s. c o  m
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void start() {
    ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false);
    // We check if the component has been defined in the configuration of the current container
    // The goal is to enable the SpringContainer only if it is needed
    Component component = cm.getComponent(ApplicationContextProvider.class);
    if (component == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(
                    "No ApplicationContextProvider has been defined, thus the SpringContainer will be disabled."
                            + " To enable the Spring Integration please define an ApplicationContextProvider");
        }
    } else {
        DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
        bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
        Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters();
        for (ComponentAdapter<?> adapter : adapters) {
            Object key = adapter.getComponentKey();
            String name = keyToBeanName(key);
            String factoryName = name + "#factory";
            RootBeanDefinition def = new RootBeanDefinition(adapter.getComponentImplementation(),
                    AbstractBeanDefinition.AUTOWIRE_NO, false);
            def.setScope(BeanDefinition.SCOPE_PROTOTYPE);
            def.setFactoryBeanName(factoryName);
            def.setFactoryMethodName("getInstance");
            def.setLazyInit(true);
            def.setTargetType(adapter.getComponentImplementation());
            if (key instanceof String) {
                def.addQualifier(new AutowireCandidateQualifier(Named.class, key));
            } else if (key instanceof Class<?> && ((Class<?>) key).isAnnotation()) {
                def.addQualifier(new AutowireCandidateQualifier((Class<?>) key));
            } else {
                def.setPrimary(true);
            }
            bf.registerBeanDefinition(name, def);
            bf.registerSingleton(factoryName, new ComponentAdapterFactoryBean(adapter));
        }
        GenericApplicationContext parentContext = new GenericApplicationContext(bf);
        parentContext.refresh();
        ApplicationContextProvider provider = super.getComponentInstanceOfType(ApplicationContextProvider.class,
                false);
        ctx = provider.getApplicationContext(parentContext);
        LOG.info("A SpringContainer has been enabled using the ApplicationContextProvider "
                + provider.getClass());
    }
    super.start();
}