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

The following examples show how to use org.springframework.beans.factory.config.ConfigurableBeanFactory#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: CommonAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.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 2
Source File: PlaceholderHelper.java    From apollo with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve placeholder property values, e.g.
 * <br />
 * <br />
 * "${somePropertyValue}" -> "the actual property value"
 */
public Object resolvePropertyValue(ConfigurableBeanFactory beanFactory, String beanName, String placeholder) {
  // resolve string value
  String strVal = beanFactory.resolveEmbeddedValue(placeholder);

  BeanDefinition bd = (beanFactory.containsBean(beanName) ? beanFactory
      .getMergedBeanDefinition(beanName) : null);

  // resolve expressions like "#{systemProperties.myProp}"
  return evaluateBeanDefinitionString(beanFactory, strVal, bd);
}
 
Example 3
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 4
Source File: MockMvcClientHttpRequestFactoryTestExecutionListener.java    From initializr with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
	ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) testContext.getApplicationContext()
			.getAutowireCapableBeanFactory();
	if (!beanFactory.containsBean("mockMvcClientHttpRequestFactory")) {
		this.factory = new MockMvcClientHttpRequestFactory(beanFactory.getBean(MockMvc.class));
		beanFactory.registerSingleton("mockMvcClientHttpRequestFactory", this.factory);
	}
	else {
		this.factory = beanFactory.getBean("mockMvcClientHttpRequestFactory",
				MockMvcClientHttpRequestFactory.class);
	}
}
 
Example 5
Source File: ConfigurationClassEnhancer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Check the BeanFactory to see whether the bean named <var>beanName</var> already
 * exists. Accounts for the fact that the requested bean may be "in creation", i.e.:
 * we're in the middle of servicing the initial request for this bean. From an enhanced
 * factory method's perspective, this means that the bean does not actually yet exist,
 * and that it is now our job to create it for the first time by executing the logic
 * in the corresponding factory method.
 * <p>Said another way, this check repurposes
 * {@link ConfigurableBeanFactory#isCurrentlyInCreation(String)} to determine whether
 * the container is calling this method or the user is calling this method.
 * @param beanName name of bean to check for
 * @return whether <var>beanName</var> already exists in the factory
 */
private boolean factoryContainsBean(ConfigurableBeanFactory beanFactory, String beanName) {
	return (beanFactory.containsBean(beanName) && !beanFactory.isCurrentlyInCreation(beanName));
}
 
Example 6
Source File: ConfigurationClassEnhancer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Check the BeanFactory to see whether the bean named <var>beanName</var> already
 * exists. Accounts for the fact that the requested bean may be "in creation", i.e.:
 * we're in the middle of servicing the initial request for this bean. From an enhanced
 * factory method's perspective, this means that the bean does not actually yet exist,
 * and that it is now our job to create it for the first time by executing the logic
 * in the corresponding factory method.
 * <p>Said another way, this check repurposes
 * {@link ConfigurableBeanFactory#isCurrentlyInCreation(String)} to determine whether
 * the container is calling this method or the user is calling this method.
 * @param beanName name of bean to check for
 * @return whether <var>beanName</var> already exists in the factory
 */
private boolean factoryContainsBean(ConfigurableBeanFactory beanFactory, String beanName) {
	return (beanFactory.containsBean(beanName) && !beanFactory.isCurrentlyInCreation(beanName));
}
 
Example 7
Source File: ConfigurationClassEnhancer.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Check the BeanFactory to see whether the bean named <var>beanName</var> already
 * exists. Accounts for the fact that the requested bean may be "in creation", i.e.:
 * we're in the middle of servicing the initial request for this bean. From an enhanced
 * factory method's perspective, this means that the bean does not actually yet exist,
 * and that it is now our job to create it for the first time by executing the logic
 * in the corresponding factory method.
 * <p>Said another way, this check repurposes
 * {@link ConfigurableBeanFactory#isCurrentlyInCreation(String)} to determine whether
 * the container is calling this method or the user is calling this method.
 * @param beanName name of bean to check for
 * @return whether <var>beanName</var> already exists in the factory
 */
private boolean factoryContainsBean(ConfigurableBeanFactory beanFactory, String beanName) {
	return (beanFactory.containsBean(beanName) && !beanFactory.isCurrentlyInCreation(beanName));
}
 
Example 8
Source File: ConfigurationClassEnhancer.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Check the BeanFactory to see whether the bean named <var>beanName</var> already
 * exists. Accounts for the fact that the requested bean may be "in creation", i.e.:
 * we're in the middle of servicing the initial request for this bean. From an enhanced
 * factory method's perspective, this means that the bean does not actually yet exist,
 * and that it is now our job to create it for the first time by executing the logic
 * in the corresponding factory method.
 * <p>Said another way, this check repurposes
 * {@link ConfigurableBeanFactory#isCurrentlyInCreation(String)} to determine whether
 * the container is calling this method or the user is calling this method.
 * @param beanName name of bean to check for
 * @return whether <var>beanName</var> already exists in the factory
 */
private boolean factoryContainsBean(ConfigurableBeanFactory beanFactory, String beanName) {
	return (beanFactory.containsBean(beanName) && !beanFactory.isCurrentlyInCreation(beanName));
}