Spring Application Context Load Order

12,940

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
12,940
chris
Author by

chris

I like to be type-safe just like a typical guy with a typical life from a typical world of Java.

Updated on June 21, 2022

Comments

  • chris
    chris almost 2 years

    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

  • chris
    chris over 13 years
    I got those already. Eclipse will automatically complain when I'm missing those schemas
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    Thought so, but I was just checking, for completeness.
  • chris
    chris over 13 years
    Based on observation, I don't think so. I have a web-app where the <mvc:annotation-driven /> is in the applicationContext.xml. In fact, if I put in the xxxxx-servlet, I just get SEVERE CONTEXT error. I need that element because my Controllers and Servicers are marked using the annotations @Controller and @Service respectively
  • chris
    chris over 13 years
    In my web.xml I only have the xxxxx-servlet.xml. I didn't declare any other Spring XML files. The applicationContext.xml is loaded by default without any special config. I don't really know why it works on some apps based on their location
  • chris
    chris over 13 years
    I'm digesting this info and currently testing...I think I'm into something
  • Michel
    Michel over 13 years
    Ok great, I'm glad that this info is helping you.
  • Bozho
    Bozho over 13 years
    @chris how about sharing the exact message and trace of the serve exception?