I want to inject the URL of a classpath resource in a way that does not create a dependency on Spring in the Bean. Meaning, the bean should not use Spring's interfaces/classes. How can I do that?
Asked
Active
Viewed 1.9k times
15
-
What do you mean by "URL", exactly? This is a rather artificial concept with classpath resources - what would you do with that URL? – skaffman Jan 02 '11 at 12:42
-
open a stream and read from it – IttayD Jan 02 '11 at 12:45
-
If it's a classpath resource, why not just inject the path itself? There's little value in Spring resource abstraction if you're not going to use it. – skaffman Jan 02 '11 at 12:48
-
@skaffman because it located through the classpath which means through the classloader used by the bean factory which is not the same as the classloader of the bean using the path (due to delegation) – IttayD Jan 03 '11 at 08:21
-
If you say so. I'm not convinced that's going to work. – skaffman Jan 03 '11 at 08:30
4 Answers
15
Spring is able to convert classpath:...
values into java.net.URL
implicitly:
public class Foo {
private URL url;
...
}
.
<bean class = "Foo">
<property name = "url" value = "classpath:..." />
</bean>

axtavt
- 239,438
- 41
- 511
- 482
-
2Caused by: org.springframework.core.convert.ConversionFailedException: Unable to convert value "classpath:etc/warmup.xml" from type 'java.lang.String' to type 'java.net.URL'; nested exception is java.net.MalformedURLException: unknown protocol: classpath – IttayD Jan 03 '11 at 08:20
-
@IttayD: Works fine for me in Spring 3.0.2. Note that path must point to the existing resource. – axtavt Jan 03 '11 at 12:56
-
I have spring 3.0.4. I looked at the source code of spring and can't see where it handled "classpath:" urls any different. From what I see it trivially tries to convert to URL. I couldn't find any URLStreamHandler implementation for "classpath" – IttayD Jan 04 '11 at 04:31
-
1@IttayD: Spring uses `PropertyEditor`'s for converting property values. `classpath:` to `URL` conversion is handled by [`URLEditor`](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/propertyeditors/URLEditor.html) – axtavt Jan 04 '11 at 12:37
5
Following on from axtavt's answer, if you will allow yourself Spring annotations in the bean, you can do it like this:
@Value("classpath:myClasspathLocation") private URL url;

Daniel Alexiuc
- 13,078
- 9
- 58
- 73
2
create your own implementation of a spring resource by extending the org.springframework.core.io.ClassPathResource like MyClasspathResource extends ClassPathResource and inject this type into your bean. Like this you do not have any dependency to spring and can later reimplement your resource with something else.
<bean class="myBean">
<property name="classPathType">
<bean class="org.test.bla.MyClasspathResource">
<constructor-arg index="0" value="classpath:/org/test/bla/MyUrl" />
</bean>
</property>
</bean>

guido
- 687
- 4
- 13
1
There is hardly anything non-spring that's equivalent to Spring's resource concept.
You could for example use Guava's InputSupplier as an alternative, but you are missing powerful standard spring features if you do.

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588