[英]Spring Application Context Load Order
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. 我问这个的原因是每当我在applicationContext.xml中放置
<mvc:annotation-driven/>
元素时,我都会遇到严重上下文错误。 But when I put that element in the springmvc-servlet.xml, my web app runs fine. 但是当我将该元素放在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. 在另一个web-app上,我在applicationContext.xml中有
<mvc:annotation-driven/>
,它运行正常。
Addendum: I do notice that the presence of aop:config poses conflict against mvc:annotation-driven 附录:我注意到aop:config的存在与mvc:annotation-driven冲突
the applicationContext.xml
context is parent to the dispatcher-servlet.xml
context. applicationContext.xml
上下文是dispatcher-servlet.xml
上下文的父dispatcher-servlet.xml
。 I don't know whether this means it is loaded first, but it does not matter in your case: 我不知道这是否意味着它首先被加载,但在你的情况下并不重要:
<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部分。
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. 我尝试在另一台Tomcat上部署我的web-app,令我惊讶的是,localhost日志中有一个堆栈跟踪。 I had a hint by trial and error that the conflict is with
<aop:config/>
. 我通过反复试验暗示冲突与
<aop:config/>
。 But what particular conflict? 但是什么特别的冲突?
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
我用google搜索上面的确切错误并发现: 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.
所以我下载了aspectj-1.6.10.jar,但我仍然缺少一个课程。 Then it turns out it should be the aspectjweaver-1.6.9
然后事实证明它应该是aspectjweaver-1.6.9
I was still using a very old aspectjweaver. 我还在使用一个非常古老的aspectjweaver。 It didn't have any version on its name.
它的名字上没有任何版本。 Problem solved.
问题解决了。 Case closed.
案件结案。
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.MappingJacksonHttpMessageConverter" />
</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. 感谢所有帮助过我的人。
Except for web.xml there is no predefined order. 除了web.xml之外,没有预定义的顺序。 This happens:
有时候是这样的:
Study the web.xml to determine the order in each case. 研究web.xml以确定每种情况下的顺序。
see also: 也可以看看:
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) (其他名称空间被剥离)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.