Polishing

This commit is contained in:
Juergen Hoeller 2014-03-15 00:46:06 +01:00
parent f1fbe85ff1
commit 189cd663c5
4 changed files with 86 additions and 82 deletions

View File

@ -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,10 +125,7 @@ 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

View File

@ -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.
@ -36,34 +36,34 @@ import org.springframework.messaging.Message;
* flexible signatures. They may have arguments of the following types, in arbitrary * flexible signatures. They may have arguments of the following types, in arbitrary
* order: * order:
* <ul> * <ul>
* <li>{@link Message} to get access to the complete message being processed.</li> * <li>{@link Message} to get access to the complete message being processed.</li>
* <li>{@link Payload}-annotated method arguments to extract the payload of * <li>{@link Payload}-annotated method arguments to extract the payload of
* a message and optionally convert it using a * a message and optionally convert it using a
* {@link org.springframework.messaging.converter.MessageConverter}. * {@link org.springframework.messaging.converter.MessageConverter}.
* The presence of the annotation is not required since it is assumed by default * The presence of the annotation is not required since it is assumed by default
* for method arguments that are not annotated. Payload method arguments annotated * for method arguments that are not annotated. Payload method arguments annotated
* with Validation annotations (like * with Validation annotations (like
* {@link org.springframework.validation.annotation.Validated}) will be subject to * {@link org.springframework.validation.annotation.Validated}) will be subject to
* JSR-303 validation.</li> * JSR-303 validation.</li>
* <li>{@link Header}-annotated method arguments to extract a specific * <li>{@link Header}-annotated method arguments to extract a specific
* header value along with type conversion with a * header value along with type conversion with a
* {@link org.springframework.core.convert.converter.Converter} if necessary.</li> * {@link org.springframework.core.convert.converter.Converter} if necessary.</li>
* <li>{@link Headers}-annotated argument that must also be assignable to * <li>{@link Headers}-annotated argument that must also be assignable to
* {@link java.util.Map} for getting access to all headers.</li> * {@link java.util.Map} for getting access to all headers.</li>
* <li>{@link org.springframework.messaging.MessageHeaders} arguments for * <li>{@link org.springframework.messaging.MessageHeaders} arguments for
* getting access to all headers.</li> * getting access to all headers.</li>
* <li>{@link org.springframework.messaging.support.MessageHeaderAccessor} or * <li>{@link org.springframework.messaging.support.MessageHeaderAccessor} or
* with STOMP over WebSocket support also sub-classes such as * with STOMP over WebSocket support also sub-classes such as
* {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor} * {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor}
* for convenient access to all method arguments.</li> * for convenient access to all method arguments.</li>
* <li>{@link DestinationVariable}-annotated arguments for access to template * <li>{@link DestinationVariable}-annotated arguments for access to template
* variable values extracted from the message destination (e.g. /hotels/{hotel}). * variable values extracted from the message destination (e.g. /hotels/{hotel}).
* Variable values will be converted to the declared method argument type.</li> * Variable values will be converted to the declared method argument type.</li>
* <li>{@link java.security.Principal} method arguments are supported with * <li>{@link java.security.Principal} method arguments are supported with
* STOMP over WebSocket messages. It reflects the user logged in to the * STOMP over WebSocket messages. It reflects the user logged in to the
* WebSocket session on which the message was received. Regular HTTP-based * WebSocket session on which the message was received. Regular HTTP-based
* authentication (e.g. Spring Security based) can be used to secure the * authentication (e.g. Spring Security based) can be used to secure the
* HTTP handshake that initiates WebSocket sessions.</li> * HTTP handshake that initiates WebSocket sessions.</li>
* </ul> * </ul>
* <p>By default the return value is wrapped as a message and sent to the destination * <p>By default the return value is wrapped as a message and sent to the destination
* specified with an {@link SendTo} method-level annotation. * specified with an {@link SendTo} method-level annotation.
@ -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})

View File

@ -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,29 +203,28 @@ 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(
BeanFactoryUtils.beansOfTypeIncludingAncestors( BeanFactoryUtils.beansOfTypeIncludingAncestors(
getApplicationContext(),MappedInterceptor.class, true, false).values()); getApplicationContext(), MappedInterceptor.class, true, false).values());
} }
/** /**
* 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());
} }
} }

View File

@ -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");
} }
} }
}
}