Java Code Examples for org.springframework.beans.factory.BeanFactory#containsBean()

The following examples show how to use org.springframework.beans.factory.BeanFactory#containsBean() . 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: BeanFactoryAnnotationUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
 * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
 * qualifier, or having a bean name matching the given qualifier.
 * @param beanFactory the factory to get the target bean from (also searching ancestors)
 * @param beanType the type of bean to retrieve
 * @param qualifier the qualifier for selecting between multiple bean matches
 * @return the matching bean of type {@code T} (never {@code null})
 * @throws NoUniqueBeanDefinitionException if multiple matching beans of type {@code T} found
 * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
 * @throws BeansException if the bean could not be created
 * @see BeanFactoryUtils#beanOfTypeIncludingAncestors(ListableBeanFactory, Class)
 */
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier)
		throws BeansException {

	Assert.notNull(beanFactory, "BeanFactory must not be null");

	if (beanFactory instanceof ListableBeanFactory) {
		// Full qualifier matching supported.
		return qualifiedBeanOfType((ListableBeanFactory) beanFactory, beanType, qualifier);
	}
	else if (beanFactory.containsBean(qualifier)) {
		// Fallback: target bean at least found by bean name.
		return beanFactory.getBean(qualifier, beanType);
	}
	else {
		throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() +
				" bean found for bean name '" + qualifier +
				"'! (Note: Qualifier matching not supported because given " +
				"BeanFactory does not implement ConfigurableListableBeanFactory.)");
	}
}
 
Example 2
Source File: BeanFactoryAnnotationUtils.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
 * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
 * qualifier, or having a bean name matching the given qualifier.
 * @param beanFactory the BeanFactory to get the target bean from
 * @param beanType the type of bean to retrieve
 * @param qualifier the qualifier for selecting between multiple bean matches
 * @return the matching bean of type {@code T} (never {@code null})
 * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
 */
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) {
	if (beanFactory instanceof ConfigurableListableBeanFactory) {
		// Full qualifier matching supported.
		return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier);
	}
	else if (beanFactory.containsBean(qualifier)) {
		// Fallback: target bean at least found by bean name.
		return beanFactory.getBean(qualifier, beanType);
	}
	else {
		throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() +
				" bean found for bean name '" + qualifier +
				"'! (Note: Qualifier matching not supported because given " +
				"BeanFactory does not implement ConfigurableListableBeanFactory.)");
	}
}
 
Example 3
Source File: SpringBeanELResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setValue(ELContext elContext, Object base, Object property, Object value) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			if (value == bf.getBean(beanName)) {
				// Setting the bean reference to the same value is alright - can simply be ignored...
				elContext.setPropertyResolved(true);
			}
			else {
				throw new PropertyNotWritableException(
						"Variable '" + beanName + "' refers to a Spring bean which by definition is not writable");
			}
		}
	}
}
 
Example 4
Source File: BeanFactoryAnnotationUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
 * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
 * qualifier, or having a bean name matching the given qualifier.
 * @param beanFactory the BeanFactory to get the target bean from
 * @param beanType the type of bean to retrieve
 * @param qualifier the qualifier for selecting between multiple bean matches
 * @return the matching bean of type {@code T} (never {@code null})
 * @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
 */
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) {
	if (beanFactory instanceof ConfigurableListableBeanFactory) {
		// Full qualifier matching supported.
		return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier);
	}
	else if (beanFactory.containsBean(qualifier)) {
		// Fallback: target bean at least found by bean name.
		return beanFactory.getBean(qualifier, beanType);
	}
	else {
		throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() +
				" bean found for bean name '" + qualifier +
				"'! (Note: Qualifier matching not supported because given " +
				"BeanFactory does not implement ConfigurableListableBeanFactory.)");
	}
}
 
Example 5
Source File: SpringBeanELResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(ELContext elContext, Object base, Object property) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			if (logger.isTraceEnabled()) {
				logger.trace("Successfully resolved variable '" + beanName + "' in Spring BeanFactory");
			}
			elContext.setPropertyResolved(true);
			return bf.getBean(beanName);
		}
	}
	return null;
}
 
