简体   繁体   中英

Spring Application Context Load Order

On my web.xml I have a "springmvc" servlet declaration (which has a corresponding 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.

Which one gets loaded first? The springmvc-servlet.xml or the 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.

Any ideas why?

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

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

the applicationContext.xml context is parent to the dispatcher-servlet.xml context. 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.

I solved my problem!

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

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?

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

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

I was still using a very old 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:

<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.

Thanks to everybody who helped me out.

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

  • 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

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

see also:

link

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

<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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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