Consistently use constructor-based instantiation instead of Class.newInstance / BeanUtils.instantiate

Issue: SPR-14486
This commit is contained in:
Juergen Hoeller
2016-07-19 19:21:06 +02:00
parent b3253ca5e2
commit aaac199e8b
54 changed files with 285 additions and 242 deletions

View File

@@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Simple implementation of a JNDI naming context builder.
@@ -210,10 +211,10 @@ public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder
"Specified class does not implement [" + InitialContextFactory.class.getName() + "]: " + icf);
}
try {
return (InitialContextFactory) icfClass.newInstance();
return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
}
catch (Throwable ex) {
throw new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf, ex);
throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf, ex);
}
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
@@ -87,7 +88,7 @@ public abstract class ProfileValueUtils {
}
else {
try {
profileValueSource = profileValueSourceType.newInstance();
profileValueSource = ReflectionUtils.accessibleConstructor(profileValueSourceType).newInstance();
}
catch (Exception ex) {
if (logger.isWarnEnabled()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -366,17 +366,17 @@ class StubWebApplicationContext implements WebApplicationContext {
@Override
public <T> T createBean(Class<T> beanClass) {
return BeanUtils.instantiate(beanClass);
return BeanUtils.instantiateClass(beanClass);
}
@Override
public Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) {
return BeanUtils.instantiate(beanClass);
return BeanUtils.instantiateClass(beanClass);
}
@Override
public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) {
return BeanUtils.instantiate(beanClass);
return BeanUtils.instantiateClass(beanClass);
}
@Override