Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Conventions in Spring

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
These days i have started learning Spring ...i would like to know about the convention support in Spring ...for example ...suppose i have the context servlet "myApp-servlet.xml" which is the core file of Spring where we defined all the urlMappings, Dependency Injection and other stuff..
For large applications we need to define many url Mappings since there are many controllers ..
Question is ..is there any mechanism (any Convention support) through which i can get rid of this huge url mapping tag ...i need to just write the controller and method name in the url and want to process it ...
any idea or alternatives ............
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mudassir,


A very good practice is to break your application context across multiple xml files. In my opinion using a unique xml file based on the servlet-name is a very poor designing decision. Especially for larger applications, maintaining a huge monolithic configuration file is only going to be a pain in the neck.

Having said that the approach I�m following is to have multiple spring-*.xml files, like spring-resources.xml, spring-hibernate.xml, etc and to have the contextConfigLocation context parameter helping with loading the beans:

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mudassir

Valentin is correct about splitting up your files, but dont go mad and have lots of xml files, it is as bad as having one big one.
To follow on there are some other ways of splitting your files,
If you are using spring mvc then in your web.xml you need to do what Valentin said to let the controling servlet know about all your xml files.


You can also use a form of xml inheritance.

<import resource="myOtherXmlFile.xml"/>

Hope this helps,
Shawn
http://www.whatjar.co.uk
 
author
Posts: 396
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by mudassir shahab:
For large applications we need to define many url Mappings since there are many controllers .. Question is ..is there any mechanism (any Convention support) through which i can get rid of this huge url mapping tag



I'm assuming that you're concerned about how lengthy a SimpleUrlHandlerMapping can get in a large application with multiple controllers...right?

If that's your concern, then you should have a look at BeanNameUrlHandlerMapping or ControllerClassNameHandlerMapping.

BeanNameUrlHandlerMapping has been part of Spring since early on and uses the bean's name as the URL mapping. For example:

<bean name="/createOrder.htm" class="com.foo.bar.CreateOrderController" />

Notice that you should use the name attribute to name your bean instead of id, as the id attribute has special meaning in XML and won't allow certain characters--the name attribute is roughly equivalent to id, but doesn't have the same hang-ups with special characters.

ControllerClassNameHandlerMapping is new to Spring 2. It assumes a convention where the controller's class name is used as the URL mapping. For example, the following controller bean...

<bean class="com.foo.bar.CreateOrderController" />

...will be mapped to "/createorder*".
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!