首页 文章 精选 留言 我的

精选列表

搜索[SpringBoot],共4203篇文章
优秀的个人博客,低调大师

SpringBoot 配置静态文件缓存

yml配置 spring: resources: chain: strategy: content: enabled: true paths: /** cache: true compressed: false enabled: true cache: cachecontrol: cache-public: true 增加代码配置 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.servlet.resource.ResourceUrlProvider; @ControllerAdvice public class ControllerConfig { @Autowired ResourceUrlProvider resourceUrlProvider; @ModelAttribute("urls") public ResourceUrlProvider urls() { return this.resourceUrlProvider; } } 修改静态文件引入fangshi <link rel="stylesheet" href="${urls.getForLookupPath('/css/index/index.css')}"> 不足之处 只能对通过controller进入的页面生效,拦截器直接跳转的页面无效(跳转到页面的URL然后通过controller进入页面是有效的,直接进入页面是无效的) 当前版本 Spring Boot 5.1.5.RELEASE

优秀的个人博客,低调大师

springboot 日志问题记录

问题:新建工程busr,采用pandora boot,引入了需要的包,简单写了点代码发布,spring-boot启动报错: Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from jar:file:/opt/**.jar!/BOOT-INF/lib/log4j-slf4j-impl-2.6.2.jar!/). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.apache.logging.slf4j.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContextat org.springframework.util.Assert.isInstanceOf(Assert.java:346)at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:231)at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:97)at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartedEvent(LoggingApplicationListener.java:226)at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:205)at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:121)at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:63)at org.springframework.boot.SpringApplicationRunListeners.started(SpringApplicationRunListeners.java:48)at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) 看到错误 "LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback"大概意思是 可以remove掉logback 解决问题。 当时直接Google了下 Stack Overflow上有篇文章也说排掉logback的包即可。 于是 我这么做了http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/5231e3284419eb9b203064fc694ae66d.png 发布启动 一切貌似正常。 正当高兴之余,突然发现不打印日志了。很明显与我刚才的操作有关系,看来刚才解决问题的办法似乎不正确。静下来分析问题的根本原因:看看spring boot 是怎么加载日志的:源码中 onApplicationStartedEvent方法,在系统启动时会被调用,LoggingSystem获取当然的日志系统 private void onApplicationStartedEvent(ApplicationStartedEvent event) {this.loggingSystem = LoggingSystem.get(event.getSpringApplication().getClassLoader());this.loggingSystem.beforeInitialize();} 默认的会引入下面3个日志系统 如果没有任何配置的化 其默认的是LogbackLoggingSystemstatic {Map systems = new LinkedHashMap();systems.put("ch.qos.logback.core.Appender","org.springframework.boot.logging.logback.LogbackLoggingSystem");systems.put("org.apache.logging.log4j.core.impl.Log4jContextFactory","org.springframework.boot.logging.log4j2.Log4J2LoggingSystem");systems.put("java.util.logging.LogManager","org.springframework.boot.logging.java.JavaLoggingSystem");SYSTEMS = Collections.unmodifiableMap(systems);} 逐步跟踪到第一次系统报错的地方private LoggerContext getLoggerContext() {ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();Assert.isInstanceOf(LoggerContext.class, factory,String.format("LoggerFactory is not a Logback LoggerContext but Logback is on " "the classpath. Either remove Logback or the competing " "implementation (%s loaded from %s). If you are using " "WebLogic you will need to add 'org.slf4j' to " "prefer-application-packages in WEB-INF/weblogic.xml",factory.getClass(), getLocation(factory))); return (LoggerContext) factory;} 说明返回的factory不是logback的实例, StaticLoggerBinder.getSingleton().getLoggerFactory() 没有找到logback,所以这个时候开始怀疑StaticLoggerBinder 是否是因为包冲突,果然 如下图http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/a9fbe3063e81267c31ec925c4b5e8c35.png 默认用了slf4j-log4j12 里面的StaticLoggerBinder 类,而没有用logback的 所以才会报上面的启动错误。 找到问题原因了 下面就是排包http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/1c7a0bf723ae52db221267fd93e1d7d1.png 来至 com.alibaba.trip.tripspider:tripspider-httpclient:jar:1.0.0-SNAPSHOT:compile排掉即可 org.slf4jslf4j-log4j12 启动一切正常,久违的日志又有了。 思考:遇到问题 google一下找解决办法或许是最快的,但是有时候往往解决方案因人而异,可能并不完全正确,明白问题出现的根本原因找解决办法才是最靠谱的。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册