Search in sources :

Example 1 with AutowireCapableBeanFactory

use of org.springframework.beans.factory.config.AutowireCapableBeanFactory in project spring-framework by spring-projects.

the class DefaultListableBeanFactory method resolveNamedBean.

//---------------------------------------------------------------------
// Dependency resolution functionality
//---------------------------------------------------------------------
@Override
public <T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException {
    NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, (Object[]) null);
    if (namedBean != null) {
        return namedBean;
    }
    BeanFactory parent = getParentBeanFactory();
    if (parent instanceof AutowireCapableBeanFactory) {
        return ((AutowireCapableBeanFactory) parent).resolveNamedBean(requiredType);
    }
    throw new NoSuchBeanDefinitionException(requiredType);
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 2 with AutowireCapableBeanFactory

use of org.springframework.beans.factory.config.AutowireCapableBeanFactory in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessor method autowireResource.

/**
	 * Obtain a resource object for the given name and type through autowiring
	 * based on the given factory.
	 * @param factory the factory to autowire against
	 * @param element the descriptor for the annotated field/method
	 * @param requestingBeanName the name of the requesting bean
	 * @return the resource object (never {@code null})
	 * @throws BeansException if we failed to obtain the target resource
	 */
protected Object autowireResource(BeanFactory factory, LookupElement element, String requestingBeanName) throws BeansException {
    Object resource;
    Set<String> autowiredBeanNames;
    String name = element.name;
    if (this.fallbackToDefaultTypeMatch && element.isDefaultName && factory instanceof AutowireCapableBeanFactory && !factory.containsBean(name)) {
        autowiredBeanNames = new LinkedHashSet<>();
        resource = ((AutowireCapableBeanFactory) factory).resolveDependency(element.getDependencyDescriptor(), requestingBeanName, autowiredBeanNames, null);
    } else {
        resource = factory.getBean(name, element.lookupType);
        autowiredBeanNames = Collections.singleton(name);
    }
    if (factory instanceof ConfigurableBeanFactory) {
        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
        for (String autowiredBeanName : autowiredBeanNames) {
            if (beanFactory.containsBean(autowiredBeanName)) {
                beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
            }
        }
    }
    return resource;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory)

Example 3 with AutowireCapableBeanFactory

use of org.springframework.beans.factory.config.AutowireCapableBeanFactory in project spring-framework by spring-projects.

the class DependencyInjectionTestExecutionListener method injectDependencies.

/**
	 * Performs dependency injection and bean initialization for the supplied
	 * {@link TestContext} as described in
	 * {@link #prepareTestInstance(TestContext) prepareTestInstance()}.
	 * <p>The {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} will be subsequently removed
	 * from the test context, regardless of its value.
	 * @param testContext the test context for which dependency injection should
	 * be performed (never {@code null})
	 * @throws Exception allows any exception to propagate
	 * @see #prepareTestInstance(TestContext)
	 * @see #beforeTestMethod(TestContext)
	 */
protected void injectDependencies(final TestContext testContext) throws Exception {
    Object bean = testContext.getTestInstance();
    AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
    beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
    beanFactory.initializeBean(bean, testContext.getTestClass().getName());
    testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}
Also used : AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory)

Example 4 with AutowireCapableBeanFactory

use of org.springframework.beans.factory.config.AutowireCapableBeanFactory in project cloudstack by apache.

the class ComponentContext method inject.

public static <T> T inject(Object instance) {
    // autowire dynamically loaded object
    AutowireCapableBeanFactory beanFactory = getApplicationContext(instance).getAutowireCapableBeanFactory();
    beanFactory.autowireBean(instance);
    return (T) instance;
}
Also used : AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory)

Example 5 with AutowireCapableBeanFactory

use of org.springframework.beans.factory.config.AutowireCapableBeanFactory in project grails-core by grails.

the class DefaultGrailsCodecClass method autowireCodecBean.

protected Object autowireCodecBean(Object existingBean) {
    if (existingBean != null && grailsApplication != null && grailsApplication.getMainContext() != null) {
        AutowireCapableBeanFactory beanFactory = grailsApplication.getMainContext().getAutowireCapableBeanFactory();
        beanFactory.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
        existingBean = beanFactory.initializeBean(existingBean, "codec");
    }
    return existingBean;
}
Also used : AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory)

Aggregations

AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)10 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 ApplicationContext (org.springframework.context.ApplicationContext)2 BaseOtterTest (com.alibaba.otter.node.etl.BaseOtterTest)1 OtterDownStreamHandler (com.alibaba.otter.node.etl.select.selector.canal.OtterDownStreamHandler)1 ManagementBean (com.cloud.utils.mgmt.ManagementBean)1 GrailsPlugin (grails.plugins.GrailsPlugin)1 ConfigObject (groovy.util.ConfigObject)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 ConfigurationException (javax.naming.ConfigurationException)1 CloudStackSpringContext (org.apache.cloudstack.spring.module.factory.CloudStackSpringContext)1