Introduced ControllerAdvice annotation

Classes with this annotation can contain @ExceptionHandler,
@InitBinder, and @ModelAttribute methods that apply to all controllers.
The new annotation is also a component annotation allowing
implementations to be discovered through component scanning.

Issue: SPR-9112
This commit is contained in:
Rossen Stoyanchev
2012-07-26 13:37:49 -04:00
parent f1105812af
commit e65b930e7a
11 changed files with 440 additions and 239 deletions

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
/**
* Indicates the annotated class assists a "Controller".
*
* <p>Serves as a specialization of {@link Component @Component}, allowing for
* implementation classes to be autodetected through classpath scanning.
*
* <p>It is typically used to define {@link ExceptionHandler @ExceptionHandler},
* {@link InitBinder @InitBinder}, and {@link ModelAttribute @ModelAttribute}
* methods that apply across controller class hierarchies.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* An {@linkplain Component @Component} annotation that indicates the annotated class
* contains {@linkplain ExceptionHandler @ExceptionHandler} methods. Such methods
* will be used in addition to {@code @ExceptionHandler} methods in
* {@code @Controller}-annotated classes.
*
* <p>In order for the the annotation to detected, an instance of
* {@code org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver}
* is configured.
*
* <p>Classes with this annotation may use the {@linkplain Order @Order} annotation
* or implement the {@link Ordered} interface to indicate the order in which they
* should be used relative to other such annotated components. However, note that
* the order is only for components registered through {@code @ExceptionResolver},
* i.e. within an
* {@code org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver}.
*
* @author Rossen Stoyanchev
* @since 3.2
*
* @see org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ExceptionResolver {
}

View File

@@ -0,0 +1,136 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind.support;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
/**
* Encapsulates information about an {@linkplain ControllerAdvice @ControllerAdvice}
* bean without requiring the bean to be instantiated.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
public class ControllerAdviceBean implements Ordered {
private final Object bean;
private final int order;
private final BeanFactory beanFactory;
/**
* Create an instance using the given bean name.
* @param beanName the name of the bean
* @param beanFactory a BeanFactory that can be used later to resolve the bean
*/
public ControllerAdviceBean(String beanName, BeanFactory beanFactory) {
Assert.hasText(beanName, "'beanName' must not be null");
Assert.notNull(beanFactory, "'beanFactory' must not be null");
Assert.isTrue(beanFactory.containsBean(beanName),
"Bean factory [" + beanFactory + "] does not contain bean " + "with name [" + beanName + "]");
this.bean = beanName;
this.beanFactory = beanFactory;
this.order = initOrder(this.beanFactory.getType(beanName));
}
/**
* Create an instance using the given bean instance.
* @param bean the bean
*/
public ControllerAdviceBean(Object bean) {
Assert.notNull(bean, "'bean' must not be null");
this.bean = bean;
this.order = initOrder(bean.getClass());
this.beanFactory = null;
}
private static int initOrder(Class<?> beanType) {
Order orderAnnot = AnnotationUtils.findAnnotation(beanType, Order.class);
return (orderAnnot != null) ? orderAnnot.value() : Ordered.LOWEST_PRECEDENCE;
}
/**
* Find the names of beans annotated with
* {@linkplain ControllerAdvice @ControllerAdvice} in the given
* ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
*/
public static List<ControllerAdviceBean> findBeans(ApplicationContext applicationContext) {
List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();
for (String name : applicationContext.getBeanDefinitionNames()) {
if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
beans.add(new ControllerAdviceBean(name, applicationContext));
}
}
return beans;
}
/**
* Return a bean instance if necessary resolving the bean name through the BeanFactory.
*/
public Object resolveBean() {
return (this.bean instanceof String) ? this.beanFactory.getBean((String) this.bean) : this.bean;
}
public int getOrder() {
return this.order;
}
/**
* Returns the type of the contained bean.
* If the bean type is a CGLIB-generated class, the original, user-defined class is returned.
*/
public Class<?> getBeanType() {
Class<?> clazz = (this.bean instanceof String)
? this.beanFactory.getType((String) this.bean) : this.bean.getClass();
return ClassUtils.getUserClass(clazz);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o != null && o instanceof ControllerAdviceBean) {
ControllerAdviceBean other = (ControllerAdviceBean) o;
return this.bean.equals(other.bean);
}
return false;
}
@Override
public int hashCode() {
return 31 * this.bean.hashCode();
}
@Override
public String toString() {
return bean.toString();
}
}

View File

