Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: Inject URL for classpath resource

Tags:

java

spring

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?

like image 692
IttayD Avatar asked Jan 02 '11 12:01

IttayD


People also ask

How do I get a resource URL?

Use the getResource() Function to Get Resource URL in Java txt. We will pass resource URLs in strings in the body of the getResource() function. The function then searches for the given resource string and returns an object containing a URL. As we can see, we stored the three files in the string URL.

What is classpath resource in spring?

ClassPathResource is a Resource implementation for class path resources. It supports resolution as java. io. File if the class path resource resides in the file system, but not for resources in a JAR.

Where is classpath in spring?

This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader. getResources(...) call), and then merged to form the final application context definition. So classpath: starts at the root of your classpath.


1 Answers

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>
like image 125
axtavt Avatar answered Sep 22 '22 02:09

axtavt