use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method testRelatedCausesFromConstructorResolution.
@Test
public void testRelatedCausesFromConstructorResolution() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
try {
xbf.getBean("rod2Accessor");
} catch (BeanCreationException ex) {
assertTrue(ex.toString().indexOf("touchy") != -1);
ex.printStackTrace();
assertNull(ex.getRelatedCauses());
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method testFailsOnInnerBean.
@Test
public void testFailsOnInnerBean() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.loadBeanDefinitions(REFTYPES_CONTEXT);
try {
xbf.getBean("failsOnInnerBean");
} catch (BeanCreationException ex) {
// Check whether message contains outer bean name.
ex.printStackTrace();
assertTrue(ex.getMessage().contains("failsOnInnerBean"));
assertTrue(ex.getMessage().contains("someMap"));
}
try {
xbf.getBean("failsOnInnerBeanForConstructor");
} catch (BeanCreationException ex) {
// Check whether message contains outer bean name.
ex.printStackTrace();
assertTrue(ex.getMessage().contains("failsOnInnerBeanForConstructor"));
assertTrue(ex.getMessage().contains("constructor argument"));
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method testNonLenientDependencyMatching.
@Test
public void testNonLenientDependencyMatching() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBean");
bd.setLenientConstructorResolution(false);
try {
xbf.getBean("lenientDependencyTestBean");
fail("Should have thrown BeanCreationException");
} catch (BeanCreationException ex) {
// expected
ex.printStackTrace();
assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class AnnotationDrivenEventListenerTests method eventListenerWorksWithCustomScope.
@Test
public void eventListenerWorksWithCustomScope() throws Exception {
load(CustomScopeTestBean.class);
CustomScope customScope = new CustomScope();
this.context.getBeanFactory().registerScope("custom", customScope);
CustomScopeTestBean proxy = this.context.getBean(CustomScopeTestBean.class);
assertTrue("bean should be a cglib proxy", AopUtils.isCglibProxy(proxy));
this.eventCollector.assertNoEventReceived(proxy.getId());
this.context.publishEvent(new ContextRefreshedEvent(this.context));
this.eventCollector.assertNoEventReceived(proxy.getId());
customScope.active = false;
this.context.publishEvent(new ContextRefreshedEvent(this.context));
customScope.active = true;
this.eventCollector.assertNoEventReceived(proxy.getId());
TestEvent event = new TestEvent();
this.context.publishEvent(event);
this.eventCollector.assertEvent(proxy.getId(), event);
this.eventCollector.assertTotalEventsCount(1);
try {
customScope.active = false;
this.context.publishEvent(new TestEvent());
fail("Should have thrown IllegalStateException");
} catch (BeanCreationException ex) {
// expected
assertTrue(ex.getCause() instanceof IllegalStateException);
}
}
use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.
the class BeanDefinitionValueResolver method resolveReference.
/**
* Resolve a reference to another bean in the factory.
*/
private Object resolveReference(Object argName, RuntimeBeanReference ref) {
try {
String refName = ref.getBeanName();
refName = String.valueOf(doEvaluate(refName));
if (ref.isToParent()) {
if (this.beanFactory.getParentBeanFactory() == null) {
throw new BeanCreationException(this.beanDefinition.getResourceDescription(), this.beanName, "Can't resolve reference to bean '" + refName + "' in parent factory: no parent factory available");
}
return this.beanFactory.getParentBeanFactory().getBean(refName);
} else {
Object bean = this.beanFactory.getBean(refName);
this.beanFactory.registerDependentBean(refName, this.beanName);
return bean;
}
} catch (BeansException ex) {
throw new BeanCreationException(this.beanDefinition.getResourceDescription(), this.beanName, "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
}
}
Aggregations