tag:blogger.com,1999:blog-3237724005744642470.post7583586046982358111..comments2016-05-11T07:17:43.335+01:00Comments on Captain Debug's Blog: Integrating Spring Into Legacy ApplicationsRoger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]Blogger8125tag:blogger.com,1999:blog-3237724005744642470.post-26028944085108822842012-06-22T13:47:02.005+01:002012-06-22T13:47:02.005+01:00Currently, my situation is the one you described a...Currently, my situation is the one you described as "a very small app that has only one servlet." that's why i thought of doing it the simplest way possible.<br />however, i have redesigned the thing to use the ContextListener. it makes sense, since the app is likely to grow, to implement things the "right" and scalable way from the beginning.<br />thanks again for your help, not only on this topic, but on the other topics you broach in your blogs and from which there is a lot to learn.jlemm[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-90956148658763176442012-06-22T10:11:28.376+01:002012-06-22T10:11:28.376+01:00jlemm
You're not missing much by using the sol...jlemm<br />You're not missing much by using the solution you describe above. Using a ContextListener is just the (preferred?) way to load application wide resources. It's good because it enforces the Single Responsibility Principle (SRP) removing the responsibility of creating a Spring ApplicartionContext from the Servlet into its own class. It also ensures that resources are loaded at the correct point in the web server's lifecycle. However, being pragmatic I guess that whether or not you use this approach depends upon the size of your app. <br /><br />If you have a large app consisting of several servlets, loads of classes that serve hundreds of pages all of which require access to the Spring Context, then using a ContextListener would be a really good idea. If, on the other hand, you have a very small app that has only one servlet, then you could argue that the ContextListener could overly complicate things.Roger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-53303966440652714552012-06-21T18:30:51.555+01:002012-06-21T18:30:51.555+01:00thanks so much for the detailed reply.
I must be ...thanks so much for the detailed reply.<br /><br />I must be missing something, but it looks like i got it to work without the ContextListener.<br /><br />public class MyApplicationContext {<br /><br /> private final static ApplicationContext context = new ClassPathXmlApplicationContext("/spring-beans.xml");<br /><br /> public static Object getSpringBean(String bean) {<br /> return context.getBean(bean);<br /> }<br />}<br /><br />then in the serlvet i do:<br /><br />Book book = (Book) MyApplicationContext.getSpringBean("bookBean");<br /><br />what am i missing by doing it this way?jlemm[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-49886488147973409892012-06-20T15:52:58.711+01:002012-06-20T15:52:58.711+01:00Jlemm,
To answer your question, it does work with...Jlemm, <br />To answer your question, it does work without switching your app to: org.springframework.web.servlet.DispatcherServlet as this kind of legacy app doesn’t use Spring MVC. I approached this problem by writing a class that implements<br /><br />javax.servlet.ServletContextListener <br /><br />for example: class com.MyContextListener implements ServletContextListener<br /><br />Implement the contextInitialized(ServletContextEvent sce) method adding the Spring initialisation code from the ‘loadExternalClassTest2’ method above so that you create and load your Spring application context. To ensure that your Spring ApplicationContext is available throughout your app, simply be pragmatic and make it a static variable accessed via a static method, something like:<br /><br />public static ApplicationContext MyContextListener.getContext();<br /><br />That way, you'll be able to write code that goes something like:<br /><br />Book book = (Book )MyContextListener.getContext().getBean("bookBean");<br /><br />To ensure that your listener class is created add the following to your web.xml file in the appropriate place:<br /><br /><listener><br /> <listener-class>com.myContextListener</listener-class> <br /></listener> <br /><br />(It generally goes between the and the elements…)<br /><br />...when the server starts, it creates an instance of MyContextListener and calls contextInitialized(...)<br /><br />Hope this helps. In retrospect, perhaps I should have included this in my original blog.Roger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-8373790643157370312012-06-19T14:58:17.266+01:002012-06-19T14:58:17.266+01:00i realize now that what i want to do is actually t...i realize now that what i want to do is actually the opposite of what you outline here: you compose a spring bean with a legacy object; i want to inject a spring bean into legacy code.<br />say i have this in a servlet's doPost method:<br /><br /> PrintWriter out = response.getWriter();<br /> Book book = new Book();<br /> book.setAuthor("David Foster Wallace");<br /> book.setTitle("Infinite Jest");<br /> out.println("Title is: " + book.getTitle());<br /> out.println("Author is: " + book.getAuthor());<br /><br />now i want to let spring create the book object, something like:<br /><br />Book book = (Book ) context.getBean("bookBean");<br /><br />where bookBean is wired in the xml file.<br />the trick is getting the context and so far i have not been able to get it to work in a servlet environment.jlemm[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-9843970730419102982012-06-19T14:40:17.108+01:002012-06-19T14:40:17.108+01:00fowler's article on the "strangler patter...fowler's article on the "strangler pattern" is fascinating, i feel inspired by it, as we're dealing with a similar issue.<br />which brings up this question: in a legacy servlet-based application, would the stuff in your article work, without having to switch it to org.springframework.web.servlet.DispatcherServlet and Spring MVC? what would web.xml look like?jlemm[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-91771955025473730512012-06-16T09:24:05.693+01:002012-06-16T09:24:05.693+01:00Thanks for the comment: typo corrected...Thanks for the comment: typo corrected...Roger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-76662623578511752602012-06-15T18:02:11.403+01:002012-06-15T18:02:11.403+01:00good post, thanks.
("pragmatically adding my ...good post, thanks.<br />("pragmatically adding my legacy class instance to it" - you mean "programmatically", right?)jlemm[email protected]