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 <mvc:annotation-driven/>
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 <mvc:annotation-driven/>
gets called first, and your own one gets ignored.
It would be better if the DefaultAnnotationHandlerMapping
registered by <mvc:annotation-driven/>
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 <mvc:annotation-driven/>
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…