Polishing
This commit is contained in:
parent
f1fbe85ff1
commit
189cd663c5
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -125,11 +125,8 @@ public class ScopedProxyFactoryBean extends ProxyConfig implements FactoryBean<O
|
||||||
if (this.proxy != null) {
|
if (this.proxy != null) {
|
||||||
return this.proxy.getClass();
|
return this.proxy.getClass();
|
||||||
}
|
}
|
||||||
if (this.scopedTargetSource != null) {
|
|
||||||
return this.scopedTargetSource.getTargetClass();
|
return this.scopedTargetSource.getTargetClass();
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSingleton() {
|
public boolean isSingleton() {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -78,7 +78,6 @@ import org.springframework.messaging.Message;
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*
|
|
||||||
* @see org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler
|
* @see org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler
|
||||||
*/
|
*/
|
||||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -19,7 +19,6 @@ package org.springframework.web.servlet.handler;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
|
@ -204,19 +203,18 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||||
* <p>Will be invoked before {@link #initInterceptors()} adapts the specified
|
* <p>Will be invoked before {@link #initInterceptors()} adapts the specified
|
||||||
* interceptors into {@link HandlerInterceptor} instances.
|
* interceptors into {@link HandlerInterceptor} instances.
|
||||||
* <p>The default implementation is empty.
|
* <p>The default implementation is empty.
|
||||||
* @param interceptors the configured interceptor List (never {@code null}),
|
* @param interceptors the configured interceptor List (never {@code null}), allowing
|
||||||
* allowing to add further interceptors before as well as after the existing
|
* to add further interceptors before as well as after the existing interceptors
|
||||||
* interceptors
|
|
||||||
*/
|
*/
|
||||||
protected void extendInterceptors(List<Object> interceptors) {
|
protected void extendInterceptors(List<Object> interceptors) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detects beans of type {@link MappedInterceptor} and adds them to the list of mapped interceptors.
|
* Detect beans of type {@link MappedInterceptor} and add them to the list of mapped interceptors.
|
||||||
* This is done in addition to any {@link MappedInterceptor}s that may have been provided via
|
* <p>This is called in addition to any {@link MappedInterceptor}s that may have been provided
|
||||||
* {@link #setInterceptors(Object[])}. Subclasses can override this method to change that.
|
* via {@link #setInterceptors}, by default adding all beans of type {@link MappedInterceptor}
|
||||||
*
|
* from the current context and its ancestors. Subclasses can override and refine this policy.
|
||||||
* @param mappedInterceptors an empty list to add MappedInterceptor types to
|
* @param mappedInterceptors an empty list to add {@link MappedInterceptor} instances to
|
||||||
*/
|
*/
|
||||||
protected void detectMappedInterceptors(List<MappedInterceptor> mappedInterceptors) {
|
protected void detectMappedInterceptors(List<MappedInterceptor> mappedInterceptors) {
|
||||||
mappedInterceptors.addAll(
|
mappedInterceptors.addAll(
|
||||||
|
@ -225,8 +223,8 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the specified interceptors, checking for {@link MappedInterceptor}s and adapting
|
* Initialize the specified interceptors, checking for {@link MappedInterceptor}s and
|
||||||
* HandlerInterceptors where necessary.
|
* adapting {@link HandlerInterceptor}s and {@link WebRequestInterceptor}s if necessary.
|
||||||
* @see #setInterceptors
|
* @see #setInterceptors
|
||||||
* @see #adaptInterceptor
|
* @see #adaptInterceptor
|
||||||
*/
|
*/
|
||||||
|
@ -238,19 +236,20 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||||
throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null");
|
throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null");
|
||||||
}
|
}
|
||||||
if (interceptor instanceof MappedInterceptor) {
|
if (interceptor instanceof MappedInterceptor) {
|
||||||
mappedInterceptors.add((MappedInterceptor) interceptor);
|
this.mappedInterceptors.add((MappedInterceptor) interceptor);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
adaptedInterceptors.add(adaptInterceptor(interceptor));
|
this.adaptedInterceptors.add(adaptInterceptor(interceptor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapt the given interceptor object to the HandlerInterceptor interface.
|
* Adapt the given interceptor object to the {@link HandlerInterceptor} interface.
|
||||||
* <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
|
* <p>By default, the supported interceptor types are {@link HandlerInterceptor}
|
||||||
* Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter.
|
* and {@link WebRequestInterceptor}. Each given {@link WebRequestInterceptor}
|
||||||
|
* will be wrapped in a {@link WebRequestHandlerInterceptorAdapter}.
|
||||||
* Can be overridden in subclasses.
|
* Can be overridden in subclasses.
|
||||||
* @param interceptor the specified interceptor object
|
* @param interceptor the specified interceptor object
|
||||||
* @return the interceptor wrapped as HandlerInterceptor
|
* @return the interceptor wrapped as HandlerInterceptor
|
||||||
|
@ -271,12 +270,12 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the adapted interceptors as HandlerInterceptor array.
|
* Return the adapted interceptors as {@link HandlerInterceptor} array.
|
||||||
* @return the array of HandlerInterceptors, or {@code null} if none
|
* @return the array of {@link HandlerInterceptor}s, or {@code null} if none
|
||||||
*/
|
*/
|
||||||
protected final HandlerInterceptor[] getAdaptedInterceptors() {
|
protected final HandlerInterceptor[] getAdaptedInterceptors() {
|
||||||
int count = adaptedInterceptors.size();
|
int count = this.adaptedInterceptors.size();
|
||||||
return (count > 0) ? adaptedInterceptors.toArray(new HandlerInterceptor[count]) : null;
|
return (count > 0 ? this.adaptedInterceptors.toArray(new HandlerInterceptor[count]) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -284,8 +283,8 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||||
* @return the array of {@link MappedInterceptor}s, or {@code null} if none
|
* @return the array of {@link MappedInterceptor}s, or {@code null} if none
|
||||||
*/
|
*/
|
||||||
protected final MappedInterceptor[] getMappedInterceptors() {
|
protected final MappedInterceptor[] getMappedInterceptors() {
|
||||||
int count = mappedInterceptors.size();
|
int count = this.mappedInterceptors.size();
|
||||||
return (count > 0) ? mappedInterceptors.toArray(new MappedInterceptor[count]) : null;
|
return (count > 0 ? this.mappedInterceptors.toArray(new MappedInterceptor[count]) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -326,31 +325,32 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||||
protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception;
|
protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a HandlerExecutionChain for the given handler, including applicable interceptors.
|
* Build a {@link HandlerExecutionChain} for the given handler, including
|
||||||
* <p>The default implementation simply builds a standard HandlerExecutionChain with
|
* applicable interceptors.
|
||||||
* the given handler, the handler mapping's common interceptors, and any {@link MappedInterceptor}s
|
* <p>The default implementation builds a standard {@link HandlerExecutionChain}
|
||||||
* matching to the current request URL. Subclasses may
|
* with the given handler, the handler mapping's common interceptors, and any
|
||||||
* override this in order to extend/rearrange the list of interceptors.
|
* {@link MappedInterceptor}s matching to the current request URL. Subclasses
|
||||||
* <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a pre-built
|
* may override this in order to extend/rearrange the list of interceptors.
|
||||||
* HandlerExecutionChain. This method should handle those two cases explicitly,
|
* <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a
|
||||||
* either building a new HandlerExecutionChain or extending the existing chain.
|
* pre-built {@link HandlerExecutionChain}. This method should handle those
|
||||||
* <p>For simply adding an interceptor, consider calling {@code super.getHandlerExecutionChain}
|
* two cases explicitly, either building a new {@link HandlerExecutionChain}
|
||||||
* and invoking {@link HandlerExecutionChain#addInterceptor} on the returned chain object.
|
* or extending the existing chain.
|
||||||
|
* <p>For simply adding an interceptor in a custom subclass, consider calling
|
||||||
|
* {@code super.getHandlerExecutionChain(handler, request)} and invoking
|
||||||
|
* {@link HandlerExecutionChain#addInterceptor} on the returned chain object.
|
||||||
* @param handler the resolved handler instance (never {@code null})
|
* @param handler the resolved handler instance (never {@code null})
|
||||||
* @param request current HTTP request
|
* @param request current HTTP request
|
||||||
* @return the HandlerExecutionChain (never {@code null})
|
* @return the HandlerExecutionChain (never {@code null})
|
||||||
* @see #getAdaptedInterceptors()
|
* @see #getAdaptedInterceptors()
|
||||||
*/
|
*/
|
||||||
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
|
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
|
||||||
HandlerExecutionChain chain =
|
HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
|
||||||
(handler instanceof HandlerExecutionChain) ?
|
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
|
||||||
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler);
|
|
||||||
|
|
||||||
chain.addInterceptors(getAdaptedInterceptors());
|
chain.addInterceptors(getAdaptedInterceptors());
|
||||||
|
|
||||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
|
||||||
for (MappedInterceptor mappedInterceptor : mappedInterceptors) {
|
for (MappedInterceptor mappedInterceptor : this.mappedInterceptors) {
|
||||||
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
|
if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
|
||||||
chain.addInterceptor(mappedInterceptor.getInterceptor());
|
chain.addInterceptor(mappedInterceptor.getInterceptor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
|
|
||||||
private StaticWebApplicationContext webAppContext;
|
private StaticWebApplicationContext webAppContext;
|
||||||
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setupOnce() {
|
public static void setupOnce() {
|
||||||
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
|
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
|
||||||
|
@ -86,23 +87,24 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
this.response = new MockHttpServletResponse();
|
this.response = new MockHttpServletResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cacheControlWithoutSessionAttributes() throws Exception {
|
public void cacheControlWithoutSessionAttributes() throws Exception {
|
||||||
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
|
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
|
||||||
this.handlerAdapter.afterPropertiesSet();
|
|
||||||
this.handlerAdapter.setCacheSeconds(100);
|
this.handlerAdapter.setCacheSeconds(100);
|
||||||
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
|
|
||||||
assertTrue(response.getHeader("Cache-Control").toString().contains("max-age"));
|
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
||||||
|
assertTrue(response.getHeader("Cache-Control").contains("max-age"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cacheControlWithSessionAttributes() throws Exception {
|
public void cacheControlWithSessionAttributes() throws Exception {
|
||||||
SessionAttributeController handler = new SessionAttributeController();
|
SessionAttributeController handler = new SessionAttributeController();
|
||||||
this.handlerAdapter.afterPropertiesSet();
|
|
||||||
this.handlerAdapter.setCacheSeconds(100);
|
this.handlerAdapter.setCacheSeconds(100);
|
||||||
this.handlerAdapter.handle(this.request, this.response, handlerMethod(handler, "handle"));
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
|
|
||||||
|
this.handlerAdapter.handle(this.request, this.response, handlerMethod(handler, "handle"));
|
||||||
assertEquals("no-cache", this.response.getHeader("Cache-Control"));
|
assertEquals("no-cache", this.response.getHeader("Cache-Control"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,6 +228,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SessionAttributes("attr1")
|
@SessionAttributes("attr1")
|
||||||
private static class SessionAttributeController {
|
private static class SessionAttributeController {
|
||||||
|
|
||||||
|
@ -234,6 +237,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static class RedirectAttributeController {
|
private static class RedirectAttributeController {
|
||||||
|
|
||||||
|
@ -243,6 +247,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
private static class ModelAttributeAdvice {
|
private static class ModelAttributeAdvice {
|
||||||
|
|
||||||
|
@ -253,6 +258,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice({"org.springframework.web.servlet.mvc.method.annotation","java.lang"})
|
@ControllerAdvice({"org.springframework.web.servlet.mvc.method.annotation","java.lang"})
|
||||||
private static class ModelAttributePackageAdvice {
|
private static class ModelAttributePackageAdvice {
|
||||||
|
|
||||||
|
@ -262,6 +268,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice("java.lang")
|
@ControllerAdvice("java.lang")
|
||||||
private static class ModelAttributeNotUsedPackageAdvice {
|
private static class ModelAttributeNotUsedPackageAdvice {
|
||||||
|
|
||||||
|
@ -270,4 +277,5 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
model.addAttribute("attr3", "gAttr3");
|
model.addAttribute("attr3", "gAttr3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue