Java Spring Boot 类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34160419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot classpath
提问by Andrea
In the Spring Boot's docs here, about serving static content, it says:
在此处的Spring Boot 文档中,关于提供静态内容,它说:
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.
默认情况下,Spring Boot 将从类路径中名为 /static(或 /public 或 /resources 或 /META-INF/resources)的目录中提供静态内容。
I found that all the content in the directory:
我发现目录中的所有内容:
src/main/resources
will be copied inside the classpath, so I can put my static content in:
将被复制到classpath 中,所以我可以把我的静态内容放在:
src/main/resources/static
and all will work fine and I'm happy since I can have my static content under the src
directory.
一切都会正常工作,我很高兴,因为我可以在src
目录下拥有我的静态内容。
But, I have some questions about this:
但是,我对此有一些疑问:
- Why the documentation doesn't say to put static content in
src/main/resources/static
instead of speaking about the classpath (I think this is a bit confusing)? - Is it good to assume that the content in
src/main/resources/
will be always copied in the classpath? - Is there some Spring Boot official documentation explaining what I'm supposed to find in the classpath other than Java classes and packages (up to now I only know I can found all the content from
src/main/resources/
)?
- 为什么文档没有说要放入静态内容
src/main/resources/static
而不是谈论类路径(我认为这有点令人困惑)? - 假设 in 的内容
src/main/resources/
将始终复制到类路径中是否很好? - 是否有一些 Spring Boot 官方文档解释了我应该在类路径中找到什么而不是 Java 类和包(到目前为止我只知道我可以从 中找到所有内容
src/main/resources/
)?
采纳答案by Sotirios Delimanolis
/src/main/resources
is a Mavenproject structure convention. It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .jar
, some physical file system location used in the classpath (with java
's -cp
option), etc.
/src/main/resources
是Maven项目结构约定。这是项目中放置资源的路径。在构建步骤中,Maven 将在那里获取文件并将它们放置在适当的位置,以便您在运行时类路径中使用它们,例如在可执行文件中.jar
,在类路径中使用的某些物理文件系统位置(带有java
's-cp
选项)等.
I could choose to build my application myself or with a different build tool. In such a case, /src/main/resources
would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class
files.
我可以选择自己构建应用程序或使用不同的构建工具。在这种情况下,/src/main/resources
将不存在。但是,目的是使类路径相同,即。包含相同的资源和.class
文件。
The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.
Spring boot 文档讨论了类路径,因为它不应该对您的项目是如何设置的做出假设。
回答by dunni
The classpath also contains additional libraries (JARs), which also can have a static
folder, which would then be included for serving static resources. So if the documentation would only state the folder src/main/resources/static
, it would be incomplete.
类路径还包含额外的库 (JAR),它也可以有一个static
文件夹,然后将包含该文件夹以提供静态资源。因此,如果文档仅说明文件夹src/main/resources/static
,则它是不完整的。
Ad 2: As long as you don't mess with the default Maven configuration, then it's safe to assume this.
广告 2:只要您不弄乱默认的 Maven 配置,就可以安全地假设这一点。
Ad 3: Maybe start with the official Oracle documentation: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html. Hint: Of course, it's not only the contents of the resources folder, which are in the classpath, but also of course all compiled classes, hence its name.
广告 3:也许从官方 Oracle 文档开始:https: //docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html。提示:当然,不仅是classpath中的resources文件夹的内容,当然还有所有编译好的类,因此得名。