This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conflict with <mvc:annotation-driven/>

 
Rakesh Basani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rakesh Basani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh,

I would like to see your web.xml too just to see how you have these contexts scoped.
 
Rakesh Basani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very guys.. problem resolved..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic