Spring Boot类路径 [英] Spring Boot classpath

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

问题描述

此处的Spring Boot文档关于提供静态内容,内容为:

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


默认情况下,Spring Boot将在 classpath
中从名为/ static(或/ public或/ resources或/ META-INF / resources)的目录
中提供静态内容。 / p>

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

将复制到 classpath ,因此我可以将静态内容放入:

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天全站免登陆