为什么 Spring Boot 将我所有的图标替换为 Spring 的叶子图标?

Sha*_*rky 5 favicon spring-boot

(我研究了类似的问题,但没有一个能解释我在这个问题末尾说明的奇怪行为。)

\n\n

我有一个 Spring Boot 1.3.5 应用程序,它坚持用 Boot 的默认图标(绿叶)替换我的图标。为了解决这个问题,我尝试了以下方法:

\n\n
    \n
  1. 在我的应用程序的静态根目录中安装我自己的图标。
  2. \n
\n\n

街上的传言是,这应该行得通。不幸的是,事实并非如此。

\n\n
    \n
  1. 将属性设置spring\xe2\x80\x8b.\xe2\x80\x8bmvc\xe2\x80\x8b.\xe2\x80\x8bfavicon\xe2\x80\x8b.\xe2\x80\x8benabled为 false。
  2. \n
\n\n

这应该禁用org\xe2\x80\x8b.\xe2\x80\x8bspringframework\xe2\x80\x8b.\xe2\x80\x8bboot\xe2\x80\x8b.\xe2\x80\x8bautoconfigure\xe2\x80\x8b.\xe2\x80\x8bweb\xe2\x80\x8b.\xe2\x80\x8bWebMvcAutoConfiguration\xe2\x80\x8b.\xe2\x80\x8bWebMvcAutoConfigurationAdapter\xe2\x80\x8b.\xe2\x80\x8bFaviconConfiguration,它似乎负责服务 Boot\ 的默认图标。通过在该类中设置断点,我能够确认当该属性设置为 false 时,该类中定义的 bean 确实不会被创建。

\n\n

不幸的是,这也没有解决问题。

\n\n
    \n
  1. 实现我自己的图标处理程序:

    \n\n
    @Configuration\npublic class FaviconConfiguration {\n\n    @Bean\n    public SimpleUrlHandlerMapping faviconHandlerMapping() {\n        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();\n        mapping.setOrder(Integer.MIN_VALUE);\n        mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));\n        return mapping;\n    }\n\n    @Bean\n    protected ResourceHttpRequestHandler faviconRequestHandler() {\n        ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();\n        ClassPathResource classPathResource = new ClassPathResource("static/");\n        List<Resource> locations = Arrays.asList(classPathResource);\n        requestHandler.setLocations(locations);\n        return requestHandler;\n    }\n\n}\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
\n\n

可悲的是,这里也没有运气。

\n\n
    \n
  1. 将我的图标从 favicon.ico 重命名为 logo.ico,然后将我所有页面的图标链接指向该图标。
  2. \n
\n\n

现在,通过这个潜在的修复,我发现了一个令人惊讶的结果。当我curl编辑新命名的icon.ico资源时,我看到了 Spring 的叶子图标。然而,当我删除资源时,我得到了404。但是当我把它放回去时,我又得到了叶子!换句话说,当我的静态资源丢失时,Spring Boot 很乐意回答 404,但是当静态资源存在时,它总是会用叶子来回答!

\n\n

顺便说一句,同一文件夹中的其他静态资源(PNG、JPG 等)也可以正常使用。

\n\n

很容易想象,当我拔掉头发时,有一些邪恶的 Spring Boot 贡献者会为此嘲笑自己。:-)

\n\n

我没主意了。任何人?

\n\n

作为最后的手段,我可​​能会放弃使用 ICO 文件作为我的网站图标,而使用 PNG 来代替,但这是有代价的(失去多分辨率支持),所以我宁愿避免这种情况。

\n

Bee*_*isy 1

这是 Spring Boot 的一个特性:

\n\n

Spring MVC 自动配置

\n\n

Spring Boot 为 Spring MVC 提供自动配置,适用于大多数应用程序。

\n\n

自动配置在 Spring\xe2\x80\x99s 默认值之上添加了以下功能:

\n\n
    \n
  1. 包含 ContentNegotiatingViewResolver 和 BeanNameViewResolver\nbean。
  2. \n
  3. 支持提供静态资源,包括支持\nWebJars(见下文)。
  4. \n
  5. 自动注册 Converter、\nGenericConverter、Formatter beans。
  6. \n
  7. 支持 HttpMessageConverters\n(见下文)。
  8. \n
  9. 自动注册 MessageCodesResolver(参见下文)。
  10. \n
  11. 静态index.html 支持。
  12. \n
  13. 自定义网站图标支持。
  14. \n
  15. 自动使用 ConfigurableWebBindingInitializer bean(见下文)。
  16. \n
\n\n

您可以在以下位置找到此文档:http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

\n\n

并且,如果您想禁用 spring boot favicon,您可以将此配置添加到您ymlperperties文件中

\n\n
spring.mvc.favicon.enabled=true # Enable resolution of favicon.ico.\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者,如果您想将图标更改为您自己的。尝试这个:

\n\n
@Configuration\npublic static class FaviconConfiguration {\n\n@Bean\npublic SimpleUrlHandlerMapping faviconHandlerMapping() {\n    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();\n    mapping.setOrder(Integer.MIN_VALUE);\n    mapping.setUrlMap(Collections.singletonMap("mylocation/favicon.ico",\n            faviconRequestHandler()));\n    return mapping;\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以在以下位置找到更多详细信息:Spring Boot:覆盖 favicon

\n\n

更新:

\n\n

将 favicon.ico 放入资源文件夹。

\n\n

将 favicon.ico 放入资源中

\n\n

并且,尝试一下:

\n\n

图标

\n

  • 该问题正在寻找人们可能追求的、问题中尚未提及的其他想法。不幸的是,这个答案并不承认问题中已经给出了它试图提供的解决方案。此外,它提供的答案之一是错误的。您需要将 ....favicon.enabled 设置为 false,而不是 true。最后,这个答案提供了与图标配置无关的不必要的细节。 (2认同)