org.springframework.beans.factory.BeanCreationException#getRootCause ( )源码实例Demo

下面列出了org.springframework.beans.factory.BeanCreationException#getRootCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Verify that embedded h2 does not start if h2 url is specified with with the
 * spring.dataflow.embedded.database.enabled is set to false.
 *
 * @throws Throwable if any error occurs and should be handled by the caller.
 */
@Test(expected = ConnectException.class)
public void testDoNotStartEmbeddedH2Server() throws Throwable {
	Throwable exceptionResult = null;
	Map<String, Object> myMap = new HashMap<>();
	myMap.put("spring.datasource.url", "jdbc:h2:tcp://localhost:19092/mem:dataflow");
	myMap.put("spring.dataflow.embedded.database.enabled", "false");
	myMap.put("spring.jpa.database", "H2");
	propertySources.addFirst(new MapPropertySource("EnvironmentTestPropsource", myMap));
	context.setEnvironment(environment);
	try {
		context.refresh();
	}
	catch (BeanCreationException exception) {
		exceptionResult = exception.getRootCause();
	}
	assertNotNull(exceptionResult);
	throw exceptionResult;
}
 
源代码2 项目: spring-analysis-note   文件: EnableCachingTests.java
@Test
public void multipleCachingConfigurers() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
	try {
		ctx.refresh();
	}
	catch (BeanCreationException ex) {
		Throwable root = ex.getRootCause();
		assertTrue(root instanceof IllegalStateException);
		assertTrue(root.getMessage().contains("implementations of CachingConfigurer"));
	}
}
 
@Test
public void testExceptionOnConfigParsingWithMismatchedAdviceMethod() {
	try {
		new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
	}
	catch (BeanCreationException ex) {
		Throwable cause = ex.getRootCause();
		assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException);
		assertTrue("invalidAbsoluteTypeName should be detected by AJ",
				cause.getMessage().contains("invalidAbsoluteTypeName"));
	}
}
 
@Test
public void testExceptionOnConfigParsingWithAmbiguousAdviceMethod() {
	try {
		new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ambiguous.xml", getClass());
	}
	catch (BeanCreationException ex) {
		Throwable cause = ex.getRootCause();
		assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException);
		assertTrue("Cannot resolve method 'myBeforeAdvice' to a unique method",
				cause.getMessage().contains("Cannot resolve method 'myBeforeAdvice' to a unique method"));
	}
}
 
@Test
public void multipleCachingConfigurers() {
	try {
		load(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
	}
	catch (BeanCreationException ex) {
		Throwable root = ex.getRootCause();
		assertTrue(root instanceof IllegalStateException);
		assertTrue(ex.getMessage().contains("implementations of CachingConfigurer"));
	}
}
 
@Test
public void multipleCachingConfigurers() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
	try {
		ctx.refresh();
	}
	catch (BeanCreationException ex) {
		Throwable root = ex.getRootCause();
		assertTrue(root instanceof IllegalStateException);
		assertTrue(root.getMessage().contains("implementations of CachingConfigurer"));
	}
}
 
@Test
public void testExceptionOnConfigParsingWithMismatchedAdviceMethod() {
	try {
		new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
	}
	catch (BeanCreationException ex) {
		Throwable cause = ex.getRootCause();
		assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException);
		assertTrue("invalidAbsoluteTypeName should be detected by AJ",
				cause.getMessage().contains("invalidAbsoluteTypeName"));
	}
}
 
@Test
public void testExceptionOnConfigParsingWithAmbiguousAdviceMethod() {
	try {
		new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ambiguous.xml", getClass());
	}
	catch (BeanCreationException ex) {
		Throwable cause = ex.getRootCause();
		assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException);
		assertTrue("Cannot resolve method 'myBeforeAdvice' to a unique method",
				cause.getMessage().contains("Cannot resolve method 'myBeforeAdvice' to a unique method"));
	}
}
 
@Test
public void multipleCachingConfigurers() {
	try {
		load(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
	}
	catch (BeanCreationException ex) {
		Throwable root = ex.getRootCause();
		assertTrue(root instanceof IllegalStateException);
		assertTrue(ex.getMessage().contains("implementations of CachingConfigurer"));
	}
}
 
源代码10 项目: spring4-understanding   文件: EnableCachingTests.java
@Test(expected = IllegalStateException.class)
public void multipleCacheManagerBeans() throws Throwable {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(MultiCacheManagerConfig.class);
	try {
		ctx.refresh();
	}
	catch (BeanCreationException ex) {
		Throwable root = ex.getRootCause();
		assertTrue(root.getMessage().contains("beans of type CacheManager"));
		throw root;
	}
}
 
源代码11 项目: spring4-understanding   文件: EnableCachingTests.java
@Test(expected = IllegalStateException.class)
public void multipleCachingConfigurers() throws Throwable {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
	try {
		ctx.refresh();
	}
	catch (BeanCreationException ex) {
		Throwable root = ex.getRootCause();
		assertTrue(root.getMessage().contains("implementations of CachingConfigurer"));
		throw root;
	}
}
 
源代码12 项目: spring4-understanding   文件: EnableCachingTests.java
@Test(expected = IllegalStateException.class)
public void noCacheManagerBeans() throws Throwable {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(EmptyConfig.class);
	try {
		ctx.refresh();
	}
	catch (BeanCreationException ex) {
		Throwable root = ex.getRootCause();
		assertTrue(root.getMessage().contains("No bean of type CacheManager"));
		throw root;
	}
}
 
@Test
public void testExceptionOnConfigParsingWithMismatchedAdviceMethod() {
	try {
		new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
	}
	catch (BeanCreationException ex) {
		Throwable cause = ex.getRootCause();
		assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException);
		assertTrue("invalidAbsoluteTypeName should be detected by AJ",
				cause.getMessage().indexOf("invalidAbsoluteTypeName") != -1);
	}
}
 
@Test
public void testExceptionOnConfigParsingWithAmbiguousAdviceMethod() {
	try {
		new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ambiguous.xml", getClass());
	}
	catch (BeanCreationException ex) {
		Throwable cause = ex.getRootCause();
		assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException);
		assertTrue("Cannot resolve method 'myBeforeAdvice' to a unique method",
				cause.getMessage().indexOf("Cannot resolve method 'myBeforeAdvice' to a unique method") != -1);
	}
}
 
@Test
public void testExtendedResourceInjectionWithOverriding() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	TestBean tb5 = new TestBean();
	annotatedBd.getPropertyValues().add("testBean2", tb5);
	bf.registerBeanDefinition("annotatedBean", annotatedBd);
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb5, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	try {
		bf.getBean("annotatedBean2");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException);
		NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
		assertEquals("testBean9", innerEx.getBeanName());
	}

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
@Test
public void testExtendedResourceInjectionWithOverriding() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	TestBean tb5 = new TestBean();
	annotatedBd.getPropertyValues().add("testBean2", tb5);
	bf.registerBeanDefinition("annotatedBean", annotatedBd);
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb5, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	try {
		bf.getBean("annotatedBean2");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException);
		NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
		assertEquals("testBean9", innerEx.getBeanName());
	}

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
@Test
public void testExtendedResourceInjectionWithOverriding() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
	TestBean tb5 = new TestBean();
	annotatedBd.getPropertyValues().add("testBean2", tb5);
	bf.registerBeanDefinition("annotatedBean", annotatedBd);
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb5, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	try {
		bf.getBean("annotatedBean2");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException);
		NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
		assertEquals("testBean9", innerEx.getBeanName());
	}

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}