optimized getMethodResolver implementation to actually benefit from ConcurrentHashMap

This commit is contained in:
Juergen Hoeller 2011-07-27 22:26:05 +00:00
parent e63e6cdbde
commit 4385367f2d
2 changed files with 38 additions and 33 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 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.
@ -30,7 +30,6 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.portlet.ActionRequest; import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse; import javax.portlet.ActionResponse;
import javax.portlet.ClientDataRequest; import javax.portlet.ClientDataRequest;
@ -152,7 +151,8 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator
private BeanExpressionContext expressionContext; private BeanExpressionContext expressionContext;
private final Map<Class<?>, PortletHandlerMethodResolver> methodResolverCache = new ConcurrentHashMap<Class<?>, PortletHandlerMethodResolver>(); private final Map<Class<?>, PortletHandlerMethodResolver> methodResolverCache =
new ConcurrentHashMap<Class<?>, PortletHandlerMethodResolver>();
/** /**
@ -302,6 +302,7 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator
} }
} }
protected ModelAndView doHandle(PortletRequest request, PortletResponse response, Object handler) throws Exception { protected ModelAndView doHandle(PortletRequest request, PortletResponse response, Object handler) throws Exception {
ExtendedModelMap implicitModel = null; ExtendedModelMap implicitModel = null;
@ -393,16 +394,18 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator
*/ */
private PortletHandlerMethodResolver getMethodResolver(Object handler) { private PortletHandlerMethodResolver getMethodResolver(Object handler) {
Class handlerClass = ClassUtils.getUserClass(handler); Class handlerClass = ClassUtils.getUserClass(handler);
synchronized (this.methodResolverCache) {
PortletHandlerMethodResolver resolver = this.methodResolverCache.get(handlerClass); PortletHandlerMethodResolver resolver = this.methodResolverCache.get(handlerClass);
if (resolver == null) {
synchronized (this.methodResolverCache) {
resolver = this.methodResolverCache.get(handlerClass);
if (resolver == null) { if (resolver == null) {
resolver = new PortletHandlerMethodResolver(handlerClass); resolver = new PortletHandlerMethodResolver(handlerClass);
this.methodResolverCache.put(handlerClass, resolver); this.methodResolverCache.put(handlerClass, resolver);
} }
}
}
return resolver; return resolver;
} }
}
/** /**
* Template method for creating a new PortletRequestDataBinder instance. * Template method for creating a new PortletRequestDataBinder instance.
@ -417,9 +420,7 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator
* @see PortletRequestDataBinder#bind(javax.portlet.PortletRequest) * @see PortletRequestDataBinder#bind(javax.portlet.PortletRequest)
* @see PortletRequestDataBinder#convertIfNecessary(Object, Class, MethodParameter) * @see PortletRequestDataBinder#convertIfNecessary(Object, Class, MethodParameter)
*/ */
protected PortletRequestDataBinder createBinder( protected PortletRequestDataBinder createBinder(PortletRequest request, Object target, String objectName) throws Exception {
PortletRequest request, Object target, String objectName) throws Exception {
return new PortletRequestDataBinder(target, objectName); return new PortletRequestDataBinder(target, objectName);
} }

View File