Example 6
Source File: SpringBeanELResolver.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(ELContext elContext, Object base, Object property, Object value) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			throw new PropertyNotWritableException(
					"Variable '" + beanName + "' refers to a Spring bean which by definition is not writable");
		}
	}
}
 
Example 7
Source File: SpringBeanELResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getType(ELContext elContext, Object base, Object property) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			elContext.setPropertyResolved(true);
			return bf.getType(beanName);
		}
	}
	return null;
}
 
Example 8
Source File: SpringBeanELResolver.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(ELContext elContext, Object base, Object property) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			if (logger.isTraceEnabled()) {
				logger.trace("Successfully resolved variable '" + beanName + "' in Spring BeanFactory");
			}
			elContext.setPropertyResolved(true);
			return bf.getBean(beanName);
		}
	}
	return null;
}
 
Example 9
Source File: AbstractBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean containsBean(String name) {
	String beanName = transformedBeanName(name);
	if (containsSingleton(beanName) || containsBeanDefinition(beanName)) {
		return (!BeanFactoryUtils.isFactoryDereference(name) || isFactoryBean(name));
	}
	// Not found -> check parent.
	BeanFactory parentBeanFactory = getParentBeanFactory();
	return (parentBeanFactory != null && parentBeanFactory.containsBean(originalBeanName(name)));
}
 
Example 10
Source File: SpringBeanELResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isReadOnly(ELContext elContext, Object base, Object property) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			return true;
		}
	}
	return false;
}
 
Example 11
Source File: SpringBeanELResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Class<?> getType(ELContext elContext, Object base, Object property) throws ELException {
	if (base == null) {
		String beanName = property.toString();
		BeanFactory bf = getBeanFactory(elContext);
		if (bf.containsBean(beanName)) {
			elContext.setPropertyResolved(true);
			return bf.getType(beanName);
		}
	}
	return null;
}
 
Example 12
Source File: CompositeBeanFactory.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Override
public boolean containsBean(String name) {
	for (BeanFactory f : factories) {
		try {
			boolean b = f.containsBean(name);
			if (b) {
				return b;
			}	
		} catch (BeansException e) {
			LOG.info("bean exception", e);
		}
	}
	return false;
}
 
Example 13
Source File: ControllerAdviceBean.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ControllerAdviceBean(Object bean, @Nullable BeanFactory beanFactory) {
	this.bean = bean;
	this.beanFactory = beanFactory;
	Class<?> beanType;

	if (bean instanceof String) {
		String beanName = (String) bean;
		Assert.hasText(beanName, "Bean name must not be null");
		Assert.notNull(beanFactory, "BeanFactory must not be null");
		if (!beanFactory.containsBean(beanName)) {
			throw new IllegalArgumentException("BeanFactory [" + beanFactory +
					"] does not contain specified controller advice bean '" + beanName + "'");
		}
		beanType = this.beanFactory.getType(beanName);
		this.order = initOrderFromBeanType(beanType);
	}
	else {
		Assert.notNull(bean, "Bean must not be null");
		beanType = bean.getClass();
		this.order = initOrderFromBean(bean);
	}

	ControllerAdvice annotation = (beanType != null ?
			AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class) : null);

	if (annotation != null) {
		this.beanTypePredicate = HandlerTypePredicate.builder()
				.basePackage(annotation.basePackages())
				.basePackageClass(annotation.basePackageClasses())
				.assignableType(annotation.assignableTypes())
				.annotation(annotation.annotations())
				.build();
	}
	else {
		this.beanTypePredicate = HandlerTypePredicate.forAnyHandlerType();
	}
}
 
Example 14
Source File: ControllerAdviceBean.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private ControllerAdviceBean(Object bean, BeanFactory beanFactory) {
	this.bean = bean;
	this.beanFactory = beanFactory;
	Class<?> beanType;

	if (bean instanceof String) {
		String beanName = (String) bean;
		Assert.hasText(beanName, "Bean name must not be null");
		Assert.notNull(beanFactory, "BeanFactory must not be null");
		if (!beanFactory.containsBean(beanName)) {
			throw new IllegalArgumentException("BeanFactory [" + beanFactory +
					"] does not contain specified controller advice bean '" + beanName + "'");
		}
		beanType = this.beanFactory.getType(beanName);
		this.order = initOrderFromBeanType(beanType);
	}
	else {
		Assert.notNull(bean, "Bean must not be null");
		beanType = bean.getClass();
		this.order = initOrderFromBean(bean);
	}

	ControllerAdvice annotation =
			AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class);

	if (annotation != null) {
		this.basePackages = initBasePackages(annotation);
		this.assignableTypes = Arrays.asList(annotation.assignableTypes());
		this.annotations = Arrays.asList(annotation.annotations());
	}
	else {
		this.basePackages = Collections.emptySet();
		this.assignableTypes = Collections.emptyList();
		this.annotations = Collections.emptyList();
	}
}
 
