|
19 | 19 | import static org.junit.Assert.assertEquals;
|
20 | 20 | import static org.junit.Assert.assertNotNull;
|
21 | 21 |
|
| 22 | +import java.lang.reflect.Method; |
22 | 23 | import java.text.SimpleDateFormat;
|
23 | 24 | import java.util.Arrays;
|
24 | 25 | import java.util.Collection;
|
25 | 26 | import java.util.Date;
|
26 | 27 |
|
| 28 | +import org.aopalliance.aop.Advice; |
27 | 29 | import org.junit.Test;
|
28 | 30 | import org.junit.runner.RunWith;
|
29 | 31 | import org.junit.runners.Parameterized;
|
30 | 32 | import org.junit.runners.Parameterized.Parameters;
|
| 33 | +import org.springframework.aop.Pointcut; |
31 | 34 | import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
32 | 35 | import org.springframework.aop.interceptor.SimpleTraceInterceptor;
|
33 | 36 | import org.springframework.aop.support.DefaultPointcutAdvisor;
|
| 37 | +import org.springframework.aop.support.StaticMethodMatcherPointcut; |
34 | 38 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
35 | 39 | import org.springframework.beans.propertyeditors.CustomDateEditor;
|
| 40 | +import org.springframework.core.annotation.AnnotationUtils; |
36 | 41 | import org.springframework.mock.web.MockHttpServletRequest;
|
37 | 42 | import org.springframework.mock.web.MockHttpServletResponse;
|
38 | 43 | import org.springframework.stereotype.Controller;
|
@@ -91,21 +96,29 @@ public static Collection<Object[]> handlerTypes() {
|
91 | 96 |
|
92 | 97 | private ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver();
|
93 | 98 |
|
94 |
| - public HandlerMethodAnnotationDetectionTests(Class<?> controllerType, boolean useAutoProxy) { |
| 99 | + public HandlerMethodAnnotationDetectionTests(final Class<?> controllerType, boolean useAutoProxy) { |
95 | 100 | GenericWebApplicationContext context = new GenericWebApplicationContext();
|
96 | 101 | context.registerBeanDefinition("controller", new RootBeanDefinition(controllerType));
|
| 102 | + context.registerBeanDefinition("handlerMapping", new RootBeanDefinition(RequestMappingHandlerMapping.class)); |
| 103 | + context.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(RequestMappingHandlerAdapter.class)); |
| 104 | + context.registerBeanDefinition("exceptionResolver", new RootBeanDefinition(ExceptionHandlerExceptionResolver.class)); |
97 | 105 | if (useAutoProxy) {
|
98 | 106 | DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
|
99 | 107 | autoProxyCreator.setBeanFactory(context.getBeanFactory());
|
100 | 108 | context.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
|
101 |
| - context.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); |
| 109 | + context.registerBeanDefinition("controllerAdvice", new RootBeanDefinition(ControllerAdvice.class)); |
102 | 110 | }
|
103 | 111 | context.refresh();
|
104 | 112 |
|
105 |
| - handlerMapping.setApplicationContext(context); |
106 |
| - handlerMapping.afterPropertiesSet(); |
107 |
| - handlerAdapter.afterPropertiesSet(); |
108 |
| - exceptionResolver.afterPropertiesSet(); |
| 113 | + this.handlerMapping = context.getBean(RequestMappingHandlerMapping.class); |
| 114 | + this.handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class); |
| 115 | + this.exceptionResolver = context.getBean(ExceptionHandlerExceptionResolver.class); |
| 116 | + } |
| 117 | + |
| 118 | + class TestPointcut extends StaticMethodMatcherPointcut { |
| 119 | + public boolean matches(Method method, Class<?> clazz) { |
| 120 | + return method.getName().equals("hashCode"); |
| 121 | + } |
109 | 122 | }
|
110 | 123 |
|
111 | 124 | @Test
|
@@ -232,11 +245,11 @@ static interface MappingInterface {
|
232 | 245 | /**
|
233 | 246 | * CONTROLLER WITH INTERFACE
|
234 | 247 | *
|
235 |
| - * No AOP: |
236 |
| - * All annotations can be on interface methods except parameter annotations. |
237 |
| - * |
238 | 248 | * JDK Dynamic proxy:
|
239 | 249 | * All annotations must be on the interface.
|
| 250 | + * |
| 251 | + * Without AOP: |
| 252 | + * Annotations can be on interface methods except parameter annotations. |
240 | 253 | */
|
241 | 254 | static class InterfaceController implements MappingInterface {
|
242 | 255 |
|
@@ -391,4 +404,21 @@ public String handleException(Exception exception) {
|
391 | 404 | static class SupportClassController extends MappingSupportClass {
|
392 | 405 | }
|
393 | 406 |
|
| 407 | + |
| 408 | + static class ControllerAdvice extends DefaultPointcutAdvisor { |
| 409 | + |
| 410 | + public ControllerAdvice() { |
| 411 | + super(getControllerPointcut(), new SimpleTraceInterceptor()); |
| 412 | + } |
| 413 | + |
| 414 | + private static StaticMethodMatcherPointcut getControllerPointcut() { |
| 415 | + return new StaticMethodMatcherPointcut() { |
| 416 | + public boolean matches(Method method, Class<?> targetClass) { |
| 417 | + return ((AnnotationUtils.findAnnotation(targetClass, Controller.class) != null) || |
| 418 | + (AnnotationUtils.findAnnotation(targetClass, RequestMapping.class) != null)); |
| 419 | + } |
| 420 | + }; |
| 421 | + } |
| 422 | + } |
| 423 | + |
394 | 424 | }
|
0 commit comments