![](/img/trans.png)
[英]how to load a 'mini' spring context from WEB-INF in order to initialize the real application context
[英]Spring Application Context Load Order
在我的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>
我也有我常用的applicationContext.xml文件。
首先加載哪一個? springmvc-servlet.xml還是applicationContext.xml?
我問這個的原因是每當我在applicationContext.xml中放置<mvc:annotation-driven/>
元素時,我都會遇到嚴重上下文錯誤。 但是當我將該元素放在springmvc-servlet.xml中時,我的Web應用程序運行正常。
有什么想法嗎?
在另一個web-app上,我在applicationContext.xml中有<mvc:annotation-driven/>
,它運行正常。
附錄:我注意到aop:config的存在與mvc:annotation-driven沖突
applicationContext.xml
上下文是dispatcher-servlet.xml
上下文的父dispatcher-servlet.xml
。 我不知道這是否意味着它首先被加載,但在你的情況下並不重要:
<mvc:annotation-driven />
必須位於dispatcher-servlet.xml
,因為它屬於應用程序的Web部分。
我解決了我的問題!
事實證明它與加載順序或聲明<mvc:annotation-driven/>
地方無關。
我嘗試在另一台Tomcat上部署我的web-app,令我驚訝的是,localhost日志中有一個堆棧跟蹤。 我通過反復試驗暗示沖突與<aop:config/>
。 但是什么特別的沖突?
然后我在日志文件中看到了這個錯誤:
java.lang.ClassCastException: org.aspectj.weaver.ResolvedType$Array cannot be cast to org.aspectj.weaver.ReferenceType
所以我們有一個強制轉換異常。 我用google搜索上面的確切錯誤並發現: Spring 3:添加導致ClassCastException
看起來線程啟動器和我有完全相同的問題。 所以我下載了aspectj-1.6.10.jar,但我仍然缺少一個課程。 然后事實證明它應該是aspectjweaver-1.6.9
我還在使用一個非常古老的aspectjweaver。 它的名字上沒有任何版本。 問題解決了。 案件結案。
作為獎勵,我手動將<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" />
當你根據我研究的內容聲明<mvc:annotation-driven/>
時,它們完全相同。
感謝所有幫助過我的人。
除了web.xml之外,沒有預定義的順序。 有時候是這樣的:
研究web.xml以確定每種情況下的順序。
也可以看看:
您可能必須將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"
>
(其他名稱空間被剝離)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.