@@ -22,7 +22,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.core.ExceptionDepthComparator;
@@ -34,57 +33,47 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.HandlerMethodSelector;
/**
* Given a set of @{@link ExceptionHandler} methods at initialization, finds
* the best matching method mapped to an exception at runtime.
*
* <p>Exception mappings are extracted from the method @{@link ExceptionHandler}
* annotation or by looking for {@link Throwable} method arguments.
*
* Discovers {@linkplain ExceptionHandler @ExceptionHandler} methods in a given class
* type, including all super types, and helps to resolve an Exception to the method
* its mapped to. Exception mappings are defined through {@code @ExceptionHandler}
* annotation or by looking at the signature of an {@code @ExceptionHandler} method.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class ExceptionHandlerMethodResolver {
private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis");
private final Map<Class<? extends Throwable>, Method> mappedMethods =
private final Map<Class<? extends Throwable>, Method> mappedMethods =
new ConcurrentHashMap<Class<? extends Throwable>, Method>();
private final Map<Class<? extends Throwable>, Method> exceptionLookupCache =
new ConcurrentHashMap<Class<? extends Throwable>, Method>();
/**
* A constructor that finds {@link ExceptionHandler} methods in a handler.
* @param handlerType the handler to inspect for exception handler methods.
* @throws IllegalStateException
* If an exception type is mapped to two methods.
* @throws IllegalArgumentException
* If an @{@link ExceptionHandler} method is not mapped to any exceptions.
* A constructor that finds {@link ExceptionHandler} methods in the given type.
* @param handlerType the type to introspect
*/
public ExceptionHandlerMethodResolver(Class<?> handlerType) {
init(HandlerMethodSelector.selectMethods(handlerType, EXCEPTION_HANDLER_METHODS));
}
private void init(Set<Method> exceptionHandlerMethods) {
for (Method method : exceptionHandlerMethods) {
for (Class<? extends Throwable> exceptionType : detectMappedExceptions(method)) {
for (Method method : HandlerMethodSelector.selectMethods(handlerType, EXCEPTION_HANDLER_METHODS)) {
for (Class<? extends Throwable> exceptionType : detectExceptionMappings(method)) {
addExceptionMapping(exceptionType, method);
}
}
}
/**
* Detect the exceptions an @{@link ExceptionHandler} method is mapped to.
* If the method @{@link ExceptionHandler} annotation doesn't have any,
* scan the method signature for all arguments of type {@link Throwable}.
* Extract exception mappings from the {@code @ExceptionHandler} annotation
* first and as a fall-back from the method signature.
*/
@SuppressWarnings("unchecked")
private List<Class<? extends Throwable>> detectMappedExceptions(Method method) {
private List<Class<? extends Throwable>> detectExceptionMappings(Method method) {
List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>();
ExceptionHandler annotation = AnnotationUtils.findAnnotation(method, ExceptionHandler.class);
if (annotation != null) {
result.addAll(Arrays.asList(annotation.value()));
}
result.addAll(Arrays.asList(annotation.value()));
if (result.isEmpty()) {
for (Class<?> paramType : method.getParameterTypes()) {
if (Throwable.class.isAssignableFrom(paramType)) {
@@ -92,7 +81,9 @@ public class ExceptionHandlerMethodResolver {
}
}
}
Assert.notEmpty(result, "No exception types mapped to {" + method + "}");
return result;
}
@@ -106,10 +97,17 @@ public class ExceptionHandlerMethodResolver {
}
/**
* Find a method to handle the given exception. If more than one match is
* found, the best match is selected via {@link ExceptionDepthComparator}.
* Whether the contained type has any exception mappings.
*/
public boolean hasExceptionMappings() {
return (this.mappedMethods.size() > 0);
}
/**
* Find a method to handle the given exception.
* Use {@link ExceptionDepthComparator} if more than one match is found.
* @param exception the exception
* @return an @{@link ExceptionHandler} method, or {@code null}
* @return a method to handle the exception or {@code null}
*/
public Method resolveMethod(Exception exception) {
Class<? extends Exception> exceptionType = exception.getClass();
@@ -122,7 +120,7 @@ public class ExceptionHandlerMethodResolver {
}
/**
* Return the method mapped to the exception type, or {@code null}.
* Return the method mapped to the given exception type or {@code null}.
*/
private Method getMappedMethod(Class<? extends Exception> exceptionType) {
List<Class<? extends Throwable>> matches = new ArrayList<Class<? extends Throwable>>();
@@ -141,7 +139,7 @@ public class ExceptionHandlerMethodResolver {
}
/**
* A filter for selecting @{@link ExceptionHandler} methods.
* A filter for selecting {@code @ExceptionHandler} methods.
*/
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {