001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.method.annotation; 018 019import javax.servlet.ServletException; 020 021import org.springframework.beans.factory.annotation.Value; 022import org.springframework.beans.factory.config.ConfigurableBeanFactory; 023import org.springframework.core.MethodParameter; 024import org.springframework.web.bind.WebDataBinder; 025import org.springframework.web.context.request.NativeWebRequest; 026 027/** 028 * Resolves method arguments annotated with {@code @Value}. 029 * 030 * <p>An {@code @Value} does not have a name but gets resolved from the default 031 * value string, which may contain ${...} placeholder or Spring Expression 032 * Language #{...} expressions. 033 * 034 * <p>A {@link WebDataBinder} may be invoked to apply type conversion to 035 * resolved argument value. 036 * 037 * @author Rossen Stoyanchev 038 * @since 3.1 039 */ 040public class ExpressionValueMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver { 041 042 /** 043 * @param beanFactory a bean factory to use for resolving ${...} 044 * placeholder and #{...} SpEL expressions in default values; 045 * or {@code null} if default values are not expected to contain expressions 046 */ 047 public ExpressionValueMethodArgumentResolver(ConfigurableBeanFactory beanFactory) { 048 super(beanFactory); 049 } 050 051 052 @Override 053 public boolean supportsParameter(MethodParameter parameter) { 054 return parameter.hasParameterAnnotation(Value.class); 055 } 056 057 @Override 058 protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { 059 Value annotation = parameter.getParameterAnnotation(Value.class); 060 return new ExpressionValueNamedValueInfo(annotation); 061 } 062 063 @Override 064 protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception { 065 // No name to resolve 066 return null; 067 } 068 069 @Override 070 protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { 071 throw new UnsupportedOperationException("@Value is never required: " + parameter.getMethod()); 072 } 073 074 075 private static class ExpressionValueNamedValueInfo extends NamedValueInfo { 076 077 private ExpressionValueNamedValueInfo(Value annotation) { 078 super("@Value", false, annotation.value()); 079 } 080 } 081 082}