开发者

Is it possible to integrate Spring MVC with Guice?

开发者 https://www.devze.com 2023-02-06 22:22 出处:网络
It\'s probably a dumb question for the experts in Spring MVC but I don\'t kno开发者_如何学运维w how does it work under the hood that\'s why I ask.

It's probably a dumb question for the experts in Spring MVC but I don't kno开发者_如何学运维w how does it work under the hood that's why I ask.

Another wording for the same question: are there any dependencies on Spring DI inside Spring MVC?


I am pretty sure it's not possible to use Spring MVC without the IOC container.

For example: at the heart of Spring MVC lies the DispatcherServlet. DispatcherServlet initializes itself using these methods:

/**
 * This implementation calls {@link #initStrategies}.
 */
@Override
protected void onRefresh(ApplicationContext context) {
    initStrategies(context);
}

/**
 * Initialize the strategy objects that this servlet uses.
 * <p>May be overridden in subclasses in order to initialize
     * further strategy objects.
 */
protected void initStrategies(ApplicationContext context) {
    initMultipartResolver(context);
    initLocaleResolver(context);
    initThemeResolver(context);
    initHandlerMappings(context);
    initHandlerAdapters(context);
    initHandlerExceptionResolvers(context);
    initRequestToViewNameTranslator(context);
    initViewResolvers(context);
}

So you can see, everything is tightly integrated with the ApplicationContext interface.

So your only chance would be to have a Guice implementation of ApplicationContext, and that would be very far-fetched, I guess.


It should be possible to use some of the more primitive Spring MVC functionality without using the Spring IOC container - this is, after all, the whole point of IoC.

It's going to be difficult, though, since many of the Spring MVC components use the Spring-proprietary lifecycle callbacks (e.g. InitializingBean and DisposableBean) which Guice won't know about. You'd have to handle those yourself.

A better question, though, is why would you want to do this? Without Spring IoC being used, Spring MVC loses most of its appeal. I don't see why you would want to use one without the other.


a side-by-side solution: http://www.earldouglas.com/guice-with-a-spring-twist

0

精彩评论

暂无评论...
验证码 换一张
取 消