Search in sources :

Example 16 with BeanCreationException

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

the class ConsumerDetailsFactoryBean method getObject.

public ConsumerDetails getObject() throws Exception {
    if ("rsa-cert".equals(typeOfSecret)) {
        try {
            Certificate cert = CertificateFactory.getInstance("X.509").generateCertificate(resourceLoader.getResource(secret).getInputStream());
            consumer.setSignatureSecret(new RSAKeySecret(cert.getPublicKey()));
        } catch (IOException e) {
            throw new BeanCreationException("RSA certificate not found at " + secret + ".", e);
        } catch (CertificateException e) {
            throw new BeanCreationException("Invalid RSA certificate at " + secret + ".", e);
        } catch (NullPointerException e) {
            throw new BeanCreationException("Could not load RSA certificate at " + secret + ".", e);
        }
    } else {
        consumer.setSignatureSecret(new SharedConsumerSecretImpl(secret));
    }
    return consumer;
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) SharedConsumerSecretImpl(org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl) RSAKeySecret(org.springframework.security.oauth.common.signature.RSAKeySecret) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) Certificate(java.security.cert.Certificate)

Example 17 with BeanCreationException

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

the class AutowiredAnnotationBeanPostProcessor method processInjection.

/**
	 * 'Native' processing method for direct calls with an arbitrary target instance,
	 * resolving all of its fields and methods which are annotated with {@code @Autowired}.
	 * @param bean the target instance to process
	 * @throws BeanCreationException if autowiring failed
	 */
public void processInjection(Object bean) throws BeanCreationException {
    Class<?> clazz = bean.getClass();
    InjectionMetadata metadata = findAutowiringMetadata(clazz.getName(), clazz, null);
    try {
        metadata.inject(bean, null, null);
    } catch (BeanCreationException ex) {
        throw ex;
    } catch (Throwable ex) {
        throw new BeanCreationException("Injection of autowired dependencies failed for class [" + clazz + "]", ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 18 with BeanCreationException

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

the class RequiredAnnotationBeanPostProcessorTests method testWithRequiredPropertyOmitted.

@Test
public void testWithRequiredPropertyOmitted() {
    try {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).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 19 with BeanCreationException

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

the class RequiredAnnotationBeanPostProcessorTests method testWithCustomAnnotation.

@Test
public void testWithCustomAnnotation() {
    try {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).getBeanDefinition();
        factory.registerBeanDefinition("testBean", beanDef);
        RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor();
        rabpp.setRequiredAnnotationType(MyRequired.class);
        factory.addBeanPostProcessor(rabpp);
        factory.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        String message = ex.getCause().getMessage();
        assertTrue(message.contains("Property"));
        assertTrue(message.contains("name"));
        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 20 with BeanCreationException

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

the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.

@Test
public void responseBodyArgMismatch() throws ServletException, IOException {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext 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);
        }
    }, RequestBodyArgMismatchController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "<b/>";
    request.setContent(requestBody.getBytes("UTF-8"));
    request.addHeader("Content-Type", "application/xml; charset=utf-8");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(400, response.getStatus());
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

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