Spring:为类路径资源注入URL

Itt*_*ayD 15 java spring

我想以一种不在Bean中创建对Spring的依赖的方式注入类路径资源的URL.意思是,bean不应该使用Spring的接口/类.我怎样才能做到这一点?

axt*_*avt 15

Spring能够将classpath:...值转换为java.net.URL隐式:

public class Foo {
    private URL url;
    ...
}
Run Code Online (Sandbox Code Playgroud)

.

<bean class = "Foo">
    <property name = "url" value = "classpath:..." />
</bean>
Run Code Online (Sandbox Code Playgroud)

  • org.springframework.core.convert.ConversionFailedException::无法值转换而引起"类路径:ETC/warmup.xml"从类型"java.lang.String中"为类型"的java.net.URL"; 嵌套异常是java.net.MalformedURLException:未知协议:classpath (2认同)

Dan*_*iuc 5

继 axtavt 的回答之后,如果您允许自己在 bean 中使用 Spring 注释,则可以这样做:

@Value("classpath:myClasspathLocation") private URL url;
Run Code Online (Sandbox Code Playgroud)