Re: [jug-discussion] grab a Spring configured bean - part Deux
a ds bean... can Spring just do MyServletFilter.setDataSource(myDataSourceBean) at startup? Unless Spring instantiates the bean, the answer is no. You can ask spring to configure a bean programmatically: http://opensource.atlassian.com/projects/spring/browse/SPR-266 relevant bit: I've just added an applyBeanPropertyValues(Object existingBean, String name) method to the AutowireCapableBeanFactory interface, analogous to the existing autowireBeanProperties method. It simply applies the property values from the bean definition with the given name to an existing bean instance, which should work nicely for Tapestry Page objects. but that doesn't really solve the problem of the dependency. You could use an aspect to pass your filter to Spring, thus limiting the dependency to that aspect. I'm considering doing this for Tapestry pages. Cheers, nick On Sep 23, 2004, at 5:06 PM, Tim Colson wrote: 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] 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] grab a Spring configured bean - part Deux
a ds bean... can Spring just do MyServletFilter.setDataSource(myDataSourceBean) at startup? Nick said: Unless Spring instantiates the bean, the answer is no. You can ask spring to configure a bean programmatically: I'm not so sure that's the case for static stuff. In fact, I kind of surprised myself -- my config of my Configuration bean already looks like it does what I'm asking about doingsorta. :-) I don't believe this trick will work for EventLog objects, but have a look at this static setup magic: bean id=FOOappConfiguration class=common.Configuration init-method =start property name=myConfigurationFiles value/resources/application.properties,/resources/sql.properties/value /property /bean Relevant bits of Configuration.java private static String theConfigFiles = ; private static Properties theProperties = null; /** * This is a utility class and is never instantiated. */ private Configuration() {} /** * Tells the class which property file(s) to read. * @param filenames */ public void setMyConfigurationFiles(String filenames) { theConfigFiles = filenames; } /** * Starts up the class, reads in the config file(s). */ public static void start() { // load properties Properties props = new Properties(); InputStream in = null; logger.info(Configuration init - + theConfigFiles); // allow for comma separated list of config files StringTokenizer tokenizer = new StringTokenizer(theConfigFiles, ,); while (tokenizer.hasMoreTokens()) { String currentFile = tokenizer.nextToken(); logger.info(loading config file: + currentFile); try { in = Configuration.class.getResourceAsStream(currentFile); if (null != in) { props.load(in); logger.info(currentFile + - loaded.); } else { logger.error(currentFile + - NOT FOUND); } } catch (Exception e) { logger.error(Unable to load + currentFile + , the error is: + e); } finally { try { in.close(); } catch (Exception ignored) { } } } theProperties = props; } -- During startup -- INFO [main] [2004-09-24 14:18:26,607] [org.springframework.beans.factory.support.DefaultListableBeanFactory]- Creating shared instance of singleton bean 'FOOappConfiguration' INFO [main] [2004-09-24 14:18:26,617] [common.Configuration]- Configuration init - /resources/application.properties,/resources/sql.properties INFO [main] [2004-09-24 14:18:26,617] [common.Configuration]- loading config file:/resources/application.properties INFO [main] [2004-09-24 14:18:26,617] [common.Configuration]- /resources/application.properties - loaded. INFO [main] [2004-09-24 14:18:26,617] [common.Configuration]- loading config file:/resources/sql.properties INFO [main] [2004-09-24 14:18:26,617] [common.Configuration]- /resources/sql.properties - loaded. And then later when I use Configuration.getProperty(test) - I get the expected value. So it seems Spring configured a Configuration class instance, byproduct is initialized static info... so I get a config'd Configuration Utility class, which is not dependant on Spring. Cool. :-) (This may not be the best design... but hey, it makes me happy today. :-) Cheers, Timo - 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]