Spring Application Context Load Order

In my web.xml, I have the servlet declaration "springmvc" (which has the 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 is loading first? Springmvc-servlet.xml or applicationContext.xml?

I ask for this when I put the <mvc:annotation-driven/> element in applicationContext.xml, I get a serious context message. But when I put this element in springmvc-servlet.xml, my web application is working fine.

Any ideas why?

In another web application, I have <mvc:annotation-driven/> inside applicationContext.xml and it works fine.

Appendix: I noticed that having aop: config creates a conflict with mvc: with annotation

+6
java spring spring-mvc
source share
4 answers

The applicationContext.xml context is the parent of the dispatcher-servlet.xml . I donโ€™t know if this means it is loading first, but in your case it doesnโ€™t matter:

<mvc:annotation-driven /> must be in dispatcher-servlet.xml because it belongs to the web part of the application.

+5
source share

I solved my problem!

Turns out this has nothing to do with the boot order or the <mvc:annotation-driven/> declaration.

I tried deploying my web application to another Tomcat, and, to my surprise, there is a stack trace in the localhost log. I had a hint of trial and error that conflict with <aop:config/> . But what is the specific 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 an exception. I searched for that exact error above and found this: Spring 3: adding reasons to ClassCastException

A thread starter appears and I have the exact same problem. So I downloaded aspectj-1.6.10.jar, but I was still missing from the class. Then it turns out that it should be aspectjweaver-1.6.9

I still used a very old gambling file. He had no version in his name. The problem is solved. Case is closed.

By the way, as a bonus, I manually deployed 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 are exactly the same when you declare <mvc:annotation-driven/> based on what I explored.

Thanks to everyone who helped me.

+3
source share

With the exception of web.xml, there is no predefined order. It happens:

  • web.xml is loaded by the servlet engine, this causes the loading of all defined servlets, filters, listeners,
  • ContextLoaderListener loads the context of the XML root application, this may include a bean definition for LocalSessionFactoryBean, start loading the entire display of Hibernate XML files
  • DispatcherServlet loads web pages XML application context

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

see also:

link

0
source share

You probably need 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 are shared)

0
source share

All Articles