Here’s a quick tip for you.
Sometimes you need to inject a java.io.File from your classpath into a bean, but you don’t want to have to spell out the absolute path (even in a configuration file). Never fear. It’s easy:
<bean id="tagProviderResource" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="/htmlcleaner.xml" />
</bean>
<util:property-path id="tagProviderFile" path="tagProviderResource.file" />
<bean id="tagProvider" class="org.htmlcleaner.ConfigFileTagProvider">
<constructor-arg ref="tagProviderFile" />
</bean>
In the configuration above, I used ClassPathResource to find the htmlcleaner.xml resource on the classpath. Then I used the handy <util:property-path> tag to assign the resource’s file property its own ID. Finally, I inject the File using constructor injection.

By shan May 17, 2012 - 6:18 am
I got message like this
can you give some help??
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error creating bean with name ‘serverConfiguration’ defined in file [C:\springsource\vfabric-tc-server-developer-2.6.4.RELEASE\spring-insight-instance\wtpwebapps\myrestaurants-social\WEB-INF\classes\META-INF\spring\applicationContext.xml]: Cannot resolve reference to bean ‘serverConfigurationResourceFile’ while setting constructor argument;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘serverConfigurationResourceFile’: FactoryBean threw exception on object creation;
nested exception is org.springframework.beans.InvalidPropertyException: Invalid property ‘file’ of bean class [org.springframework.core.io.ClassPathResource]: Getter for property ‘file’ threw exception;
nested exception is java.lang.reflect.InvocationTargetException
By shan May 17, 2012 - 6:28 am
sorry my fault ^^;
By Oliver Gierke May 20, 2012 - 8:29 am
Why not simply take a Resource as argument in ConfigFileTagProvider’s constructor and configure the constructor-arg to value=”classpath:htmlcleaner.xml”? You could call getFile() on the resource object then. It might even work if the type of the constructor argument was File directly but I’m not sure about that and haven’t tried either ;)
By Willie Wheeler May 20, 2012 - 9:31 am
Hey Oliver. In this case the ConfigFileTagProvider is a third-party component, so I don’t have control over the constructor params. But I agree that that would be a simpler way to do it in cases where you have that control. It would involve of course exposing a Spring Framework object as part of the API, which may or may not be appropriate depending on the context.
I haven’t tried using classpath:htmlcleaner.xml for a File arg either. I’d be surprised if that worked, but I’ve been surprised before. :-) I’ll have to give that a try.