Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 203
chris Avatar asked Nov 22 '10 08:11

chris


2 Answers

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.

like image 163
Bozho Avatar answered Nov 15 '22 06:11

Bozho


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.

like image 42
chris Avatar answered Nov 15 '22 06:11

chris