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();
}
}
}
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;
}
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;
}
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();
}
}
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;
}
Aggregations