org.springframework.beans.factory.BeanCreationException.getCause()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.8k)|赞(0)|评价(0)|浏览(159)

本文整理了Java中org.springframework.beans.factory.BeanCreationException.getCause()方法的一些代码示例,展示了BeanCreationException.getCause()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BeanCreationException.getCause()方法的具体详情如下:
包路径:org.springframework.beans.factory.BeanCreationException
类名称:BeanCreationException
方法名:getCause

BeanCreationException.getCause介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testInvalidBeanNameReference() throws Exception {
  3. try {
  4. this.beanFactory.getBean("jumble2");
  5. fail("Should have thrown BeanCreationException");
  6. }
  7. catch (BeanCreationException ex) {
  8. assertTrue(ex.getCause() instanceof BeanDefinitionStoreException);
  9. assertTrue(ex.getCause().getMessage().contains("rod2"));
  10. }
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void equivalentMappingsWithSameMethodName() throws Exception {
  3. try {
  4. initServletWithControllers(ChildController.class);
  5. fail("Expected 'method already mapped' error");
  6. }
  7. catch (BeanCreationException e) {
  8. assertTrue(e.getCause() instanceof IllegalStateException);
  9. assertTrue(e.getCause().getMessage().contains("Ambiguous mapping"));
  10. }
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testWithRequiredPropertyOmitted() {
  3. try {
  4. DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
  5. BeanDefinition beanDef = BeanDefinitionBuilder
  6. .genericBeanDefinition(RequiredTestBean.class)
  7. .addPropertyValue("name", "Rob Harrop")
  8. .addPropertyValue("favouriteColour", "Blue")
  9. .addPropertyValue("jobTitle", "Grand Poobah")
  10. .getBeanDefinition();
  11. factory.registerBeanDefinition("testBean", beanDef);
  12. factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
  13. factory.preInstantiateSingletons();
  14. fail("Should have thrown BeanCreationException");
  15. }
  16. catch (BeanCreationException ex) {
  17. String message = ex.getCause().getMessage();
  18. assertTrue(message.contains("Property"));
  19. assertTrue(message.contains("age"));
  20. assertTrue(message.contains("testBean"));
  21. }
  22. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testSpringInitBean() throws Exception {
  3. try {
  4. beanFactory.getBean("spring-init");
  5. fail("expected security exception");
  6. }
  7. catch (BeanCreationException ex) {
  8. assertTrue(ex.getCause() instanceof SecurityException);
  9. }
  10. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testWithStaticFactoryMethod() {
  3. try {
  4. DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
  5. BeanDefinition beanDef = BeanDefinitionBuilder
  6. .genericBeanDefinition(RequiredTestBean.class)
  7. .setFactoryMethod("create")
  8. .addPropertyValue("name", "Rob Harrop")
  9. .addPropertyValue("favouriteColour", "Blue")
  10. .addPropertyValue("jobTitle", "Grand Poobah")
  11. .getBeanDefinition();
  12. factory.registerBeanDefinition("testBean", beanDef);
  13. factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
  14. factory.preInstantiateSingletons();
  15. fail("Should have thrown BeanCreationException");
  16. }
  17. catch (BeanCreationException ex) {
  18. String message = ex.getCause().getMessage();
  19. assertTrue(message.contains("Property"));
  20. assertTrue(message.contains("age"));
  21. assertTrue(message.contains("testBean"));
  22. }
  23. }

代码示例来源:origin: spring-projects/spring-framework

  1. private void testDoubleTargetSourceIsRejected(String name) {
  2. try {
  3. DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
  4. new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(DBL_TARGETSOURCE_CONTEXT, CLASS));
  5. bf.getBean(name);
  6. fail("Should not allow TargetSource to be specified in interceptorNames as well as targetSource property");
  7. }
  8. catch (BeanCreationException ex) {
  9. // Root cause of the problem must be an AOP exception
  10. AopConfigException aex = (AopConfigException) ex.getCause();
  11. assertTrue(aex.getMessage().contains("TargetSource"));
  12. }
  13. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testCustomInitBean() throws Exception {
  3. try {
  4. beanFactory.getBean("custom-init");
  5. fail("expected security exception");
  6. }
  7. catch (BeanCreationException ex) {
  8. assertTrue(ex.getCause() instanceof SecurityException);
  9. }
  10. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testCustomFactoryObject() throws Exception {
  3. try {
  4. beanFactory.getBean("spring-factory");
  5. fail("expected security exception");
  6. }
  7. catch (BeanCreationException ex) {
  8. assertTrue(ex.getCause() instanceof SecurityException);
  9. }
  10. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testWithThreeRequiredPropertiesOmitted() {
  3. try {
  4. DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
  5. BeanDefinition beanDef = BeanDefinitionBuilder
  6. .genericBeanDefinition(RequiredTestBean.class)
  7. .addPropertyValue("name", "Rob Harrop")
  8. .getBeanDefinition();
  9. factory.registerBeanDefinition("testBean", beanDef);
  10. factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
  11. factory.preInstantiateSingletons();
  12. fail("Should have thrown BeanCreationException");
  13. }
  14. catch (BeanCreationException ex) {
  15. String message = ex.getCause().getMessage();
  16. assertTrue(message.contains("Properties"));
  17. assertTrue(message.contains("age"));
  18. assertTrue(message.contains("favouriteColour"));
  19. assertTrue(message.contains("jobTitle"));
  20. assertTrue(message.contains("testBean"));
  21. }
  22. }

代码示例来源:origin: spring-projects/spring-framework

  1. public void xtestTypeMismatch() {
  2. try {
  3. getBeanFactory().getBean("typeMismatch");
  4. fail("Shouldn't succeed with type mismatch");
  5. }
  6. catch (BeanCreationException wex) {
  7. assertEquals("typeMismatch", wex.getBeanName());
  8. assertTrue(wex.getCause() instanceof PropertyBatchUpdateException);
  9. PropertyBatchUpdateException ex = (PropertyBatchUpdateException) wex.getCause();
  10. // Further tests
  11. assertTrue("Has one error ", ex.getExceptionCount() == 1);
  12. assertTrue("Error is for field age", ex.getPropertyAccessException("age") != null);
  13. assertTrue("We have rejected age in exception", ex.getPropertyAccessException("age").getPropertyChangeEvent().getNewValue().equals("34x"));
  14. }
  15. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testWithCustomAnnotation() {
  3. try {
  4. DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
  5. BeanDefinition beanDef = BeanDefinitionBuilder
  6. .genericBeanDefinition(RequiredTestBean.class)
  7. .getBeanDefinition();
  8. factory.registerBeanDefinition("testBean", beanDef);
  9. RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor();
  10. rabpp.setRequiredAnnotationType(MyRequired.class);
  11. factory.addBeanPostProcessor(rabpp);
  12. factory.preInstantiateSingletons();
  13. fail("Should have thrown BeanCreationException");
  14. }
  15. catch (BeanCreationException ex) {
  16. String message = ex.getCause().getMessage();
  17. assertTrue(message.contains("Property"));
  18. assertTrue(message.contains("name"));
  19. assertTrue(message.contains("testBean"));
  20. }
  21. }

代码示例来源:origin: spring-projects/spring-batch

  1. @Test
  2. public void testJobLaunchingGatewayNoJobLauncher() throws Exception {
  3. try {
  4. setUp("JobLaunchingGatewayParserTestsNoJobLauncher-context.xml", getClass());
  5. }
  6. catch(BeanCreationException e) {
  7. assertEquals("No bean named 'jobLauncher' available", e.getCause().getMessage());
  8. return;
  9. }
  10. fail("Expected a NoSuchBeanDefinitionException to be thrown.");
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testTargetSourceNotAtEndOfInterceptorNamesIsRejected() {
  3. try {
  4. DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
  5. new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(NOTLAST_TARGETSOURCE_CONTEXT, CLASS));
  6. bf.getBean("targetSourceNotLast");
  7. fail("TargetSource or non-advised object must be last in interceptorNames");
  8. }
  9. catch (BeanCreationException ex) {
  10. // Root cause of the problem must be an AOP exception
  11. AopConfigException aex = (AopConfigException) ex.getCause();
  12. assertTrue(aex.getMessage().contains("interceptorNames"));
  13. }
  14. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. @SuppressWarnings("resource")
  3. public void testServletContextAttributeFactoryBeanWithAttributeNotFound() {
  4. MockServletContext sc = new MockServletContext();
  5. StaticWebApplicationContext wac = new StaticWebApplicationContext();
  6. wac.setServletContext(sc);
  7. MutablePropertyValues pvs = new MutablePropertyValues();
  8. pvs.add("attributeName", "myAttr");
  9. wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs);
  10. try {
  11. wac.refresh();
  12. fail("Should have thrown BeanCreationException");
  13. }
  14. catch (BeanCreationException ex) {
  15. // expected
  16. assertTrue(ex.getCause() instanceof IllegalStateException);
  17. assertTrue(ex.getCause().getMessage().contains("myAttr"));
  18. }
  19. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. @SuppressWarnings("resource")
  3. public void testServletContextParameterFactoryBeanWithAttributeNotFound() {
  4. MockServletContext sc = new MockServletContext();
  5. StaticWebApplicationContext wac = new StaticWebApplicationContext();
  6. wac.setServletContext(sc);
  7. MutablePropertyValues pvs = new MutablePropertyValues();
  8. pvs.add("initParamName", "myParam");
  9. wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs);
  10. try {
  11. wac.refresh();
  12. fail("Should have thrown BeanCreationException");
  13. }
  14. catch (BeanCreationException ex) {
  15. // expected
  16. assertTrue(ex.getCause() instanceof IllegalStateException);
  17. assertTrue(ex.getCause().getMessage().contains("myParam"));
  18. }
  19. }

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Globals must be followed by a target.
  3. */
  4. @Test
  5. public void testGlobalsWithoutTarget() {
  6. DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
  7. new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(INVALID_CONTEXT, CLASS));
  8. try {
  9. bf.getBean("globalsWithoutTarget");
  10. fail("Should require target name");
  11. }
  12. catch (BeanCreationException ex) {
  13. assertTrue(ex.getCause() instanceof AopConfigException);
  14. }
  15. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testPossibleMatches() {
  3. try {
  4. DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
  5. MutablePropertyValues pvs = new MutablePropertyValues();
  6. pvs.add("ag", "foobar");
  7. RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
  8. bd.setPropertyValues(pvs);
  9. lbf.registerBeanDefinition("tb", bd);
  10. lbf.getBean("tb");
  11. fail("Should throw exception on invalid property");
  12. }
  13. catch (BeanCreationException ex) {
  14. assertTrue(ex.getCause() instanceof NotWritablePropertyException);
  15. NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause();
  16. // expected
  17. assertEquals(1, cause.getPossibleMatches().length);
  18. assertEquals("age", cause.getPossibleMatches()[0]);
  19. }
  20. }

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Test that if a custom initializer throws an exception, it's handled correctly
  3. */
  4. @Test
  5. public void testInitMethodThrowsException() {
  6. DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
  7. new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
  8. try {
  9. xbf.getBean("init-method2");
  10. fail();
  11. }
  12. catch (BeanCreationException ex) {
  13. assertTrue(ex.getResourceDescription().contains("initializers.xml"));
  14. assertEquals("init-method2", ex.getBeanName());
  15. assertTrue(ex.getCause() instanceof IOException);
  16. }
  17. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testDefaultLazyInit() throws Exception {
  3. InitAndIB.constructed = false;
  4. DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
  5. new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_LAZY_CONTEXT);
  6. assertFalse(InitAndIB.constructed);
  7. xbf.preInstantiateSingletons();
  8. assertTrue(InitAndIB.constructed);
  9. try {
  10. xbf.getBean("lazy-and-bad");
  11. }
  12. catch (BeanCreationException ex) {
  13. assertTrue(ex.getCause() instanceof IOException);
  14. }
  15. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void eventListenerWorksWithCustomScope() throws Exception {
  3. load(CustomScopeTestBean.class);
  4. CustomScope customScope = new CustomScope();
  5. this.context.getBeanFactory().registerScope("custom", customScope);
  6. CustomScopeTestBean proxy = this.context.getBean(CustomScopeTestBean.class);
  7. assertTrue("bean should be a cglib proxy", AopUtils.isCglibProxy(proxy));
  8. this.eventCollector.assertNoEventReceived(proxy.getId());
  9. this.context.publishEvent(new ContextRefreshedEvent(this.context));
  10. this.eventCollector.assertNoEventReceived(proxy.getId());
  11. customScope.active = false;
  12. this.context.publishEvent(new ContextRefreshedEvent(this.context));
  13. customScope.active = true;
  14. this.eventCollector.assertNoEventReceived(proxy.getId());
  15. TestEvent event = new TestEvent();
  16. this.context.publishEvent(event);
  17. this.eventCollector.assertEvent(proxy.getId(), event);
  18. this.eventCollector.assertTotalEventsCount(1);
  19. try {
  20. customScope.active = false;
  21. this.context.publishEvent(new TestEvent());
  22. fail("Should have thrown IllegalStateException");
  23. }
  24. catch (BeanCreationException ex) {
  25. // expected
  26. assertTrue(ex.getCause() instanceof IllegalStateException);
  27. }
  28. }

相关文章