Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring MVC: same class different bean

 
Jason Nesbitt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same class that I'm configuring as two different beans because the properties set need to be different for each. In turn, I have different urls mapped to each bean. Unfortunately, Spring throws an exception that says the following.

"Cannot map handler [mediaCoverageController] to URL path [/news*]: There is already handler [org.mycompany.web.NewsController@19318fa] mapped."

Anybody know what I'm doing wrong?

Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, besides the obvious of what is written in the message.

How are you doing mapping, xml or Annotations? Either way, can you post your mappings and bean declarations so we can see where the conflict is happening?

Thanks

Mark
 
Jason Nesbitt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not obvious because the message is misleading. See mappings and declarations below.

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well based on your mappings it is using the default BeanName Mapping Handler class to map news* to your NewsController. But I don't see in your mappings where you are mapping /news* to your MediaCoverageController. So without seeing that anywhere, I would guess that news is the context of your web application. What is your war file name, is it news.war? What about the servlet mapping in your web.xml, what is that?

Mark

I am thinking something is in the air this week.
 
Jason Nesbitt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not mapping to /news* anywhere. The context of the application is ROOT (/) and the war file is not named news.war. The mapping strategy I'm using is org.springframework.web.servlet.handler.SimpleUrlHandlerMapping. Based on what you said I'm guessing that it's trying to infer mapping urls from the class names. I'm guessing the confusion then arises because two beans have the same class. I didn't know that this handler behaved like this. Is there one that I can use which doesn't infer any mapping urls?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Nesbitt:
I'm not mapping to /news* anywhere. The context of the application is ROOT (/) and the war file is not named news.war. The mapping strategy I'm using is org.springframework.web.servlet.handler.SimpleUrlHandlerMapping. Based on what you said I'm guessing that it's trying to infer mapping urls from the class names. I'm guessing the confusion then arises because two beans have the same class. I didn't know that this handler behaved like this. Is there one that I can use which doesn't infer any mapping urls?


Not that particular Handler, but there is a BeanName handler that is a default handler given to you, that if it can't find the URL mapping via the other Handlers, then it will use it.

From the Spring JavaDocs for the DispatcherServlet

It can use any HandlerMapping implementation - pre-built or provided as part of an application - to control the routing of requests to handler objects. Default is BeanNameUrlHandlerMapping, as well as a DefaultAnnotationHandlerMapping when running on Java 5+. HandlerMapping objects can be defined as beans in the servlet's application context, implementing the HandlerMapping interface, overriding the default HandlerMapping if present. HandlerMappings can be given any bean name (they are tested by type).


http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/servlet/DispatcherServlet.html

So I agree with you about the conflict. So then the question is, do you need to have two bean definitions for the same controller?

Mark
 
Jason Nesbitt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark

Thanks for the info and helpful references.

So I agree with you about the conflict. So then the question is, do you need to have two bean definitions for the same controller?


To sidestep this problem, I did end up just creating different controllers for each bean. However, I can think of a lot of scenarios where you would want to use the same controller class, only configured differently, in more than one bean definition.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can use the "name" attribute instead of the "id" and give the controller two different names.

list

<bean name="firstName, secondName">

but that would just be one instance of that bean created, whereas in your scenario you have two different instances with different names.

There is a way to turn off the default Handlers, but it is a little convoluted, and I don't quite understand it myself, but in the web.xml there is a property you can set to false for the DispatcherServlet, then there will be no default Handlers, but I am not sure if that will also end up skipping the the Handler you declared in your xml. But if you look at the DispatcherServlet Javadocs it has that propery there.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic