简体   繁体   English

Spring 引导类路径

[英]Spring Boot classpath

In the Spring Boot's docs here , about serving static content, it says:此处的 Spring 引导文档中,关于提供 static 内容,它说:

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 引导将提供来自 classpath 中名为 /static (或 /public 或 /resources 或 /META-INF/resources )目录的static内容。

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:将被复制到类路径中,所以我可以将我的 static 内容放在:

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目录下拥有我的 static 内容。

But, I have some questions about this:但是,我对此有一些疑问:

  1. 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)?为什么文档没有说将 static 内容放在src/main/resources/static中而不是谈论类路径(我认为这有点令人困惑)?
  2. Is it good to assume that the content in src/main/resources/ will be always copied in the classpath?假设src/main/resources/中的内容将始终复制到类路径中是否很好?
  3. 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/ )?是否有一些 Spring Boot 官方文档解释了除了 Java 类和包之外我应该在类路径中找到的内容(到目前为止,我只知道我可以从src/main/resources/找到所有内容)?

/src/main/resources is a Maven project structure convention. /src/main/resourcesMaven项目结构约定。 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.在构建步骤中,Maven 会将文件放入其中并将它们放置在适当的位置,以便您在运行时类路径中使用它们,例如在可执行文件.jar ,类路径中使用的某些物理文件系统位置(带有java-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.在这种情况下, /src/main/resources将不存在。 However, the intention is for the classpath to be the same, ie.但是,目的是使类路径相同,即。 to contain the same resources and .class files.包含相同的资源和.class文件。

The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up. Spring boot 文档讨论了类路径,因为它不应该对您的项目是如何设置的做出假设。

The classpath also contains additional libraries (JARs), which also can have a static folder, which would then be included for serving static resources.类路径还包含额外的库 (JAR),它也可以有一个static文件夹,然后将包含该文件夹以提供静态资源。 So if the documentation would only state the folder src/main/resources/static , it would be incomplete.因此,如果文档只说明文件夹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 .广告 3:也许从官方 Oracle 文档开始: 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.提示:当然,不仅是classpath中的resources文件夹的内容,当然还有所有编译好的类,因此得名。

To add to previous answers, it's the Spring Boot Maven Plugin (spring-boot-maven-plugin in the pom.xml ) that that enables you to run the application using Maven. To add to previous answers, it's the Spring Boot Maven Plugin (spring-boot-maven-plugin in the pom.xml ) that that enables you to run the application using Maven. One of its functions is to ensure contents in the app including dependency libraries are available on the runtime classpath, such as within the the executable JAR file.它的功能之一是确保应用程序中的内容(包括依赖库)在运行时类路径中可用,例如在可执行 JAR 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM