File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Spring: applicationContext.xml and other XML files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring: applicationContext.xml and other XML files" Watch "Spring: applicationContext.xml and other XML files" New topic
Author

Spring: applicationContext.xml and other XML files

K Aditi
Ranch Hand

Joined: Mar 17, 2008
Posts: 89
I am new to Spring and I am kind of intimidated by all the xml files required.I learning SpringMVC and had few questions,

1. Why would I require <<appname>>-servlet.xml, <<appname>>-service.xml, and so on if I can configure it in applicationContext.xml?

2. Is applicationContext.xml necessary? Isn't <<appname>>-servlet.xml enough?

3. Do I have to do everyting in Spring using beans?

By the way, I am using Spring 2.x.

Thanks,


Aditi
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

No you don't

First there are Annotation alternatives that will require very minimum in the xml.

For the xml, you do want to split them up into separate files so that you have a clear separation of concerns even in xml.

For instance the applicationContext.xml is only for you Service/Repository layers. You would probably want to import an infrastructure-config.xml for things like Datasources and TransactionManagers. Then if you are using AOP you might want to create and import an aspect-config.xml. This is so that it is easy to find anything you might want to change. Want to change a Pointcut, well go to aspect-config.xml. Want to change a Serviceconfig, well applicationContext.xml

As far as the SpringMVC stuff. The xxx-servlet.xml is where you define your Controllers, and View resolvers, and if for MappingHandlers (if you don't want to use the default). That is all.

So with annotations I can have two lines in my applicationContext.xml that points to the package that holds the Service and Repository implementation classes with Annotations. In the xxx-servlet.xml I can put the scan for the package that has the Controller classes, and one entry for a Resolver. Real small and simple

Also the difference between the applicationContext.xml and the xxx-servlet.xml is huge. a separation of concerns. The applicationContext can be re-used by many Servlets / web apps. So you can have your application layers available to a Spring MVC app and also to Spring web services without having to duplicate it all. Also when those two xml files really get loaded are at different times and placed in different locations when in a Web environment.

Mark


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

Joined: Mar 17, 2008
Posts: 89
Thanks for the reply!
Well, I couldn't really understand thr following point.Could you give me an example?
The applicationContext can be re-used by many Servlets / web apps. So you can have your application layers available to a Spring MVC app and also to Spring web services without having to duplicate it all.


Thanks,
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Simple, all you would need to do is have two <servlet> tags one for the DispatcherServlet and one for the Web Service Servlet. But you still only have one ContextLoaderListener to load the application context.



Basic layered architecture of a Spring app with both a Web Layer and a Web Service, but only one Application Context. So you want both Web stuff to share the one Application Context rather than creating two of them.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring: applicationContext.xml and other XML files