Example 15
Source File: AbstractBeanFactory.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean containsBean(String name) {
	String beanName = transformedBeanName(name);
	if (containsSingleton(beanName) || containsBeanDefinition(beanName)) {
		return (!BeanFactoryUtils.isFactoryDereference(name) || isFactoryBean(name));
	}
	// Not found -> check parent.
	BeanFactory parentBeanFactory = getParentBeanFactory();
	return (parentBeanFactory != null && parentBeanFactory.containsBean(originalBeanName(name)));
}
 
Example 16
Source File: AbstractBeanFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean containsBean(String name) {
	String beanName = transformedBeanName(name);
	if (containsSingleton(beanName) || containsBeanDefinition(beanName)) {
		return (!BeanFactoryUtils.isFactoryDereference(name) || isFactoryBean(name));
	}
	// Not found -> check parent.
	BeanFactory parentBeanFactory = getParentBeanFactory();
	return (parentBeanFactory != null && parentBeanFactory.containsBean(originalBeanName(name)));
}
 
Example 17
Source File: ControllerAdviceBean.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ControllerAdviceBean(Object bean, @Nullable BeanFactory beanFactory) {
	this.bean = bean;
	this.beanFactory = beanFactory;
	Class<?> beanType;

	if (bean instanceof String) {
		String beanName = (String) bean;
		Assert.hasText(beanName, "Bean name must not be null");
		Assert.notNull(beanFactory, "BeanFactory must not be null");
		if (!beanFactory.containsBean(beanName)) {
			throw new IllegalArgumentException("BeanFactory [" + beanFactory +
					"] does not contain specified controller advice bean '" + beanName + "'");
		}
		beanType = this.beanFactory.getType(beanName);
		this.order = initOrderFromBeanType(beanType);
	}
	else {
		Assert.notNull(bean, "Bean must not be null");
		beanType = bean.getClass();
		this.order = initOrderFromBean(bean);
	}

	ControllerAdvice annotation = (beanType != null ?
			AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class) : null);

	if (annotation != null) {
		this.beanTypePredicate = HandlerTypePredicate.builder()
				.basePackage(annotation.basePackages())
				.basePackageClass(annotation.basePackageClasses())
				.assignableType(annotation.assignableTypes())
				.annotation(annotation.annotations())
				.build();
	}
	else {
		this.beanTypePredicate = HandlerTypePredicate.forAnyHandlerType();
	}
}
 
Example 18
Source File: AbstractBeanFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean containsBean(String name) {
	String beanName = transformedBeanName(name);
	if (containsSingleton(beanName) || containsBeanDefinition(beanName)) {
		return (!BeanFactoryUtils.isFactoryDereference(name) || isFactoryBean(name));
	}
	// Not found -> check parent.
	BeanFactory parentBeanFactory = getParentBeanFactory();
	return (parentBeanFactory != null && parentBeanFactory.containsBean(originalBeanName(name)));
}
 
Example 19
Source File: CommonAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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<String>();
		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;
}
 
Example 20
Source File: BeanUtils.java    From spring-context-support with Apache License 2.0 2 votes vote down vote up
/**
 * Is Bean Present or not by the specified name and class
 *
 * @param beanFactory {@link BeanFactory}
 * @param beanName    The bean name
 * @param beanClass   The bean class
 * @return If present , return <code>true</code> , or <code>false</code>
 * @since 1.0.6
 */
public static boolean isBeanPresent(BeanFactory beanFactory, String beanName, Class<?> beanClass)
        throws NullPointerException {
    return beanFactory.containsBean(beanName) && beanFactory.isTypeMatch(beanName, beanClass);
}