RE: [jug-discussion] How to grab a Spring configured bean?
So I've found some info on getting the Spring Context inside a generic servlet. ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext); But where I need to get the context is inside a class that is a Servlet Filter... which does not have access to a servlet context (presumably because filters can act cross contexts?) So I'm still stuck. :-( Timo -Original Message- From: Tim Colson [mailto:[EMAIL PROTECTED] Sent: Thursday, September 23, 2004 1:32 PM To: [EMAIL PROTECTED] Subject: [jug-discussion] How to grab a Spring configured bean? Hey folks - I'm trying to understand and use Spring, falling down a bit. I have a ds bean in Spring config (DataSource), and a Struts action which extends DispatchActionSupport. It is easy to grab the bean using: getWebApplicationContext().getBean(ds); Cool but now I am in another class which is not an action -- and I'm at a loss how to grab the ds bean. :-( I'm reading the 200 pages of docs ...but if anybody could point me to the relevant bit to jump-start me, that'd be great! Timo - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [jug-discussion] How to grab a Spring configured bean?
Rambling away...slowing getting closer... But where I need to get the context is inside a class that is a Servlet Filter... which does not have access to a servlet context (presumably because filters can act cross contexts?) Wrong. In the Filter Init, there is a FilterConfig object which can get to the ServletContext... So I'm going to give that a tumble. :-) Cheers, Timo - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [jug-discussion] How to grab a Spring configured bean?
In the init() method you can get a reference to the ServletContext from the FilterConfig object... then you can either use it then or store it in an instance variable of the filter. If you filter is acting across more than one context this is not a good idea; howerver, I don't think they can... am I wrong? The filter has always seemed a bit lacking to me. Hope this helps. Chris [EMAIL PROTECTED] -Original Message- From: Tim Colson [EMAIL PROTECTED] Sent: Sep 23, 2004 2:14 PM To: [EMAIL PROTECTED] Subject: RE: [jug-discussion] How to grab a Spring configured bean? So I've found some info on getting the Spring Context inside a generic servlet. ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext); But where I need to get the context is inside a class that is a Servlet Filter... which does not have access to a servlet context (presumably because filters can act cross contexts?) So I'm still stuck. :-( Timo -Original Message- From: Tim Colson [mailto:[EMAIL PROTECTED] Sent: Thursday, September 23, 2004 1:32 PM To: [EMAIL PROTECTED] Subject: [jug-discussion] How to grab a Spring configured bean? Hey folks - I'm trying to understand and use Spring, falling down a bit. I have a ds bean in Spring config (DataSource), and a Struts action which extends DispatchActionSupport. It is easy to grab the bean using: getWebApplicationContext().getBean(ds); Cool but now I am in another class which is not an action -- and I'm at a loss how to grab the ds bean. :-( I'm reading the 200 pages of docs ...but if anybody could point me to the relevant bit to jump-start me, that'd be great! Timo - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Christopher J. Stehno [EMAIL PROTECTED] www.stehno.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [jug-discussion] How to grab a Spring configured bean?
Cast the request to an http servlet request. That should have access to the session, which has access to the context.(I think). Good luck. --nick On Sep 23, 2004, at 2:14 PM, Tim Colson wrote: So I've found some info on getting the Spring Context inside a generic servlet. ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext); But where I need to get the context is inside a class that is a Servlet Filter... which does not have access to a servlet context (presumably because filters can act cross contexts?) So I'm still stuck. :-( Timo -Original Message- From: Tim Colson [mailto:[EMAIL PROTECTED] Sent: Thursday, September 23, 2004 1:32 PM To: [EMAIL PROTECTED] Subject: [jug-discussion] How to grab a Spring configured bean? Hey folks - I'm trying to understand and use Spring, falling down a bit. I have a ds bean in Spring config (DataSource), and a Struts action which extends DispatchActionSupport. It is easy to grab the bean using: getWebApplicationContext().getBean(ds); Cool but now I am in another class which is not an action -- and I'm at a loss how to grab the ds bean. :-( I'm reading the 200 pages of docs ...but if anybody could point me to the relevant bit to jump-start me, that'd be great! Timo - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Nicholas Lesiecki Software Craftsman, specializing in J2EE, Agile Methods, and aspect-oriented programming Books: * Mastering AspectJ: http://tinyurl.com/66vf * Java Tools for Extreme Programming: http://tinyurl.com/66vt Articles on AspectJ: * http://tinyurl.com/66vu and http://tinyurl.com/66vv - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [jug-discussion] How to grab a Spring configured bean?
Timo, I'm not a spring expert, so I set out to the trusty google... following article looked fairly promising... http://www.devx.com/Java/Article/21665 Had some examples of using XmlBeanFactory to get at the info (apparently not really recommended, though), discussion of setter/constructor dependency injection, etc. Page 3 of the article looked especially promising. Robert Tim Colson wrote: So I've found some info on getting the Spring Context inside a generic servlet. ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext); But where I need to get the context is inside a class that is a Servlet Filter... which does not have access to a servlet context (presumably because filters can act cross contexts?) So I'm still stuck. :-( Timo -Original Message- From: Tim Colson [mailto:[EMAIL PROTECTED] Sent: Thursday, September 23, 2004 1:32 PM To: [EMAIL PROTECTED] Subject: [jug-discussion] How to grab a Spring configured bean? Hey folks - I'm trying to understand and use Spring, falling down a bit. I have a ds bean in Spring config (DataSource), and a Struts action which extends DispatchActionSupport. It is easy to grab the bean using: getWebApplicationContext().getBean(ds); Cool but now I am in another class which is not an action -- and I'm at a loss how to grab the ds bean. :-( I'm reading the 200 pages of docs ...but if anybody could point me to the relevant bit to jump-start me, that'd be great! Timo - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [jug-discussion] How to grab a Spring configured bean?
Hi Tim, We had the same problem. I have come up with a solution, but it may be a bit of a hack. If anyone else has a better solution, I would love to hear it. My solution involves the use of a singleton, initialized by a servlet that loads at application start up. I then use a utility to simplify the access to the singleton. I have included the source to the pieces. My spring configuration in web.xml is as follows (the last item calls the servlet that initializes the singleton): !-- spring configuration -- context-param param-namecontextConfigLocation/param-name param-value/WEB-INF/applicationContext.xml/param-value /context-param listener listener-classorg.springframework.web.context.ContextLoaderListener/liste ner-class /listener servlet servlet-nameContextServlet/servlet-name servlet-classeblox.commons.spring.ContextServlet/servlet-class load-on-startup1/load-on-startup /servlet -- Once this is all done, getting at a spring managed bean can be done anywhere and is as easy as: SpringUtils.getBean(myBean); Hope this helps, Andy On 9/23/04 2:14 PM, Tim Colson [EMAIL PROTECTED] wrote: So I've found some info on getting the Spring Context inside a generic servlet. ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext); But where I need to get the context is inside a class that is a Servlet Filter... which does not have access to a servlet context (presumably because filters can act cross contexts?) So I'm still stuck. :-( Timo -Original Message- From: Tim Colson [mailto:[EMAIL PROTECTED] Sent: Thursday, September 23, 2004 1:32 PM To: [EMAIL PROTECTED] Subject: [jug-discussion] How to grab a Spring configured bean? Hey folks - I'm trying to understand and use Spring, falling down a bit. I have a ds bean in Spring config (DataSource), and a Struts action which extends DispatchActionSupport. It is easy to grab the bean using: getWebApplicationContext().getBean(ds); Cool but now I am in another class which is not an action -- and I'm at a loss how to grab the ds bean. :-( I'm reading the 200 pages of docs ...but if anybody could point me to the relevant bit to jump-start me, that'd be great! Timo - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Andrew Barton eBlox, Inc. 520.903.2541 Check out the new Free Distributor Resource Center (including an online directory of more than 1200 Suppliers)! http://resourcecenter.promoorder.com Instantly source more than 10,000 products online with Product Buffet. It's Free to Distributors and Suppliers! http://productbuffet.eblox.com SpringUtils.java Description: Binary data ServletContextSingleton.java Description: Binary data ContextServlet.java Description: Binary data - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [jug-discussion] How to grab a Spring configured bean?
Andrew Barton wrote: We had the same problem. I have come up with a solution, but it may be a bit of a hack. If anyone else has a better solution, I would love to hear it. My solution involves the use of a singleton, initialized by a servlet that loads at application start up. I then use a utility to simplify the access to the singleton. I have included the source to the pieces. My spring configuration in web.xml is as follows (the last item calls the servlet that initializes the singleton): !-- spring configuration -- context-param param-namecontextConfigLocation/param-name param-value/WEB-INF/applicationContext.xml/param-value /context-param listener listener-classorg.springframework.web.context.ContextLoaderListener/liste ner-class /listener servlet servlet-nameContextServlet/servlet-name servlet-classeblox.commons.spring.ContextServlet/servlet-class load-on-startup1/load-on-startup /servlet -- Once this is all done, getting at a spring managed bean can be done anywhere and is as easy as: SpringUtils.getBean(myBean); The WebApplicationContext is the way that I go. I find that you can get to the ServletContext wherever it matters (even in the Filter, as you no doubt found out). I split up applicationContexts into many different ones for discrete purposes (like modelled objects in hibernate, DAOs, Services, application-specific) and then load them sequentially (so that you get a root context that has access to all of the chained contexts): web.xml: !-- Spring application context locations -- context-param param-namecontextConfigLocation/param-name param-value classpath:/org/ognl/model/applicationContext-model.xml, classpath:/org/ognl/dao/applicationContext-dao.xml, classpath:/org/ognl/service/applicationContext-service.xml, /WEB-INF/applicationContext.xml /param-value /context-param !-- Listeners -- !-- Spring application context loader; loads when context is initialized -- listener listener-classorg.springframework.web.context.ContextLoaderListener/listener-class /listener This makes it much more configurable, especially since I learned the classpath: prefix trick for the config locations. Of course you can use WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()) [whew!] to get the ApplicationContext for the entire application. Just a tip. Use it together. Use it in peace. - Drew -- +-+ Drew Davidson | OGNL Technology +-+ | Email: [EMAIL PROTECTED] / |Web: http://www.ognl.org / |Vox: (520) 531-1966 |Fax: (520) 531-1965\ | Mobile: (520) 405-2967 \ +-+ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [jug-discussion] grab a Spring configured bean - part Deux
Hey gang - I managed to hack the Filter to get a ServletContext and then the Spring WebApplicationContext. Thanks to all for the many helpful replies. Nick wrote: Cast the request to an http servlet request. That should have access to the session, which has access to the context.(I think). Correct. That works too, but I stuck with the init() hook because it just seemed like something I'd only need to do once. But here is part two... I now have a Servlet Filter that is coupled to Spring! That seems like a bad thing... I mean, if the whole point of Spring is to allow you to inject different implementations/configurations, shouldn't one strive to be able to replace Spring with another container in the future? So the question now becomes a bit more esoteric (since I have a soln. that works and that gives me a little breathing room)...and perhaps this is obvious to Spring folks, but not to me yet. Can I setup a spring config that injects a DataSource into MyServletFilter class? So rather than have MyServletFilter obtain a Spring context and then ask for a ds bean... can Spring just do MyServletFilter.setDataSource(myDataSourceBean) at startup? Timo - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [jug-discussion] Java core files?
At 06:29 AM 9/20/2004 -0700, Drew Davidson wrote: I think about the closest you are going to get on a Java VM is the old ctrl-break trick (on Windows) to generate a thread dump. Not exactly a core file, but then again what could you do with a Java core file anyway? There aren't any debuggers that could work with them (unless you just want to dump the state of the VM to disk and restart it later). Thanks for that note. I don't have a need at the moment but I got to thinking about how core files can be useful in some cases and that made me wonder if a similar capability of any sort had been developed for Java. A cooperative debugger would be part of the picture, of course. Today I got to wondering if any shells or (even better) operating systems have addressed the problem of treating class files or jars as first-class executables... :) |-| | William H. Mitchell | | Mitchell Software Engineering | | Consulting/Development/Training | | | | [EMAIL PROTECTED]520-577-6431 | |-| - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]