简体   繁体   中英

Is it possible to integrate Spring MVC with Guice?

It's probably a dumb question for the experts in Spring MVC but I don't know 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 (eg 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.

一个并排的解决方案: http//www.earldouglas.com/guice-with-a-spring-twist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM