Search in sources :

Example 66 with BeanCreationException

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

the class XmlBeanFactoryTests method defaultLazyInit.

@Test
void defaultLazyInit() {
    InitAndIB.constructed = false;
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_LAZY_CONTEXT);
    assertThat(InitAndIB.constructed).isFalse();
    xbf.preInstantiateSingletons();
    assertThat(InitAndIB.constructed).isTrue();
    try {
        xbf.getBean("lazy-and-bad");
    } catch (BeanCreationException ex) {
        assertThat(ex.getCause() instanceof IOException).isTrue();
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 67 with BeanCreationException

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

the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.

@PathPatternsParameterizedTest
void responseBodyArgMismatch(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestBodyArgMismatchController.class, usePathPatterns, wac -> {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setClassesToBeBound(A.class, B.class);
        try {
            marshaller.afterPropertiesSet();
        } catch (Exception ex) {
            throw new BeanCreationException(ex.getMessage(), ex);
        }
        MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        adapterDef.getPropertyValues().add("messageConverters", messageConverter);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "<b/>";
    request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
    request.addHeader("Content-Type", "application/xml; charset=utf-8");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(400);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) ServletException(jakarta.servlet.ServletException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 68 with BeanCreationException

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

the class ContextLoaderTests method singletonDestructionOnStartupFailure.

@Test
@SuppressWarnings("resource")
void singletonDestructionOnStartupFailure() throws IOException {
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> new ClassPathXmlApplicationContext(new String[] { "/org/springframework/web/context/WEB-INF/applicationContext.xml", "/org/springframework/web/context/WEB-INF/fail.xml" }) {

        @Override
        public void refresh() throws BeansException {
            try {
                super.refresh();
            } catch (BeanCreationException ex) {
                DefaultListableBeanFactory factory = (DefaultListableBeanFactory) getBeanFactory();
                assertThat(factory.getSingletonCount()).isEqualTo(0);
                throw ex;
            }
        }
    });
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) BeansException(org.springframework.beans.BeansException) Test(org.junit.jupiter.api.Test)

Example 69 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project dhis2-core by dhis2.

the class DefaultTrackerPreheatServiceTest method shouldDoNothingWhenSupplierBeanNotFound.

@Test
void shouldDoNothingWhenSupplierBeanNotFound() {
    when(applicationContext.getBean(bean.capture(), preheatSupplierClassCaptor.capture())).thenThrow(new BeanCreationException("e"));
    preheatService.preheat(preheatParams);
    verify(applicationContext).getBean(bean.getValue(), preheatSupplierClassCaptor.getValue());
    verify(classBasedSupplier, times(0)).add(any(), any());
    verify(classBasedSupplier, times(0)).preheatAdd(any(), any());
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) Test(org.junit.jupiter.api.Test)

Example 70 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-cloud-connectors by spring-cloud.

the class CloudServiceIntroducer method postProcessBeanFactory.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    try {
        Constructor<?> ctor = serviceConnectorFactoryType.getConstructor(String.class, ServiceConnectorConfig.class);
        AbstractCloudServiceConnectorFactory<?> serviceFactory = (AbstractCloudServiceConnectorFactory<?>) ctor.newInstance(serviceId, serviceConnectorConfig);
        serviceFactory.setServiceConnectorType((Class) serviceConnectorType);
        serviceFactory.setBeanFactory(beanFactory);
        serviceFactory.afterPropertiesSet();
        // id is the beanId if specified, otherwise the serviceId
        if (StringUtils.hasText(beanId)) {
            beanFactory.registerSingleton(beanId, serviceFactory);
        } else {
            beanFactory.registerSingleton(serviceFactory.getServiceId(), serviceFactory);
        }
    } catch (Exception ex) {
        throw new BeanCreationException("Error registering service factory", ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) AbstractCloudServiceConnectorFactory(org.springframework.cloud.service.AbstractCloudServiceConnectorFactory) BeansException(org.springframework.beans.BeansException) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

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