Avoid reflection when creating instances of well-known View classes
Closes gh-25847
This commit is contained in:
parent
a0af552d0f
commit
f5d9babfd2
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -94,9 +94,12 @@ public class UrlBasedViewResolver extends ViewResolverSupport
|
|||
|
||||
|
||||
/**
|
||||
* Set the view class to instantiate through {@link #createView(String)}.
|
||||
* Set the view class that should be used to create views.
|
||||
* @param viewClass a class that is assignable to the required view class
|
||||
* which by default is AbstractUrlBasedView
|
||||
* (by default: AbstractUrlBasedView)
|
||||
* @see #requiredViewClass()
|
||||
* @see #instantiateView()
|
||||
* @see AbstractUrlBasedView
|
||||
*/
|
||||
public void setViewClass(@Nullable Class<?> viewClass) {
|
||||
if (viewClass != null && !requiredViewClass().isAssignableFrom(viewClass)) {
|
||||
|
@ -109,21 +112,13 @@ public class UrlBasedViewResolver extends ViewResolverSupport
|
|||
|
||||
/**
|
||||
* Return the view class to be used to create views.
|
||||
* @see #setViewClass
|
||||
*/
|
||||
@Nullable
|
||||
protected Class<?> getViewClass() {
|
||||
return this.viewClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the required type of view for this resolver.
|
||||
* This implementation returns {@link AbstractUrlBasedView}.
|
||||
* @see AbstractUrlBasedView
|
||||
*/
|
||||
protected Class<?> requiredViewClass() {
|
||||
return AbstractUrlBasedView.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix that gets prepended to view names when building a URL.
|
||||
*/
|
||||
|
@ -265,6 +260,29 @@ public class UrlBasedViewResolver extends ViewResolverSupport
|
|||
return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the required type of view for this resolver.
|
||||
* This implementation returns {@link AbstractUrlBasedView}.
|
||||
* @see #instantiateView()
|
||||
* @see AbstractUrlBasedView
|
||||
*/
|
||||
protected Class<?> requiredViewClass() {
|
||||
return AbstractUrlBasedView.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate the specified view class.
|
||||
* <p>The default implementation uses reflection to instantiate the class.
|
||||
* @return a new instance of the view class
|
||||
* @since 5.3
|
||||
* @see #setViewClass
|
||||
*/
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
Class<?> viewClass = getViewClass();
|
||||
Assert.state(viewClass != null, "No view class");
|
||||
return (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new View instance of the specified view class and configures it.
|
||||
* <p>Does <i>not</i> perform any lookup for pre-defined View instances.
|
||||
|
@ -277,10 +295,7 @@ public class UrlBasedViewResolver extends ViewResolverSupport
|
|||
* @see #applyLifecycleMethods
|
||||
*/
|
||||
protected AbstractUrlBasedView createView(String viewName) {
|
||||
Class<?> viewClass = getViewClass();
|
||||
Assert.state(viewClass != null, "No view class");
|
||||
|
||||
AbstractUrlBasedView view = (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass);
|
||||
AbstractUrlBasedView view = instantiateView();
|
||||
view.setSupportedMediaTypes(getSupportedMediaTypes());
|
||||
view.setDefaultCharset(getDefaultCharset());
|
||||
view.setUrl(getPrefix() + viewName + getSuffix());
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.web.reactive.result.view.freemarker;
|
||||
|
||||
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
|
||||
import org.springframework.web.reactive.result.view.UrlBasedViewResolver;
|
||||
|
||||
/**
|
||||
|
@ -57,4 +58,9 @@ public class FreeMarkerViewResolver extends UrlBasedViewResolver {
|
|||
return FreeMarkerView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == FreeMarkerView.class ? new FreeMarkerView() : super.instantiateView());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.web.reactive.result.view.script;
|
||||
|
||||
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
|
||||
import org.springframework.web.reactive.result.view.UrlBasedViewResolver;
|
||||
|
||||
/**
|
||||
|
@ -61,4 +62,9 @@ public class ScriptTemplateViewResolver extends UrlBasedViewResolver {
|
|||
return ScriptTemplateView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == ScriptTemplateView.class ? new ScriptTemplateView() : super.instantiateView());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -82,14 +82,6 @@ public class InternalResourceViewResolver extends UrlBasedViewResolver {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* This resolver requires {@link InternalResourceView}.
|
||||
*/
|
||||
@Override
|
||||
protected Class<?> requiredViewClass() {
|
||||
return InternalResourceView.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify whether to always include the view rather than forward to it.
|
||||
* <p>Default is "false". Switch this flag on to enforce the use of a
|
||||
|
@ -101,6 +93,17 @@ public class InternalResourceViewResolver extends UrlBasedViewResolver {
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Class<?> requiredViewClass() {
|
||||
return InternalResourceView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == InternalResourceView.class ? new InternalResourceView() :
|
||||
(getViewClass() == JstlView.class ? new JstlView() : super.instantiateView()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
|
||||
InternalResourceView view = (InternalResourceView) super.buildView(viewName);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -144,8 +144,10 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements
|
|||
|
||||
/**
|
||||
* Set the view class that should be used to create views.
|
||||
* @param viewClass class that is assignable to the required view class
|
||||
* (by default, AbstractUrlBasedView)
|
||||
* @param viewClass a class that is assignable to the required view class
|
||||
* (by default: AbstractUrlBasedView)
|
||||
* @see #requiredViewClass()
|
||||
* @see #instantiateView()
|
||||
* @see AbstractUrlBasedView
|
||||
*/
|
||||
public void setViewClass(@Nullable Class<?> viewClass) {
|
||||
|
@ -158,21 +160,13 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements
|
|||
|
||||
/**
|
||||
* Return the view class to be used to create views.
|
||||
* @see #setViewClass
|
||||
*/
|
||||
@Nullable
|
||||
protected Class<?> getViewClass() {
|
||||
return this.viewClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the required type of view for this resolver.
|
||||
* This implementation returns AbstractUrlBasedView.
|
||||
* @see AbstractUrlBasedView
|
||||
*/
|
||||
protected Class<?> requiredViewClass() {
|
||||
return AbstractUrlBasedView.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix that gets prepended to view names when building a URL.
|
||||
*/
|
||||
|
@ -510,6 +504,29 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements
|
|||
return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the required type of view for this resolver.
|
||||
* This implementation returns {@link AbstractUrlBasedView}.
|
||||
* @see #instantiateView()
|
||||
* @see AbstractUrlBasedView
|
||||
*/
|
||||
protected Class<?> requiredViewClass() {
|
||||
return AbstractUrlBasedView.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate the specified view class.
|
||||
* <p>The default implementation uses reflection to instantiate the class.
|
||||
* @return a new instance of the view class
|
||||
* @since 5.3
|
||||
* @see #setViewClass
|
||||
*/
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
Class<?> viewClass = getViewClass();
|
||||
Assert.state(viewClass != null, "No view class");
|
||||
return (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to {@code buildView} for creating a new instance of the
|
||||
* specified view class. Applies the following Spring lifecycle methods
|
||||
|
@ -547,10 +564,7 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements
|
|||
* @see #loadView(String, java.util.Locale)
|
||||
*/
|
||||
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
|
||||
Class<?> viewClass = getViewClass();
|
||||
Assert.state(viewClass != null, "No view class");
|
||||
|
||||
AbstractUrlBasedView view = (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass);
|
||||
AbstractUrlBasedView view = instantiateView();
|
||||
view.setUrl(getPrefix() + viewName + getSuffix());
|
||||
view.setAttributesMap(getAttributesMap());
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.web.servlet.view.freemarker;
|
||||
|
||||
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
|
||||
import org.springframework.web.servlet.view.AbstractUrlBasedView;
|
||||
|
||||
/**
|
||||
* Convenience subclass of {@link org.springframework.web.servlet.view.UrlBasedViewResolver}
|
||||
|
@ -70,4 +71,9 @@ public class FreeMarkerViewResolver extends AbstractTemplateViewResolver {
|
|||
return FreeMarkerView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == FreeMarkerView.class ? new FreeMarkerView() : super.instantiateView());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.view.groovy;
|
|||
import java.util.Locale;
|
||||
|
||||
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
|
||||
import org.springframework.web.servlet.view.AbstractUrlBasedView;
|
||||
|
||||
/**
|
||||
* Convenience subclass of @link AbstractTemplateViewResolver} that supports
|
||||
|
@ -65,6 +66,11 @@ public class GroovyMarkupViewResolver extends AbstractTemplateViewResolver {
|
|||
return GroovyMarkupView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == GroovyMarkupView.class ? new GroovyMarkupView() : super.instantiateView());
|
||||
}
|
||||
|
||||
/**
|
||||
* This resolver supports i18n, so cache keys should contain the locale.
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.web.servlet.view.script;
|
||||
|
||||
import org.springframework.web.servlet.view.AbstractUrlBasedView;
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
||||
|
||||
/**
|
||||
|
@ -62,4 +63,9 @@ public class ScriptTemplateViewResolver extends UrlBasedViewResolver {
|
|||
return ScriptTemplateView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == ScriptTemplateView.class ? new ScriptTemplateView() : super.instantiateView());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.view.tiles3;
|
|||
import org.apache.tiles.request.render.Renderer;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.view.AbstractUrlBasedView;
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
||||
|
||||
/**
|
||||
|
@ -40,19 +41,14 @@ public class TilesViewResolver extends UrlBasedViewResolver {
|
|||
private Boolean alwaysInclude;
|
||||
|
||||
|
||||
/**
|
||||
* This resolver requires {@link TilesView}.
|
||||
*/
|
||||
public TilesViewResolver() {
|
||||
setViewClass(requiredViewClass());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This resolver requires {@link TilesView}.
|
||||
*/
|
||||
@Override
|
||||
protected Class<?> requiredViewClass() {
|
||||
return TilesView.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link Renderer} to use. If not specified, a default
|
||||
* {@link org.apache.tiles.renderer.DefinitionRenderer} will be used.
|
||||
|
@ -74,6 +70,16 @@ public class TilesViewResolver extends UrlBasedViewResolver {
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Class<?> requiredViewClass() {
|
||||
return TilesView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == TilesView.class ? new TilesView() : super.instantiateView());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TilesView buildView(String viewName) throws Exception {
|
||||
TilesView view = (TilesView) super.buildView(viewName);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -53,16 +53,14 @@ public class XsltViewResolver extends UrlBasedViewResolver {
|
|||
private boolean cacheTemplates = true;
|
||||
|
||||
|
||||
/**
|
||||
* This resolver requires {@link XsltView}.
|
||||
*/
|
||||
public XsltViewResolver() {
|
||||
setViewClass(requiredViewClass());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Class<?> requiredViewClass() {
|
||||
return XsltView.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the model attribute that represents the XSLT Source.
|
||||
* If not specified, the model map will be searched for a matching value type.
|
||||
|
@ -127,6 +125,16 @@ public class XsltViewResolver extends UrlBasedViewResolver {
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Class<?> requiredViewClass() {
|
||||
return XsltView.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView instantiateView() {
|
||||
return (getViewClass() == XsltView.class ? new XsltView() : super.instantiateView());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
|
||||
XsltView view = (XsltView) super.buildView(viewName);
|
||||
|
|
Loading…
Reference in New Issue