I have a class that must be initialized with an absolute pathname. The thing I want to initialize it with the pathname of is a file sitting in WEB-INF.
I am using the ContextLoaderListener from Spring to set this all into motion, so I can’t run Java code to obtain the path from the context and stick it where a ${whatever} could find it.
Consider some bean definition like:
<bean class="my.class">
<property name="somePath" value="/WEB-INF/a.txt"/>
</bean>
I need a way, if possible, to make that pathname pass through the ServletContextResource mechanism. There doesn’t seem to be a ‘prefix’ for those like classpath:
In this case, it won’t help to have the item in the classpath, trust me.
EDIT:
I went and dug up the source of the bean class, and it already accepts Resources on the relevant properties. So Something Ridiculous is going on here, insofar as it complains as if it can’t find things. Off to the debugger for me.
EDIT AGAIN:
So, this turns out to be a maven prank, unrelated to Spring. It’s upvotes all around for your help, and open another question.
My preference would be to modify this class to take a
Resource, rather than a pathname. You can then inject it using:This is more flexible, and you can use the various methods on the
Resourceinterface to get things like the underlying file pathname, such asgetFile().getAbsolutePath().However, if modifying the target class is not feasible, then you need some way of converting a
Resourceinto a pathname. You could use aFactoryBeanfor this, something like:You can then use it to inject your path: