How to register handler interceptors with spring mvc 3.0?

By default, Spring will register a BeanNameUrlHandlerMapping and a DefaultAnnotationHandlerMapping without any explicit config required.

By default, Spring will register a BeanNameUrlHandlerMapping, and a DefaultAnnotationHandlerMapping, without any explicit config required. If you define your own HandlerMapping beans, then the default ones will not be registered, and you'll just get the explicitly declared ones. So far, so good.

The problem comes when you add to the mix. This also declares its own DefaultAnnotationHandlerMapping, which replaces the defaults. However, if you also declare your own one, then you end up with two.

Since they are consulted in order of declaration, this usually means the one registered by gets called first, and your own one gets ignored. It would be better if the DefaultAnnotationHandlerMapping registered by acted like the default one, i.e. If explicitly declared ones took precedence, but that's not the way they wrote it.My current preference is to not use at all, it's too confusing, and too unpredictable when mixed with other config options.

It doesn't really do anything especially complex, it's not difficult or verbose to explicitly add the stuff that it does for you, and the end result is easier to follow.

Good explanation. Marking it as accepted. I'll ask another question with what remains unclear - i.e.

How to activate parts of what mvc:annotation-driven is doing. – Bozho Jul 12 '10 at 18:58 2 jira.springsource. Org/browse/SPR-6524 seems there is an issue for that, and Juergen Hoeller's comments that this is intended.

– Bozho Jul 12 '10 at 19:00 - no need of another question :) – Bozho Jul 12 '10 at 19:04.

The reason for this behaviour is that two beans of type org.springframework.web.servlet.mvc.annotation. DefaultAnnotationHandlerMapping exist in the application context. Spring resolves the two, but asks only the first for interceptors.To fix this, the following init parameter should be set to the DispatcherServlet detectAllHandlerMappings false This makes the dispatcher servlet use only the handlerMapping defined in the x-servlet.

Xml It is beyond me why this is the default behaviour. I'm expecting an answer from the spring community.

2 The defaults when using the namespace are wildly confusing. I prefer the older style of specifying everything yourself - the namespace configuration methods seem to rely too much on not-easily-found-in-documentation defaults – matt be Jul 12 '10 at 17:59.

1 +1, that's good. But I can't use ref there. – Bozho Jul 12 '10 at 18:16 1 @Bozho: You can use a BeanReferenceFactoryBean (yes, I know that it's ugly).

– axtavt Jul 12 '10 at 18:33 yes, it is :) I think I'll stick to my solution for now. – Bozho Jul 12 '10 at 18:36.

What about the resources that doesn't use the DispatchServlet myServlet org.springframework.web.servlet. DispatcherServlet detectAllHandlerMappings false 1 myServlet *. Html Because, when I go to myserver.com:8080/ it doesn't pass through the interceptor :(.

Some suggestions?

Up vote 1 down vote favorite 1 share g+ share fb share tw.

It should be easy: but this way the interceptor isn't called. Java spring spring-mvc link|improve this question asked Jul 12 '10 at 17:23Bozho161k10136297 84% accept rate.

– skaffman Jul 12 '10 at 18:25 Yes, in order to activate javax. Validation handling (and perhaps a pile of other things) – Bozho Jul 12 '10 at 18:27.

By default, Spring will register a BeanNameUrlHandlerMapping, and a DefaultAnnotationHandlerMapping, without any explicit config required. If you define your own HandlerMapping beans, then the default ones will not be registered, and you'll just get the explicitly declared ones. So far, so good.

The problem comes when you add to the mix. This also declares its own DefaultAnnotationHandlerMapping, which replaces the defaults. However, if you also declare your own one, then you end up with two.

Since they are consulted in order of declaration, this usually means the one registered by gets called first, and your own one gets ignored. It would be better if the DefaultAnnotationHandlerMapping registered by acted like the default one, i.e. If explicitly declared ones took precedence, but that's not the way they wrote it.

My current preference is to not use at all, it's too confusing, and too unpredictable when mixed with other config options. It doesn't really do anything especially complex, it's not difficult or verbose to explicitly add the stuff that it does for you, and the end result is easier to follow.

Good explanation. Marking it as accepted. I'll ask another question with what remains unclear - i.e.

How to activate parts of what mvc:annotation-driven is doing. – Bozho Jul 12 '10 at 18:58 2 jira.springsource.org/browse/SPR-6524 seems there is an issue for that, and Juergen Hoeller's comments that this is intended. – Bozho Jul 12 '10 at 19:00 - no need of another question :) – Bozho Jul 12 '10 at 19:04.

The reason for this behaviour is that two beans of type org.springframework.web.servlet.mvc.annotation. DefaultAnnotationHandlerMapping exist in the application context. Spring resolves the two, but asks only the first for interceptors.

To fix this, the following init parameter should be set to the DispatcherServlet detectAllHandlerMappings false This makes the dispatcher servlet use only the handlerMapping defined in the x-servlet. Xml It is beyond me why this is the default behaviour. I'm expecting an answer from the spring community.

In Spring MVC 3.0 you can use instead of manual defining the handler mapping.

What about the resources that doesn't use the DispatchServlet myServlet org.springframework.web.servlet. DispatcherServlet detectAllHandlerMappings false 1 myServlet *. Html Because, when I go to http://myserver.

Com:8080/ it doesn't pass through the interceptor :(. Some suggestions?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions