Setting HandlerInterceptors in the context of @Configuration

I am using @Configuration in Spring 3.0.6 to use dependency injection without using .xml definition .xml .

I use a mixed approach for defining controllers, I put them in DispatcherServlet @Configuration using @Bean("/sample/path") and use @RequestMapping in my methods to further refine the mapping (including using it to add to the bean).

This works well with the default HandlerMapper configuration.

Now I want to add HandlerInterceptors to the mix. It seems I need to duplicate the default configuration of HandlerMapper and then add my HandlerInterceptor .

However, this does not seem like a great idea. Is there a way to add my HandlerInterceptor to the default configuration of HandlerMapper ?

+4
source share
1 answer

By default, "confgiuration HandlerMapper" do you mean declaring BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping beans, and then setting their "interceptors" properties? In this case, I do not see much duplication.

An alternative would be to declare a bean of type MappedInterceptor. It is detected by type and applied to all incoming requests or limited to path templates.

+1
source

Source: https://habr.com/ru/post/1381558/


All Articles