Search in sources :

Example 36 with BeanInitializationException

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithInvalidKey.

@Test
public void testPropertyOverrideConfigurerWithInvalidKey() {
    factory.registerBeanDefinition("tb1", genericBeanDefinition(TestBean.class).getBeanDefinition());
    factory.registerBeanDefinition("tb2", genericBeanDefinition(TestBean.class).getBeanDefinition());
    {
        PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
        poc.setIgnoreInvalidKeys(true);
        Properties props = new Properties();
        props.setProperty("argh", "hgra");
        props.setProperty("tb2.name", "test");
        props.setProperty("tb2.nam", "test");
        props.setProperty("tb3.name", "test");
        poc.setProperties(props);
        poc.postProcessBeanFactory(factory);
        assertThat(factory.getBean("tb2", TestBean.class).getName()).isEqualTo("test");
    }
    {
        PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
        Properties props = new Properties();
        props.setProperty("argh", "hgra");
        props.setProperty("tb2.age", "99");
        props.setProperty("tb2.name", "test2");
        poc.setProperties(props);
        // won't actually do anything since we're not processing through an app ctx
        poc.setOrder(0);
        try {
            poc.postProcessBeanFactory(factory);
        } catch (BeanInitializationException ex) {
            // prove that the processor chokes on the invalid key
            assertThat(ex.getMessage().toLowerCase().contains("argh")).isTrue();
        }
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 37 with BeanInitializationException

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

the class WebMvcConfigurationSupport method mvcValidator.

/**
 * Return a global {@link Validator} instance for example for validating
 * {@code @ModelAttribute} and {@code @RequestBody} method arguments.
 * Delegates to {@link #getValidator()} first and if that returns {@code null}
 * checks the classpath for the presence of a JSR-303 implementations
 * before creating a {@code OptionalValidatorFactoryBean}.If a JSR-303
 * implementation is not available, a no-op {@link Validator} is returned.
 */
@Bean
public Validator mvcValidator() {
    Validator validator = getValidator();
    if (validator == null) {
        if (ClassUtils.isPresent("jakarta.validation.Validator", getClass().getClassLoader())) {
            Class<?> clazz;
            try {
                String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
                clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
            } catch (ClassNotFoundException | LinkageError ex) {
                throw new BeanInitializationException("Failed to resolve default validator class", ex);
            }
            validator = (Validator) BeanUtils.instantiateClass(clazz);
        } else {
            validator = new NoOpValidator();
        }
    }
    return validator;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Validator(org.springframework.validation.Validator) Bean(org.springframework.context.annotation.Bean)

Example 38 with BeanInitializationException

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

the class ResourceHandlerRegistry method getRequestHandler.

@SuppressWarnings("deprecation")
private ResourceHttpRequestHandler getRequestHandler(ResourceHandlerRegistration registration) {
    ResourceHttpRequestHandler handler = registration.getRequestHandler();
    if (this.pathHelper != null) {
        handler.setUrlPathHelper(this.pathHelper);
    }
    if (this.contentNegotiationManager != null) {
        handler.setContentNegotiationManager(this.contentNegotiationManager);
    }
    handler.setServletContext(this.servletContext);
    handler.setApplicationContext(this.applicationContext);
    try {
        handler.afterPropertiesSet();
    } catch (Throwable ex) {
        throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
    }
    return handler;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler)

Example 39 with BeanInitializationException

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

the class DispatcherServlet method getDefaultStrategies.

/**
 * Create a List of default strategy objects for the given strategy interface.
 * <p>The default implementation uses the "DispatcherServlet.properties" file (in the same
 * package as the DispatcherServlet class) to determine the class names. It instantiates
 * the strategy objects through the context's BeanFactory.
 * @param context the current WebApplicationContext
 * @param strategyInterface the strategy interface
 * @return the List of corresponding strategy objects
 */
@SuppressWarnings("unchecked")
protected <T> List<T> getDefaultStrategies(ApplicationContext context, Class<T> strategyInterface) {
    if (defaultStrategies == null) {
        try {
            // Load default strategy implementations from properties file.
            // This is currently strictly internal and not meant to be customized
            // by application developers.
            ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
            defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException ex) {
            throw new IllegalStateException("Could not load '" + DEFAULT_STRATEGIES_PATH + "': " + ex.getMessage());
        }
    }
    String key = strategyInterface.getName();
    String value = defaultStrategies.getProperty(key);
    if (value != null) {
        String[] classNames = StringUtils.commaDelimitedListToStringArray(value);
        List<T> strategies = new ArrayList<>(classNames.length);
        for (String className : classNames) {
            try {
                Class<?> clazz = ClassUtils.forName(className, DispatcherServlet.class.getClassLoader());
                Object strategy = createDefaultStrategy(context, clazz);
                strategies.add((T) strategy);
            } catch (ClassNotFoundException ex) {
                throw new BeanInitializationException("Could not find DispatcherServlet's default strategy class [" + className + "] for interface [" + key + "]", ex);
            } catch (LinkageError err) {
                throw new BeanInitializationException("Unresolvable class definition for DispatcherServlet's default strategy class [" + className + "] for interface [" + key + "]", err);
            }
        }
        return strategies;
    } else {
        return Collections.emptyList();
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 40 with BeanInitializationException

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

the class WebFluxConfigurationSupport method webFluxValidator.

/**
 * Return a global {@link Validator} instance for example for validating
 * {@code @RequestBody} method arguments.
 * <p>Delegates to {@link #getValidator()} first. If that returns {@code null}
 * checks the classpath for the presence of a JSR-303 implementations
 * before creating a {@code OptionalValidatorFactoryBean}. If a JSR-303
 * implementation is not available, a "no-op" {@link Validator} is returned.
 */
@Bean
public Validator webFluxValidator() {
    Validator validator = getValidator();
    if (validator == null) {
        if (ClassUtils.isPresent("jakarta.validation.Validator", getClass().getClassLoader())) {
            Class<?> clazz;
            try {
                String name = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
                clazz = ClassUtils.forName(name, getClass().getClassLoader());
            } catch (ClassNotFoundException | LinkageError ex) {
                throw new BeanInitializationException("Failed to resolve default validator class", ex);
            }
            validator = (Validator) BeanUtils.instantiateClass(clazz);
        } else {
            validator = new NoOpValidator();
        }
    }
    return validator;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Validator(org.springframework.validation.Validator) Bean(org.springframework.context.annotation.Bean)

Aggregations

BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)41 IOException (java.io.IOException)9 BeansException (org.springframework.beans.BeansException)7 HashMap (java.util.HashMap)4 Bean (org.springframework.context.annotation.Bean)4 File (java.io.File)3 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 Field (java.lang.reflect.Field)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Matcher (java.util.regex.Matcher)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValue (org.springframework.beans.PropertyValue)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2