Spring Boot 类路径 [英] Spring Boot classpath

查看:48
本文介绍了Spring Boot 类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot 的文档在这里,关于提供静态内容,它说:

In the Spring Boot's docs here, about serving static content, it says:

默认情况下,Spring Boot 将从目录中提供静态内容称为/static(或/public 或/resources 或/META-INF/resources)类路径.

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.

我发现目录中的所有内容:

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:

src/main/resources/static

一切正常,我很高兴,因为我可以在 src 目录下拥有我的静态内容.

and all will work fine and I'm happy since I can have my static content under the src directory.

但是,我对此有一些疑问:

But, I have some questions about this:

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

推荐答案

/src/main/resources 是一个 Maven 项目结构约定.这是项目中放置资源的路径.在构建步骤中,Maven 将把文件放在那里,并将它们放在适当的位置,以便您在运行时类路径中使用它们,例如在一个可执行文件 .jar 中,一些物理文件系统位置用于类路径(带有java-cp 选项)等

/src/main/resources is a Maven project 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 将不存在.但是,目的是使类路径相同,即.包含相同的资源和 .class 文件.

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.

Spring 启动文档讨论了类路径,因为它不应该对您的项目的设置方式做出假设.

The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.

这篇关于Spring Boot 类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