aspose file tools*
The moose likes Spring and the fly likes Integrating Spring in an existing servlets project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Integrating Spring in an existing servlets project" Watch "Integrating Spring in an existing servlets project" New topic
Author

Integrating Spring in an existing servlets project

Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
Hello,

I have a servlet & jsp project which i need to extend with new functionality. Is it possible to integrate spring in this project just for the new functionality without affecting the other code?

Thanks
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
What Spring features do you want to use?

If you use just Servlets/JSPs, you will not fully access to power of dependency injection.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
I want to use the dependency injection (this is out it seems) and spring mvc. I don't know how splitting the spring controllers definitions will impact the servlets' definitions that are already present in web.xml. I mean, for example, can I still use multiple deployment descriptors? Or what can I use?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Ok, so you want to use Spring Web MVC, right?
In that case, there is nothing to worry, you can use DI to Spring MVC controllers, this is not related to existing Servlets. There is no any impact to existing Servlets either.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

First you need to bootstrap your ApplicationContext into your web application. This is necessary for Servlets or Spring MVC

In your web.xml you add the ContextLoaderListener and point to your application context xml.

You then can have your Servlet access an ApplicationContext

in your Servlet you would call

ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

That is all that is needed, now you can lookup your beans from your Servlet using context.getBean();

Most of the time the call to get the App context and your lookup are done in the init method of your Servlet.

Thanks is the "Dependency Injection" of a Spring class into your Servlet

Mark


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

Joined: May 07, 2008
Posts: 126
I'm not planning to use servlets for the code that i'm supposed to implement, but spring controllers. I need to inject beans only in the controllers that i develop. So, is this setup still necesary?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You just need to specify ContextLoaderListener in web.xml, it'll instantiate IoC container using applicationContext.xml.

If you don't want to use Servlets for new features, you wouldn't call the following:


context.getBean is dependency lookup.
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
You mean that only these lines i need to add in web.xml



and i can start using spring without any impact on the other code?
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
Ok, i have this error:



These are the first 4 lines in servlets.xml:


It seems the doctype (which works in tomcat btw) doesn't work on jboss. Is there any workaround for this?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Try the updated Spring 2.x DTD -



or the xmlns version -



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
No luck. Is there something else i must do besides having spring.jar in my buildpath and declaring the doctype/xmlns in xml?
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
The issue seems to be firewall related. www.spring.... cannot be reached. Is there anyway i can validate the dtd's locally? I tried and i've put the dtd in my application's root, but the issue is still there.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Nick Potter wrote:



Change to the following:

If still have problem, try to debug ContextLoaderListener.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you need to access the DTD locally it should be -


and the DTD files should go in the /WEB-INF directory.
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
I didn't use the SYSTEM... but i found a way for people who have this problem too. So to access the dtd locally, one must have it in workspace (i didn't put in the WEB-INF, i think it can be anywhere on the hard-drive, but it's probably bad) and have an XML Catalog validate it. For eclipse users it's easy, since it has a built-in XML Catalog in windows - preferences - xml. Should be pretty easy to use.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integrating Spring in an existing servlets project
 
Similar Threads
Is anybody tell me good book for Spring framework
Spring IDE setup problem
Spring MVC over existing Sturts project
Wont let me import ApplicationContext
spring modules integration