PDA

View Full Version : Bean property from resource


sergek
Jan 22nd, 2005, 05:33 PM
I'm migrating from my hand-glued IoC beans to using the Spring framework. I have this one bean that retrieves emails, processes them, and stores them (for archival purposes) in a directory under my WEB-INF/archive path.

In my current glue code, I do:

MyBean bean = new MyBean();
bean.setArchivePath(servletContext.getRealPath("/WEB-INF/archive"));
...


With Spring I'm using org.springframework.web.context.ContextLoaderListe ner to configure and load my beans from /WEB-INF/applicationContext.xml.

I can see how (programmatically) I can grab the servlet context, or even better move to the Spring's Resource API to reference the archive directory, and the WebApplicationContext will translate my path to the appropriate path, which is great.

However, I cannot figure out how to populate this bean value based on notation in my spring xml file. Is there some wrapper class or lookup notation to grab a resource and set it as my bean property?

irbouho
Jan 22nd, 2005, 05:54 PM
Spring has build in support for Resource injection (org.springframework.core.io.Resource). Following declaration injects a resource into bean:
<bean id="myBean" class="MyBean">
<property name="archivePath">
<value>WEB-INF/archive</value>
</property>
</bean>
</bean>
archivePath needs to implement the Resource interface.
HTH

sergek
Jan 22nd, 2005, 06:08 PM
archivePath needs to implement the Resource interface.

Thank you for the quick and valuable response. The above piece is what I was missing. It's too bad that I have to tie my bean code to Spring, but this is a small change and works great. Thanks again!