0

As I understand from docs the <mvc:annotation-driven/> is just a shortcut for registering RequestMappingHandlerMapping, RequestMappingHandlerAdapter, ExceptionHandlerExceptionResolver and some other things.

My question is: how I can manually define same functionality as provides <mvc:annotation-driven/> without using <mvc:annotation-driven/> ?

I.e. I need example of Spring configuration which defines and configures all the beans implicitly created by <mvc:annotation-driven/> shortcut. In other words I need compiled version of <mvc:annotation-driven/> (with all default values).

P.S. I need it because <mvc:annotation-driven/> is not so flexible and doesn't provide ways to configure implicitly registered beans (I want to configure alwaysUseFullPath property of RequestMappingHandlerMapping)

WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • Which is pretty easy to do. Create a bean implementing `BeanPostProcessor` check in the `postProcessBeforeInitialization` if the bean is of the type you need, set the additional properties. Which is a lot easier then trying to configure all the things the namespace does. – M. Deinum Jul 06 '16 at 05:44

2 Answers2

2

You can even do it without manually configuring everything. Just configure the UrlPathHelper and set it on the configuration.

<bean id="urlPathHelper" class="org.springframework.web.util.UrlPathHelper">
    <property name="alwaysUseFullPath" value="true" />
<bean>

<mvc:annotation-driven path-helper="urlPathHelper" />

If that doesn't work because you are on an older spring version use a BeanPostProcessor and in its postProcessBeforeInitialization do the additional settings/init you want. It would be to cumbersome to do trying to recreate all the options of <mvc:annotation-driven />so, just to set a single property. Instead create a bean which implementsBeanPostProcessor`

public class WebMvcConfigurer implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof RequestMappingHandlerMapping) {
            ((RequestMappingHandlerMapping) bean).setAlwaysUseFullPath(true);
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            return bean;
    }
}

Just add this bean to your xml configuration and be done.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
0

You could try by removing <mvc:annotation-driven/> tag and define your all bean explicitly using <bean> tag and other helping tag.

Also see this Howto get rid of <mvc:annotation-driven />?

Community
  • 1
  • 1
rev_dihazum
  • 818
  • 1
  • 9
  • 19