@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -103,7 +104,6 @@ import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestScope; import org.springframework.web.context.request.RequestScope;
import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
@ -183,7 +183,8 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
private BeanExpressionContext expressionContext; private BeanExpressionContext expressionContext;
private final Map<Class<?>, ServletHandlerMethodResolver> methodResolverCache = new ConcurrentHashMap<Class<?>, ServletHandlerMethodResolver>(); private final Map<Class<?>, ServletHandlerMethodResolver> methodResolverCache =
new ConcurrentHashMap<Class<?>, ServletHandlerMethodResolver>();
private final Map<Class<?>, Boolean> sessionAnnotatedClassesCache = new ConcurrentHashMap<Class<?>, Boolean>(); private final Map<Class<?>, Boolean> sessionAnnotatedClassesCache = new ConcurrentHashMap<Class<?>, Boolean>();
@ -393,12 +394,11 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
throws Exception { throws Exception {
Class<?> clazz = ClassUtils.getUserClass(handler); Class<?> clazz = ClassUtils.getUserClass(handler);
Boolean annotated = sessionAnnotatedClassesCache.get(clazz); Boolean annotated = this.sessionAnnotatedClassesCache.get(clazz);
if (annotated == null) { if (annotated == null) {
annotated = Boolean.valueOf(AnnotationUtils.findAnnotation(handler annotated = (AnnotationUtils.findAnnotation(handler.getClass(), SessionAttributes.class) != null);
.getClass(), SessionAttributes.class) != null); this.sessionAnnotatedClassesCache.put(clazz, annotated);
sessionAnnotatedClassesCache.put(clazz, annotated);
} }
if (annotated) { if (annotated) {
@ -442,30 +442,35 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
} }
/** /**
* This method always returns -1 since an annotated controller can have many methods, each * This method always returns -1 since an annotated controller can have many methods,
* requiring separate lastModified calculations. Instead an @{@link RequestMapping} method * each requiring separate lastModified calculations. Instead, an
* can calculate the lastModified value, call {@link WebRequest#checkNotModified(long)} to * @{@link RequestMapping}-annotated method can calculate the lastModified value, call
* check it, and return {@code null} if that returns {@code true}. * {@link org.springframework.web.context.request.WebRequest#checkNotModified(long)}
* @see WebRequest#checkNotModified(long) * to check it, and return {@code null} if that returns {@code true}.
* @see org.springframework.web.context.request.WebRequest#checkNotModified(long)
*/ */
public long getLastModified(HttpServletRequest request, Object handler) { public long getLastModified(HttpServletRequest request, Object handler) {
return -1; return -1;
} }
/** /**
* Build a HandlerMethodResolver for the given handler type. * Build a HandlerMethodResolver for the given handler type.
*/ */
private ServletHandlerMethodResolver getMethodResolver(Object handler) { private ServletHandlerMethodResolver getMethodResolver(Object handler) {
Class handlerClass = ClassUtils.getUserClass(handler); Class handlerClass = ClassUtils.getUserClass(handler);
synchronized (this.methodResolverCache) {
ServletHandlerMethodResolver resolver = this.methodResolverCache.get(handlerClass); ServletHandlerMethodResolver resolver = this.methodResolverCache.get(handlerClass);
if (resolver == null) {
synchronized (this.methodResolverCache) {
resolver = this.methodResolverCache.get(handlerClass);
if (resolver == null) { if (resolver == null) {
resolver = new ServletHandlerMethodResolver(handlerClass); resolver = new ServletHandlerMethodResolver(handlerClass);
this.methodResolverCache.put(handlerClass, resolver); this.methodResolverCache.put(handlerClass, resolver);
} }
return resolver;
} }
} }
return resolver;
}
/** /**
@ -481,8 +486,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
* @see ServletRequestDataBinder#bind(javax.servlet.ServletRequest) * @see ServletRequestDataBinder#bind(javax.servlet.ServletRequest)
* @see ServletRequestDataBinder#convertIfNecessary(Object, Class, org.springframework.core.MethodParameter) * @see ServletRequestDataBinder#convertIfNecessary(Object, Class, org.springframework.core.MethodParameter)
*/ */
protected ServletRequestDataBinder createBinder(HttpServletRequest request, Object target, String objectName) protected ServletRequestDataBinder createBinder(HttpServletRequest request, Object target, String objectName) throws Exception {
throws Exception {
return new ServletRequestDataBinder(target, objectName); return new ServletRequestDataBinder(target, objectName);
} }
@ -516,7 +520,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
*/ */
private class ServletHandlerMethodResolver extends HandlerMethodResolver { private class ServletHandlerMethodResolver extends HandlerMethodResolver {
private final Map<Method, RequestMappingInfo> mappings = new ConcurrentHashMap<Method, RequestMappingInfo>(); private final Map<Method, RequestMappingInfo> mappings = new HashMap<Method, RequestMappingInfo>();
private ServletHandlerMethodResolver(Class<?> handlerType) { private ServletHandlerMethodResolver(Class<?> handlerType) {
init(handlerType); init(handlerType);