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);
}
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;
}
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);
}
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;
}
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;
}
Aggregations