简体   繁体   English

是否可以将Spring MVC与Guice集成?

[英]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. 对于Spring MVC的专家来说,这可能是一个愚蠢的问题,但我不知道它是如何在引擎盖下工作的,这就是我问的原因。

Another wording for the same question: are there any dependencies on Spring DI inside Spring MVC? 针对同一问题的另一个措辞:Spring MVC中是否存在对Spring DI的依赖?

I am pretty sure it's not possible to use Spring MVC without the IOC container. 我很确定没有IOC容器就不可能使用Spring MVC。

For example: at the heart of Spring MVC lies the DispatcherServlet . 例如:Spring MVC的核心是DispatcherServlet DispatcherServlet initializes itself using these methods: DispatcherServlet使用以下方法初始化自身:

/**
 * 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. 所以你可以看到,一切都与ApplicationContext接口紧密集成。

So your only chance would be to have a Guice implementation of ApplicationContext, and that would be very far-fetched, I guess. 因此,你唯一的机会就是拥有一个ApplicationContext的Guice实现,我想这将是非常牵强的。

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. 应该可以在不使用Spring IOC容器的情况下使用一些更原始的Spring MVC功能 - 毕竟,这是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. 但是,这很困难,因为许多Spring MVC组件使用Guice不会知道的Spring专有生命周期回调(例如InitializingBeanDisposableBean )。 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. 如果没有使用Spring IoC,Spring MVC将失去其大部分吸引力。 I don't see why you would want to use one without the other. 我不明白为什么你会想要使用一个没有另一个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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