Java Code Examples for org.springframework.beans.factory.config.NamedBeanHolder#getBeanInstance()

The following examples show how to use org.springframework.beans.factory.config.NamedBeanHolder#getBeanInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScheduledAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private <T> T resolveSchedulerBean(BeanFactory beanFactory, Class<T> schedulerType, boolean byName) {
	if (byName) {
		T scheduler = beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, schedulerType);
		if (this.beanName != null && this.beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
					DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName);
		}
		return scheduler;
	}
	else if (beanFactory instanceof AutowireCapableBeanFactory) {
		NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) beanFactory).resolveNamedBean(schedulerType);
		if (this.beanName != null && beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) beanFactory).registerDependentBean(holder.getBeanName(), this.beanName);
		}
		return holder.getBeanInstance();
	}
	else {
		return beanFactory.getBean(schedulerType);
	}
}
 
Example 2
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Find a single default EntityManagerFactory in the Spring application context.
 * @return the default EntityManagerFactory
 * @throws NoSuchBeanDefinitionException if there is no single EntityManagerFactory in the context
 */
protected EntityManagerFactory findDefaultEntityManagerFactory(@Nullable String requestingBeanName)
		throws NoSuchBeanDefinitionException {

	Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup");

	if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
		// Fancy variant with dependency registration
		ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory;
		NamedBeanHolder<EntityManagerFactory> emfHolder = clbf.resolveNamedBean(EntityManagerFactory.class);
		if (requestingBeanName != null) {
			clbf.registerDependentBean(emfHolder.getBeanName(), requestingBeanName);
		}
		return emfHolder.getBeanInstance();
	}
	else {
		// Plain variant: just find a default bean
		return this.beanFactory.getBean(EntityManagerFactory.class);
	}
}
 
Example 3
Source File: DefaultListableBeanFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private <T> T resolveBean(ResolvableType requiredType, @Nullable Object[] args, boolean nonUniqueAsNull) {
	NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, args, nonUniqueAsNull);
	if (namedBean != null) {
		return namedBean.getBeanInstance();
	}
	BeanFactory parent = getParentBeanFactory();
	if (parent instanceof DefaultListableBeanFactory) {
		return ((DefaultListableBeanFactory) parent).resolveBean(requiredType, args, nonUniqueAsNull);
	}
	else if (parent != null) {
		ObjectProvider<T> parentProvider = parent.getBeanProvider(requiredType);
		if (args != null) {
			return parentProvider.getObject(args);
		}
		else {
			return (nonUniqueAsNull ? parentProvider.getIfUnique() : parentProvider.getIfAvailable());
		}
	}
	return null;
}
 
Example 4
Source File: ScheduledAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
private <T> T resolveSchedulerBean(BeanFactory beanFactory, Class<T> schedulerType, boolean byName) {
	if (byName) {
		T scheduler = beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, schedulerType);
		if (this.beanName != null && this.beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
					DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName);
		}
		return scheduler;
	}
	else if (beanFactory instanceof AutowireCapableBeanFactory) {
		NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) beanFactory).resolveNamedBean(schedulerType);
		if (this.beanName != null && beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) beanFactory).registerDependentBean(holder.getBeanName(), this.beanName);
		}
		return holder.getBeanInstance();
	}
	else {
		return beanFactory.getBean(schedulerType);
	}
}
 
Example 5
Source File: PersistenceAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Find a single default EntityManagerFactory in the Spring application context.
 * @return the default EntityManagerFactory
 * @throws NoSuchBeanDefinitionException if there is no single EntityManagerFactory in the context
 */
protected EntityManagerFactory findDefaultEntityManagerFactory(@Nullable String requestingBeanName)
		throws NoSuchBeanDefinitionException {

	Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup");

	if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
		// Fancy variant with dependency registration
		ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory;
		NamedBeanHolder<EntityManagerFactory> emfHolder = clbf.resolveNamedBean(EntityManagerFactory.class);
		if (requestingBeanName != null) {
			clbf.registerDependentBean(emfHolder.getBeanName(), requestingBeanName);
		}
		return emfHolder.getBeanInstance();
	}
	else {
		// Plain variant: just find a default bean
		return this.beanFactory.getBean(EntityManagerFactory.class);
	}
}
 
Example 6
Source File: DefaultListableBeanFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private <T> T resolveBean(ResolvableType requiredType, @Nullable Object[] args, boolean nonUniqueAsNull) {
	NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, args, nonUniqueAsNull);
	if (namedBean != null) {
		return namedBean.getBeanInstance();
	}
	BeanFactory parent = getParentBeanFactory();
	if (parent instanceof DefaultListableBeanFactory) {
		return ((DefaultListableBeanFactory) parent).resolveBean(requiredType, args, nonUniqueAsNull);
	}
	else if (parent != null) {
		ObjectProvider<T> parentProvider = parent.getBeanProvider(requiredType);
		if (args != null) {
			return parentProvider.getObject(args);
		}
		else {
			return (nonUniqueAsNull ? parentProvider.getIfUnique() : parentProvider.getIfAvailable());
		}
	}
	return null;
}
 
Example 7
Source File: ScheduledAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private <T> T resolveSchedulerBean(Class<T> schedulerType, boolean byName) {
	if (byName) {
		T scheduler = this.beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, schedulerType);
		if (this.beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
					DEFAULT_TASK_SCHEDULER_BEAN_NAME, this.beanName);
		}
		return scheduler;
	}
	else if (this.beanFactory instanceof AutowireCapableBeanFactory) {
		NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) this.beanFactory).resolveNamedBean(schedulerType);
		if (this.beanFactory instanceof ConfigurableBeanFactory) {
			((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(
					holder.getBeanName(), this.beanName);
		}
		return holder.getBeanInstance();
	}
	else {
		return this.beanFactory.getBean(schedulerType);
	}
}
 
Example 8
Source File: PersistenceAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find a single default EntityManagerFactory in the Spring application context.
 * @return the default EntityManagerFactory
 * @throws NoSuchBeanDefinitionException if there is no single EntityManagerFactory in the context
 */
protected EntityManagerFactory findDefaultEntityManagerFactory(String requestingBeanName)
		throws NoSuchBeanDefinitionException {

	if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
		// Fancy variant with dependency registration
		ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory;
		NamedBeanHolder<EntityManagerFactory> emfHolder = clbf.resolveNamedBean(EntityManagerFactory.class);
		clbf.registerDependentBean(emfHolder.getBeanName(), requestingBeanName);
		return emfHolder.getBeanInstance();
	}
	else {
		// Plain variant: just find a default bean
		return this.beanFactory.getBean(EntityManagerFactory.class);
	}
}
 
Example 9
Source File: DefaultListableBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
	NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, args);
	if (namedBean != null) {
		return namedBean.getBeanInstance();
	}
	BeanFactory parent = getParentBeanFactory();
	if (parent != null) {
		return parent.getBean(requiredType, args);
	}
	throw new NoSuchBeanDefinitionException(requiredType);
}
 
Example 10
Source File: ScheduledTaskBeanLocator.java    From bugsnag-java with MIT License 5 votes vote down vote up
private <T> T resolveSchedulerBeanByType(Class<T> schedulerType) {
    if (this.beanFactory instanceof AutowireCapableBeanFactory) {
        AutowireCapableBeanFactory factory = (AutowireCapableBeanFactory) this.beanFactory;
        NamedBeanHolder<T> holder = factory.resolveNamedBean(schedulerType);
        return holder.getBeanInstance();
    } else {
        return this.beanFactory.getBean(schedulerType);
    }
}