wood burning stoves 2.0*
The moose likes Spring and the fly likes Conflict with <mvc:annotation-driven/> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Conflict with <mvc:annotation-driven/>" Watch "Conflict with <mvc:annotation-driven/>" New topic
Author

Conflict with <mvc:annotation-driven/>

Rakesh Basani
Ranch Hand

Joined: Jun 04, 2012
Posts: 38
Hi friends..

I am working on a spring mvc project.. When I place <mvc:annotation-driven/> tag in dipatcherservlet-servlet.xml then getting trouble in accessing page (Unable to access 404 status code).. dipatcherservlet-servlet.xml contains other bean configurations like InternalResouceViewResolver..

When I place following 3 tags in separate xml file then everything going excellent..

<context:annotation-config />
<context:component-scan base-package="com.ggk.common.controller" />
<mvc:annotation-driven />


Could anyone explain me what is exactly going on underthehood..


Thanks in advance
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

You did not post quite enough to answer for sure. I guess I would start with the bit at the top and make sure your namespaces are configured properly, should look like below




[How To Ask Questions][Read before you PM me]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Also note you have redundency with

<context:annotation-config />
<context:component-scan base-package="com.ggk.common.controller" />

context:component-scan automatically includes context:annotation-config

So you can remove the context:annotation-config

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rakesh Basani
Ranch Hand

Joined: Jun 04, 2012
Posts: 38
Thanks for your replies..

The following file is not working.. Its unable to identify @RequestMappings.. getting 404 error..

mvc-dipatcher-servlet.xml


But when I divide the above file into following two files then everything going excellent..
applicationContext.xml

mvc-dipatcher-servlet.xml
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Rakesh,

I would like to see your web.xml too just to see how you have these contexts scoped.
Rakesh Basani
Ranch Hand

Joined: Jun 04, 2012
Posts: 38
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

OK, first your file is probably not called

mvc-dipatcher-servlet.xml

but probably

mvc-dispatcher-servlet.xml

Also, you put your mvc-dispatcher-servlet.xml in the list of config files for the ContextLoaderListener, which is used to make an ApplicationContext for your middle tier beans, there shouldn't be any web tier beans in those files. The dispatcher servlet xml should be used by the Dispatcher Servlet for it to create a second ApplicationContext that only stores your Web Tier beans.

So in your mvc-dispatcher-servlet.xml it should have these tags only



In your second config file you can have benas for your Services, Repositories and other middle tier beans like DataSource, TransactionManager.

I think in the end the reason why your first didn't work was because you might have had a typo in the xml file name.

Mark
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

I would just like to further explain what Mark has already said just to make sure you understand.

When you do something like this



it is functionally equivalent to the following



What this means is you are running into one of those convention over configuration situations. Now what that means for your set up is you are with what you have posted creating the beans defined in mvc-dispatcher-servlet.xml in the root application context and then overriding them in the servlet context with the exact same thing.

Best practice is to do what Mark said and remove mvc-dispatcher-servlet.xml from your context-params as all of these beans go into your root context. This way you will have your web beans correctly scoped and only available at the dispatcher servlet.

Now if you were to want to keep all of your beans in just the root context (I am not advising this here) then you would get rid of mvc-dispatcher-servlet.xml as the name is misleading and put those beans in your application-context.xml. Then you would define the dispatcher servlet like this



This would in essence tell spring don't do your convention over configuration stuff I don't want a servlet scoped context I am going to put everything in the root context.
Rakesh Basani
Ranch Hand

Joined: Jun 04, 2012
Posts: 38
Thank you very guys.. problem resolved..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conflict with <mvc:annotation-driven/>
 
Similar Threads
Cannot get basic Spring validation to work. Why?
"Request method 'GET' not supported" jsp viewResolver JstlView
<mvc:annotation-driven/> Vs <context:annotation-config/>
Spring/Hibernate - No Hibernate Session bound to thread, and configuration does not allow creation..
AOP Not working with Spring 3 MVC application