13

I've seen some Spring code that read config files and other resources directly off the runtime classpath using the classpath:/some/path/to/resource URL protocol.

Is this a Spring construct or a Java construct?

I can't find any documentation besides this question - URL to load resources from the classpath in Java, which doesn't indicate either way.

If it's a Java construct, can anyone point me to its official documentation?

4

2 回答 2

13

Well you can always register URL handlers. Java also has a file:/// and jar: handler. Also class.getResource will by default read from the classpath.

http://code.google.com/p/madura-classpath-protocol-handler/

apparently it is a spring feature.

"You can see these standard handlers, and associated implementation classes,in the JDK's RT.JAR file. Look for classes whose fully-qualified name starts with sun.net.www.protocol.For example,the class sun.net.www.protocol.http.Handler defines the HTTP protocol handler. Class sun.net.www.protocol.ftp.Handler defines the FTP protocol handler class."

http://java.sun.com/developer/onlineTraining/protocolhandlers/

"Exception in thread "main" java.net.MalformedURLException: unknown protocol: classpath" (says java 1.6)

于 2012-08-17T13:20:07.687 回答
6

类路径:特定于spring。Spring 的资源解析机制(即PathMatchingResourcePatternResolver或其他实现)知道"classpath:"和“classpath*:”前缀。

它接受并解析为ClassPathResource对象,这些对象恰好实现了 springsResource接口。

Resource接口,除其他外,有一个方法getInputStream()可以用来获取内容,而不必知道它是什么类型的资源。

这与任何 URL 协议处理完全分开,因此您不一定能够直接将其添加为协议处理程序。

但是,您可以将ClassPathResource类本身用作协议处理程序的一部分。

于 2012-08-17T15:33:22.023 回答