Sha*_*rky 5 favicon spring-boot
(我研究了类似的问题,但没有一个能解释我在这个问题末尾说明的奇怪行为。)
\n\n我有一个 Spring Boot 1.3.5 应用程序,它坚持用 Boot 的默认图标(绿叶)替换我的图标。为了解决这个问题,我尝试了以下方法:
\n\n街上的传言是,这应该行得通。不幸的是,事实并非如此。
\n\nspring\xe2\x80\x8b.\xe2\x80\x8bmvc\xe2\x80\x8b.\xe2\x80\x8bfavicon\xe2\x80\x8b.\xe2\x80\x8benabled为 false。这应该禁用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@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}\nRun Code Online (Sandbox Code Playgroud)可悲的是,这里也没有运气。
\n\n现在,通过这个潜在的修复,我发现了一个令人惊讶的结果。当我curl编辑新命名的icon.ico资源时,我看到了 Spring 的叶子图标。然而,当我删除资源时,我得到了404。但是当我把它放回去时,我又得到了叶子!换句话说,当我的静态资源丢失时,Spring Boot 很乐意回答 404,但是当静态资源存在时,它总是会用叶子来回答!
顺便说一句,同一文件夹中的其他静态资源(PNG、JPG 等)也可以正常使用。
\n\n很容易想象,当我拔掉头发时,有一些邪恶的 Spring Boot 贡献者会为此嘲笑自己。:-)
\n\n我没主意了。任何人?
\n\n作为最后的手段,我可能会放弃使用 ICO 文件作为我的网站图标,而使用 PNG 来代替,但这是有代价的(失去多分辨率支持),所以我宁愿避免这种情况。
\n这是 Spring Boot 的一个特性:
\n\nSpring MVC 自动配置
\n\nSpring Boot 为 Spring MVC 提供自动配置,适用于大多数应用程序。
\n\n自动配置在 Spring\xe2\x80\x99s 默认值之上添加了以下功能:
\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,您可以将此配置添加到您yml或perperties文件中
spring.mvc.favicon.enabled=true # Enable resolution of favicon.ico.\nRun 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}\nRun Code Online (Sandbox Code Playgroud)\n\n您可以在以下位置找到更多详细信息:Spring Boot:覆盖 favicon
\n\n更新:
\n\n将 favicon.ico 放入资源文件夹。
\n\n\n\n并且,尝试一下:
\n\n\n| 归档时间: |
|
| 查看次数: |
9028 次 |
| 最近记录: |