diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java index e780d35f95a..b60a03cccf4 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -59,6 +59,9 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac private static final String AJC_MAGIC = "ajc$"; + private static final Class[] ASPECTJ_ANNOTATION_CLASSES = new Class[] { + Pointcut.class, Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class}; + /** Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); @@ -122,14 +125,12 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac /** * Find and return the first AspectJ annotation on the given method - * (there should only be one anyway...) + * (there should only be one anyway...). */ @SuppressWarnings("unchecked") protected static AspectJAnnotation findAspectJAnnotationOnMethod(Method method) { - Class[] classesToLookFor = new Class[] { - Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class, Pointcut.class}; - for (Class c : classesToLookFor) { - AspectJAnnotation foundAnnotation = findAnnotation(method, (Class) c); + for (Class clazz : ASPECTJ_ANNOTATION_CLASSES) { + AspectJAnnotation foundAnnotation = findAnnotation(method, (Class) clazz); if (foundAnnotation != null) { return foundAnnotation; } @@ -148,6 +149,9 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac } + /** + * AspectJ annotation types. + */ protected enum AspectJAnnotationType { AtPointcut, @@ -167,16 +171,16 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac private static final String[] EXPRESSION_PROPERTIES = new String[] {"value", "pointcut"}; - private static Map, AspectJAnnotationType> annotationTypes = - new HashMap, AspectJAnnotationType>(); + private static Map, AspectJAnnotationType> annotationTypeMap = + new HashMap, AspectJAnnotationType>(8); static { - annotationTypes.put(Pointcut.class,AspectJAnnotationType.AtPointcut); - annotationTypes.put(After.class,AspectJAnnotationType.AtAfter); - annotationTypes.put(AfterReturning.class,AspectJAnnotationType.AtAfterReturning); - annotationTypes.put(AfterThrowing.class,AspectJAnnotationType.AtAfterThrowing); - annotationTypes.put(Around.class,AspectJAnnotationType.AtAround); - annotationTypes.put(Before.class,AspectJAnnotationType.AtBefore); + annotationTypeMap.put(Pointcut.class, AspectJAnnotationType.AtPointcut); + annotationTypeMap.put(Before.class, AspectJAnnotationType.AtBefore); + annotationTypeMap.put(After.class, AspectJAnnotationType.AtAfter); + annotationTypeMap.put(AfterReturning.class, AspectJAnnotationType.AtAfterReturning); + annotationTypeMap.put(AfterThrowing.class, AspectJAnnotationType.AtAfterThrowing); + annotationTypeMap.put(Around.class, AspectJAnnotationType.AtAround); } private final A annotation; @@ -202,9 +206,9 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac } private AspectJAnnotationType determineAnnotationType(A annotation) { - for (Class type : annotationTypes.keySet()) { + for (Class type : annotationTypeMap.keySet()) { if (type.isInstance(annotation)) { - return annotationTypes.get(type); + return annotationTypeMap.get(type); } } throw new IllegalStateException("Unknown annotation type: " + annotation.toString()); @@ -213,19 +217,15 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac private String resolveExpression(A annotation) throws Exception { String expression = null; for (String methodName : EXPRESSION_PROPERTIES) { - Method method; try { - method = annotation.getClass().getDeclaredMethod(methodName); - } - catch (NoSuchMethodException ex) { - method = null; - } - if (method != null) { + Method method = annotation.getClass().getDeclaredMethod(methodName); String candidate = (String) method.invoke(annotation); if (StringUtils.hasText(candidate)) { expression = candidate; } } + catch (NoSuchMethodException ex) { + } } return expression; } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java index 491ed14c97b..de466a4540c 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java @@ -53,8 +53,8 @@ import org.springframework.util.ClassUtils; * @author Stephane Nicoll * @since 2.5 * @see ContextAnnotationAutowireCandidateResolver - * @see CommonAnnotationBeanPostProcessor * @see ConfigurationClassPostProcessor + * @see CommonAnnotationBeanPostProcessor * @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor * @see org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor * @see org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor @@ -102,7 +102,6 @@ public class AnnotationConfigUtils { public static final String PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME = "org.springframework.context.annotation.internalPersistenceAnnotationProcessor"; - private static final String PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME = "org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"; @@ -155,7 +154,7 @@ public class AnnotationConfigUtils { } } - Set beanDefs = new LinkedHashSet(4); + Set beanDefs = new LinkedHashSet(8); if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) { RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class); @@ -202,6 +201,7 @@ public class AnnotationConfigUtils { def.setSource(source); beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME)); } + if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) { RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class); def.setSource(source); diff --git a/spring-context/src/main/java/org/springframework/context/event/DefaultEventListenerFactory.java b/spring-context/src/main/java/org/springframework/context/event/DefaultEventListenerFactory.java index 8d6cd686445..b0ffe7516a1 100644 --- a/spring-context/src/main/java/org/springframework/context/event/DefaultEventListenerFactory.java +++ b/spring-context/src/main/java/org/springframework/context/event/DefaultEventListenerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -24,6 +24,7 @@ import org.springframework.core.Ordered; /** * Default {@link EventListenerFactory} implementation that supports the * regular {@link EventListener} annotation. + * *

Used as "catch-all" implementation by default. * * @author Stephane Nicoll @@ -33,15 +34,17 @@ public class DefaultEventListenerFactory implements EventListenerFactory, Ordere private int order = LOWEST_PRECEDENCE; - @Override - public int getOrder() { - return order; - } public void setOrder(int order) { this.order = order; } + @Override + public int getOrder() { + return this.order; + } + + public boolean supportsMethod(Method method) { return true; } diff --git a/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java b/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java index 24b8cf97b01..c3a19fe4661 100644 --- a/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -44,8 +44,7 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; /** - * Register {@link EventListener} annotated method as individual {@link ApplicationListener} - * instances. + * Registers {@link EventListener} methods as individual {@link ApplicationListener} instances. * * @author Stephane Nicoll * @author Juergen Hoeller diff --git a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java index 19d5ab82f86..57aae2b1340 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java +++ b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -21,11 +21,11 @@ import java.lang.reflect.Method; import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListenerFactory; import org.springframework.core.Ordered; -import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.AnnotatedElementUtils; /** * {@link EventListenerFactory} implementation that handles {@link TransactionalEventListener} - * annotated method. + * annotated methods. * * @author Stephane Nicoll * @since 4.2 @@ -47,7 +47,7 @@ public class TransactionalEventListenerFactory implements EventListenerFactory, @Override public boolean supportsMethod(Method method) { - return (AnnotationUtils.findAnnotation(method, TransactionalEventListener.class) != null); + return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class); } @Override diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index ac2fbf714b4..28717f2eb54 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -116,6 +116,28 @@ import org.springframework.web.util.WebUtils; public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter implements BeanFactoryAware, InitializingBean { + /** + * MethodFilter that matches {@link InitBinder @InitBinder} methods. + */ + public static final MethodFilter INIT_BINDER_METHODS = new MethodFilter() { + @Override + public boolean matches(Method method) { + return (AnnotationUtils.findAnnotation(method, InitBinder.class) != null); + } + }; + + /** + * MethodFilter that matches {@link ModelAttribute @ModelAttribute} methods. + */ + public static final MethodFilter MODEL_ATTRIBUTE_METHODS = new MethodFilter() { + @Override + public boolean matches(Method method) { + return (AnnotationUtils.findAnnotation(method, RequestMapping.class) == null && + AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null); + } + }; + + private List customArgumentResolvers; private HandlerMethodArgumentResolverComposite argumentResolvers; @@ -945,26 +967,4 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter return mav; } - - /** - * MethodFilter that matches {@link InitBinder @InitBinder} methods. - */ - public static final MethodFilter INIT_BINDER_METHODS = new MethodFilter() { - @Override - public boolean matches(Method method) { - return AnnotationUtils.findAnnotation(method, InitBinder.class) != null; - } - }; - - /** - * MethodFilter that matches {@link ModelAttribute @ModelAttribute} methods. - */ - public static final MethodFilter MODEL_ATTRIBUTE_METHODS = new MethodFilter() { - @Override - public boolean matches(Method method) { - return ((AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) && - (AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null)); - } - }; - }