Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 247
Roman Avatar asked Jan 24 '11 11:01

Roman


Video Answer


2 Answers

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.

like image 107
Sean Patrick Floyd Avatar answered Sep 27 '22 21:09

Sean Patrick Floyd


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.

like image 44
skaffman Avatar answered Sep 27 '22 23:09

skaffman