use of org.springframework.beans.factory.BeanCreationException in project zipkin by openzipkin.
the class ZipkinRabbitMQCollectorConfigurationTest method providesCollectorComponent_whenAddressesSet.
@Test
public void providesCollectorComponent_whenAddressesSet() {
context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("zipkin.collector.rabbitmq.addresses:localhost:1234").applyTo(context);
context.register(PropertyPlaceholderAutoConfiguration.class, ZipkinRabbitMQCollectorConfiguration.class, InMemoryConfiguration.class);
try {
context.refresh();
failBecauseExceptionWasNotThrown(BeanCreationException.class);
} catch (BeanCreationException e) {
assertThat(e.getCause()).hasMessageContaining("Unable to establish connection to RabbitMQ server: Connection refused");
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class StaticListableBeanFactory method getBean.
// ---------------------------------------------------------------------
// Implementation of BeanFactory interface
// ---------------------------------------------------------------------
@Override
public Object getBean(String name) throws BeansException {
String beanName = BeanFactoryUtils.transformedBeanName(name);
Object bean = this.beans.get(beanName);
if (bean == null) {
throw new NoSuchBeanDefinitionException(beanName, "Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]");
}
// bean factory if the bean isn't a factory
if (BeanFactoryUtils.isFactoryDereference(name) && !(bean instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(beanName, bean.getClass());
}
if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
try {
Object exposedObject = ((FactoryBean<?>) bean).getObject();
if (exposedObject == null) {
throw new BeanCreationException(beanName, "FactoryBean exposed null object");
}
return exposedObject;
} catch (Exception ex) {
throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
}
} else {
return bean;
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class BeanConfigurerSupport method configureBean.
/**
* Configure the bean instance.
* <p>Subclasses can override this to provide custom configuration logic.
* Typically called by an aspect, for all bean instances matched by a pointcut.
* @param beanInstance the bean instance to configure (must <b>not</b> be {@code null})
*/
public void configureBean(Object beanInstance) {
if (this.beanFactory == null) {
if (logger.isDebugEnabled()) {
logger.debug("BeanFactory has not been set on " + ClassUtils.getShortName(getClass()) + ": " + "Make sure this configurer runs in a Spring container. Unable to configure bean of type [" + ClassUtils.getDescriptiveType(beanInstance) + "]. Proceeding without injection.");
}
return;
}
BeanWiringInfoResolver bwiResolver = this.beanWiringInfoResolver;
Assert.state(bwiResolver != null, "No BeanWiringInfoResolver available");
BeanWiringInfo bwi = bwiResolver.resolveWiringInfo(beanInstance);
if (bwi == null) {
// Skip the bean if no wiring info given.
return;
}
ConfigurableListableBeanFactory beanFactory = this.beanFactory;
Assert.state(beanFactory != null, "No BeanFactory available");
try {
String beanName = bwi.getBeanName();
if (bwi.indicatesAutowiring() || (bwi.isDefaultBeanName() && beanName != null && !beanFactory.containsBean(beanName))) {
// Perform autowiring (also applying standard factory / post-processor callbacks).
beanFactory.autowireBeanProperties(beanInstance, bwi.getAutowireMode(), bwi.getDependencyCheck());
beanFactory.initializeBean(beanInstance, (beanName != null ? beanName : ""));
} else {
// Perform explicit wiring based on the specified bean definition.
beanFactory.configureBean(beanInstance, (beanName != null ? beanName : ""));
}
} catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
if (rootCause instanceof BeanCurrentlyInCreationException) {
BeanCreationException bce = (BeanCreationException) rootCause;
String bceBeanName = bce.getBeanName();
if (bceBeanName != null && beanFactory.isCurrentlyInCreation(bceBeanName)) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to create target bean '" + bce.getBeanName() + "' while configuring object of type [" + beanInstance.getClass().getName() + "] - probably due to a circular reference. This is a common startup situation " + "and usually not fatal. Proceeding without injection. Original exception: " + ex);
}
return;
}
}
throw ex;
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class FactoryBeanRegistrySupport method getObjectFromFactoryBean.
/**
* Obtain an object to expose from the given FactoryBean.
* @param factory the FactoryBean instance
* @param beanName the name of the bean
* @param shouldPostProcess whether the bean is subject to post-processing
* @return the object obtained from the FactoryBean
* @throws BeanCreationException if FactoryBean object creation failed
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
if (factory.isSingleton() && containsSingleton(beanName)) {
synchronized (getSingletonMutex()) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
object = doGetObjectFromFactoryBean(factory, beanName);
// Only post-process and store if not put there already during getObject() call above
// (e.g. because of circular reference processing triggered by custom getBean calls)
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {
object = alreadyThere;
} else {
if (shouldPostProcess) {
if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily return non-post-processed object, not storing it yet..
return object;
}
beforeSingletonCreation(beanName);
try {
object = postProcessObjectFromFactoryBean(object, beanName);
} catch (Throwable ex) {
throw new BeanCreationException(beanName, "Post-processing of FactoryBean's singleton object failed", ex);
} finally {
afterSingletonCreation(beanName);
}
}
if (containsSingleton(beanName)) {
this.factoryBeanObjectCache.put(beanName, object);
}
}
}
return object;
}
} else {
Object object = doGetObjectFromFactoryBean(factory, beanName);
if (shouldPostProcess) {
try {
object = postProcessObjectFromFactoryBean(object, beanName);
} catch (Throwable ex) {
throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
}
}
return object;
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class ScriptFactoryPostProcessor method predictBeanType.
@Override
@Nullable
public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
// We only apply special treatment to ScriptFactory implementations here.
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
return null;
}
Assert.state(this.beanFactory != null, "No BeanFactory set");
BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
try {
String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
Class<?> scriptedType = scriptFactory.getScriptedObjectType(scriptSource);
if (scriptedType != null) {
return scriptedType;
} else if (!ObjectUtils.isEmpty(interfaces)) {
return (interfaces.length == 1 ? interfaces[0] : createCompositeInterface(interfaces));
} else {
if (bd.isSingleton()) {
return this.scriptBeanFactory.getBean(scriptedObjectBeanName).getClass();
}
}
} catch (Exception ex) {
if (ex instanceof BeanCreationException && ((BeanCreationException) ex).getMostSpecificCause() instanceof BeanCurrentlyInCreationException) {
if (logger.isTraceEnabled()) {
logger.trace("Could not determine scripted object type for bean '" + beanName + "': " + ex.getMessage());
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Could not determine scripted object type for bean '" + beanName + "'", ex);
}
}
}
return null;
}
Aggregations