java Spring 应用上下文加载顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4243710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:28:32  来源:igfitidea点击:

Spring Application Context Load Order

javaspringspring-mvcweb.xml

提问by chris

On my web.xml I have a "springmvc" servlet declaration (which has a corresponding springmvc-servlet.xml)

在我的 web.xml 上,我有一个“springmvc”servlet 声明(它有一个相应的 springmvc-servlet.xml)

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/myapp/*</url-pattern>
</servlet-mapping>

I also have my usual applicationContext.xml file.

我也有我常用的 applicationContext.xml 文件。

Which one gets loaded first? The springmvc-servlet.xml or the applicationContext.xml?

哪个先加载?springmvc-servlet.xml 还是 applicationContext.xml?

The reason I'm asking this is whenever I place the <mvc:annotation-driven/>element in the applicationContext.xml, I get a Severe Context error. But when I put that element in the springmvc-servlet.xml, my web app runs fine.

我问这个的原因是每当我将<mvc:annotation-driven/>元素放在 applicationContext.xml 中时,我都会收到 Severe Context 错误。但是当我将该元素放入 springmvc-servlet.xml 时,我的 Web 应用程序运行良好。

Any ideas why?

任何想法为什么?

On another web-app, I have the <mvc:annotation-driven/>inside the applicationContext.xml and it runs fine.

在另一个网络应用程序上,我有<mvc:annotation-driven/>applicationContext.xml 并且它运行良好。

Addendum: I do notice that the presence of aop:configposes conflict against mvc:annotation-driven

附录:我确实注意到aop:config 的存在与mvc:annotation-driven存在冲突

回答by Bozho

the applicationContext.xmlcontext is parent to the dispatcher-servlet.xmlcontext. I don't know whether this means it is loaded first, but it does not matter in your case:

applicationContext.xml上下文是父的dispatcher-servlet.xml上下文。我不知道这是否意味着它首先被加载,但在你的情况下并不重要:

<mvc:annotation-driven />must be in the dispatcher-servlet.xml, because it belongs to the web-part of the application.

<mvc:annotation-driven />必须在 中dispatcher-servlet.xml,因为它属于应用程序的 web 部分。

回答by chris

I solved my problem!

我解决了我的问题!

It turns out it has nothing to do with the load order or where the <mvc:annotation-driven/>is declared.

事实证明,它与加载顺序或<mvc:annotation-driven/>声明的位置无关。

I tried deploying my web-app on another Tomcat and to my surprise there's a stack trace in the localhost log. I had a hint by trial and error that the conflict is with <aop:config/>. But what particular conflict?

我尝试在另一个 Tomcat 上部署我的 web 应用程序,令我惊讶的是,本地主机日志中有一个堆栈跟踪。我通过反复试验得到了一个提示,即冲突与<aop:config/>. 但是有什么特别的冲突?

Then I saw this error in the log file:

然后我在日志文件中看到了这个错误:

java.lang.ClassCastException: org.aspectj.weaver.ResolvedType$Array cannot be cast to org.aspectj.weaver.ReferenceType

So we have a cast exception. I googled that exact error above and found this: Spring 3: adding causes ClassCastException

所以我们有一个演员表异常。我用谷歌搜索了上面那个确切的错误,发现了这个:Spring 3:添加原因 ClassCastException

It appears the thread starter and I have the same exact issue. So I downloaded the aspectj-1.6.10.jar but I was still missing a class. Then it turns out it should be the aspectjweaver-1.6.9

看起来线程启动器和我有同样的问题。所以我下载了 aspectj-1.6.10.jar 但我仍然缺少一个类。然后事实证明它应该是aspectjweaver-1.6.9

I was still using a very old aspectjweaver. It didn't have any version on its name. Problem solved. Case closed.

我还在使用一个非常旧的aspectjweaver。它的名字没有任何版本。问题解决了。案件结案。

By the way as a bonus, I've manually unrolled the <mvc:annotation-driven/>element to its equivalent xml declaration:

顺便说一句,作为奖励,我手动将<mvc:annotation-driven/>元素展开为其等效的 xml 声明:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator" ref="validator" />
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
            <bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter" />
        </list>
    </property>
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />

They're exactly the same when you declare the <mvc:annotation-driven/>based on what I've researched.

当您<mvc:annotation-driven/>根据我的研究声明它们时,它们完全相同。

Thanks to everybody who helped me out.

感谢所有帮助过我的人。

回答by Sean Patrick Floyd

You probably have to add the mvc namespace to the application context:

您可能必须将 mvc 命名空间添加到应用程序上下文中:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>

(other namespaces stripped)

(其他命名空间被剥离)

回答by Michel

Except for web.xml there is no predefined order. This happens:

除了 web.xml 之外,没有预定义的顺序。有时候是这样的:

  • web.xml is loaded by the servlet engine, this triggers the load of all defined servlets, filters, listeners,
  • the ContextLoaderListener loads the root application context XML, this might include a bean definition for a LocalSessionFactoryBean, triggering the load of all Hibernate mapping XML files
  • the DispatcherServlet loads the web application context XML
  • web.xml 由 servlet 引擎加载,这会触发所有定义的 servlet、过滤器、侦听器、
  • ContextLoaderListener 加载根应用程序上下文 XML,这可能包括 LocalSessionFactoryBean 的 bean 定义,触发所有 Hibernate 映射 XML 文件的加载
  • DispatcherServlet 加载 Web 应用程序上下文 XML

Study the web.xml to determine the order in each case.

研究 web.xml 以确定每种情况下的顺序。

see also:

也可以看看:

link

关联