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

@@ -567,7 +567,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
catch (LinkageError ex) {
throw new BeanInitializationException("Could not load default validator class", ex);
}
validator = (Validator) BeanUtils.instantiate(clazz);
validator = (Validator) BeanUtils.instantiateClass(clazz);
}
else {
validator = new NoOpValidator();

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.
@@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.ModelAndView;
/**
@@ -78,13 +79,11 @@ import org.springframework.web.servlet.ModelAndView;
* @author Juergen Hoeller
* @since 1.1.1
* @see ServletForwardingController
* @see org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor
* @see org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
*/
public class ServletWrappingController extends AbstractController
implements BeanNameAware, InitializingBean, DisposableBean {
private Class<?> servletClass;
private Class<? extends Servlet> servletClass;
private String servletName;
@@ -105,7 +104,7 @@ public class ServletWrappingController extends AbstractController
* Needs to implement {@code javax.servlet.Servlet}.
* @see javax.servlet.Servlet
*/
public void setServletClass(Class<?> servletClass) {
public void setServletClass(Class<? extends Servlet> servletClass) {
this.servletClass = servletClass;
}
@@ -142,12 +141,12 @@ public class ServletWrappingController extends AbstractController
}
if (!Servlet.class.isAssignableFrom(this.servletClass)) {
throw new IllegalArgumentException("servletClass [" + this.servletClass.getName() +
"] needs to implement interface [javax.servlet.Servlet]");
"] needs to implement interface [javax.servlet.Servlet]");
}
if (this.servletName == null) {
this.servletName = this.beanName;
}
this.servletInstance = (Servlet) this.servletClass.newInstance();
this.servletInstance = ReflectionUtils.accessibleConstructor(this.servletClass).newInstance();
this.servletInstance.init(new DelegatingServletConfig());
}
@@ -158,7 +157,7 @@ public class ServletWrappingController extends AbstractController
*/
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {
throws Exception {
this.servletInstance.service(request, response);
return null;

View File

@@ -612,9 +612,9 @@ public class MvcUriComponentsBuilder {
if (proxy == null) {
try {
proxy = proxyClass.newInstance();
proxy = ReflectionUtils.accessibleConstructor(proxyClass).newInstance();
}
catch (Exception ex) {
catch (Throwable ex) {
throw new IllegalStateException("Unable to instantiate controller proxy using Objenesis, " +
"and regular controller instantiation via default constructor fails as well", ex);
}

View File

@@ -336,7 +336,7 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
LocaleResolver resolver) {
if (definitionsFactoryClass != null) {
DefinitionsFactory factory = BeanUtils.instantiate(definitionsFactoryClass);
DefinitionsFactory factory = BeanUtils.instantiateClass(definitionsFactoryClass);
if (factory instanceof ApplicationContextAware) {
((ApplicationContextAware) factory).setApplicationContext(applicationContext);
}
@@ -357,7 +357,7 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
@Override
protected PreparerFactory createPreparerFactory(ApplicationContext context) {
if (preparerFactoryClass != null) {
return BeanUtils.instantiate(preparerFactoryClass);
return BeanUtils.instantiateClass(preparerFactoryClass);
}
else {
return super.createPreparerFactory(context);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@@ -46,6 +46,7 @@ import org.springframework.context.ApplicationContextException;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.SimpleTransformErrorListener;
import org.springframework.util.xml.TransformerUtils;
@@ -73,7 +74,7 @@ import org.springframework.web.util.WebUtils;
*/
public class XsltView extends AbstractUrlBasedView {
private Class<?> transformerFactoryClass;
private Class<? extends TransformerFactory> transformerFactoryClass;
private String sourceKey;
@@ -97,7 +98,7 @@ public class XsltView extends AbstractUrlBasedView {
* <p>The default constructor of the specified class will be called
* to build the TransformerFactory for this view.
*/
public void setTransformerFactoryClass(Class<?> transformerFactoryClass) {
public void setTransformerFactoryClass(Class<? extends TransformerFactory> transformerFactoryClass) {
Assert.isAssignable(TransformerFactory.class, transformerFactoryClass);
this.transformerFactoryClass = transformerFactoryClass;
}
@@ -195,10 +196,10 @@ public class XsltView extends AbstractUrlBasedView {
* @see #setTransformerFactoryClass
* @see #getTransformerFactory()
*/
protected TransformerFactory newTransformerFactory(Class<?> transformerFactoryClass) {
protected TransformerFactory newTransformerFactory(Class<? extends TransformerFactory> transformerFactoryClass) {
if (transformerFactoryClass != null) {
try {
return (TransformerFactory) transformerFactoryClass.newInstance();
return ReflectionUtils.accessibleConstructor(transformerFactoryClass).newInstance();
}
catch (Exception ex) {
throw new TransformerFactoryConfigurationError(ex, "Could not instantiate TransformerFactory");