Search in sources :

Example 86 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-boot by spring-projects.

the class ConfigurationPropertiesBindingPostProcessor method postProcessBeforeInitialization.

private void postProcessBeforeInitialization(Object bean, String beanName, ConfigurationProperties annotation) {
    Object target = bean;
    PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<>(target);
    factory.setPropertySources(this.propertySources);
    factory.setValidator(determineValidator(bean));
    // If no explicit conversion service is provided we add one so that (at least)
    // comma-separated arrays of convertibles can be bound automatically
    factory.setConversionService(this.conversionService == null ? getDefaultConversionService() : this.conversionService);
    if (annotation != null) {
        factory.setIgnoreInvalidFields(annotation.ignoreInvalidFields());
        factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
        factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
        if (StringUtils.hasLength(annotation.prefix())) {
            factory.setTargetName(annotation.prefix());
        }
    }
    try {
        factory.bindPropertiesToTarget();
    } catch (Exception ex) {
        String targetClass = ClassUtils.getShortName(target.getClass());
        throw new BeanCreationException(beanName, "Could not bind properties to " + targetClass + " (" + getAnnotationDetails(annotation) + ")", ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertiesConfigurationFactory(org.springframework.boot.bind.PropertiesConfigurationFactory) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 87 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testIncompleteBeanDefinition.

@Test
public void testIncompleteBeanDefinition() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("testBean", new GenericBeanDefinition());
    try {
        bf.getBean("testBean");
    } catch (BeanCreationException ex) {
        assertTrue(ex.getRootCause() instanceof IllegalStateException);
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 88 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class RequiredAnnotationBeanPostProcessorTests method testWithThreeRequiredPropertiesOmitted.

@Test
public void testWithThreeRequiredPropertiesOmitted() {
    try {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).addPropertyValue("name", "Rob Harrop").getBeanDefinition();
        factory.registerBeanDefinition("testBean", beanDef);
        factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
        factory.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        String message = ex.getCause().getMessage();
        assertTrue(message.contains("Properties"));
        assertTrue(message.contains("age"));
        assertTrue(message.contains("favouriteColour"));
        assertTrue(message.contains("jobTitle"));
        assertTrue(message.contains("testBean"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 89 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class RequiredAnnotationBeanPostProcessorTests method testWithStaticFactoryMethod.

@Test
public void testWithStaticFactoryMethod() {
    try {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).setFactoryMethod("create").addPropertyValue("name", "Rob Harrop").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
        factory.registerBeanDefinition("testBean", beanDef);
        factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
        factory.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        String message = ex.getCause().getMessage();
        assertTrue(message.contains("Property"));
        assertTrue(message.contains("age"));
        assertTrue(message.contains("testBean"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 90 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class JaxWsSupportTests method doTestJaxWsPortAccess.

private void doTestJaxWsPortAccess(WebServiceFeature... features) throws Exception {
    GenericApplicationContext ac = new GenericApplicationContext();
    GenericBeanDefinition serviceDef = new GenericBeanDefinition();
    serviceDef.setBeanClass(OrderServiceImpl.class);
    ac.registerBeanDefinition("service", serviceDef);
    GenericBeanDefinition exporterDef = new GenericBeanDefinition();
    exporterDef.setBeanClass(SimpleJaxWsServiceExporter.class);
    exporterDef.getPropertyValues().add("baseAddress", "http://localhost:9999/");
    ac.registerBeanDefinition("exporter", exporterDef);
    GenericBeanDefinition clientDef = new GenericBeanDefinition();
    clientDef.setBeanClass(JaxWsPortProxyFactoryBean.class);
    clientDef.getPropertyValues().add("wsdlDocumentUrl", "http://localhost:9999/OrderService?wsdl");
    clientDef.getPropertyValues().add("namespaceUri", "http://jaxws.remoting.springframework.org/");
    clientDef.getPropertyValues().add("username", "juergen");
    clientDef.getPropertyValues().add("password", "hoeller");
    clientDef.getPropertyValues().add("serviceName", "OrderService");
    clientDef.getPropertyValues().add("serviceInterface", OrderService.class);
    clientDef.getPropertyValues().add("lookupServiceOnStartup", Boolean.FALSE);
    if (features != null) {
        clientDef.getPropertyValues().add("portFeatures", features);
    }
    ac.registerBeanDefinition("client", clientDef);
    GenericBeanDefinition serviceFactoryDef = new GenericBeanDefinition();
    serviceFactoryDef.setBeanClass(LocalJaxWsServiceFactoryBean.class);
    serviceFactoryDef.getPropertyValues().add("wsdlDocumentUrl", "http://localhost:9999/OrderService?wsdl");
    serviceFactoryDef.getPropertyValues().add("namespaceUri", "http://jaxws.remoting.springframework.org/");
    serviceFactoryDef.getPropertyValues().add("serviceName", "OrderService");
    ac.registerBeanDefinition("orderService", serviceFactoryDef);
    ac.registerBeanDefinition("accessor", new RootBeanDefinition(ServiceAccessor.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
    try {
        ac.refresh();
        OrderService orderService = ac.getBean("client", OrderService.class);
        assertTrue(orderService instanceof BindingProvider);
        ((BindingProvider) orderService).getRequestContext();
        String order = orderService.getOrder(1000);
        assertEquals("order 1000", order);
        try {
            orderService.getOrder(0);
            fail("Should have thrown OrderNotFoundException");
        } catch (OrderNotFoundException ex) {
        // expected
        } catch (RemoteAccessException ex) {
        // ignore - probably setup issue with JAX-WS provider vs JAXB
        }
        ServiceAccessor serviceAccessor = ac.getBean("accessor", ServiceAccessor.class);
        order = serviceAccessor.orderService.getOrder(1000);
        assertEquals("order 1000", order);
        try {
            serviceAccessor.orderService.getOrder(0);
            fail("Should have thrown OrderNotFoundException");
        } catch (OrderNotFoundException ex) {
        // expected
        } catch (WebServiceException ex) {
        // ignore - probably setup issue with JAX-WS provider vs JAXB
        }
    } catch (BeanCreationException ex) {
        if ("exporter".equals(ex.getBeanName()) && ex.getRootCause() instanceof ClassNotFoundException) {
        // ignore - probably running on JDK without the JAX-WS impl present
        } else {
            throw ex;
        }
    } finally {
        ac.close();
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) WebServiceException(javax.xml.ws.WebServiceException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BindingProvider(javax.xml.ws.BindingProvider)

Aggregations

BeanCreationException (org.springframework.beans.factory.BeanCreationException)133 Test (org.junit.Test)30 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)18 BeansException (org.springframework.beans.BeansException)16 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)13 IOException (java.io.IOException)12 Map (java.util.Map)12 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 Test (org.junit.jupiter.api.Test)11 HashMap (java.util.HashMap)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 ArrayList (java.util.ArrayList)7 PostConstruct (javax.annotation.PostConstruct)7 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)7 Bean (org.springframework.context.annotation.Bean)7 HasId (org.apache.camel.spi.HasId)6 BeanCurrentlyInCreationException (org.springframework.beans.factory.BeanCurrentlyInCreationException)6 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)6 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)5