我今天来分享一下我打包项目遇到的一个坑,就是一道早起来昨天没干完的项目干完然后终于到了打包的阶段我匆匆忙忙的先mvn clean一下《以防万一》再来mvn package 一下此打包过程中出现了以下错误并且中断了打包
Error starting ApplicationContext. To display the conditions report re-run your application
with 'debug' enabled. 2023-10-06 11:01:12.247 ERROR 27480 --- [ main] o.s.boot.SpringApplication : Application run failed java.lang.IllegalStateException: Failed
to execute ApplicationRunner at
org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762) ~[spring-
boot-2.7.6.jar:2.7.6] at
org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:749) ~
[spring-boot-2.7.6.jar:2.7.6] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-
2.7.6.jar:2.7.6] at
org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContext
Loader.java:136) [spring-boot-test-2.7.6.jar:2.7.6] at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextIn
ternal(DefaultCacheAwareContextLoaderDelegate.java:141) [spring-test-5.3.24.jar:5.3.24] at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(D
efaultCacheAwareContextLoaderDelegate.java:90) [spring-test-5.3.24.jar:5.3.24] at
org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTe
stContext.java:124) [spring-test-5.3.24.jar:5.3.24] at
org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNece
ssary(ServletTestExecutionListener.java:190) [spring-test-5.3.24.jar:5.3.24] at
org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(Servl
etTestExecutionListener.java:132) [spring-test-5.3.24.jar:5.3.24]。。。。
错误一部分
于是我看日志看到了以下一行内容
-
java.lang.IllegalStateException: Failed to execute ApplicationRunner 这是一个Java异常,表示执行ApplicationRunner失败。ApplicationRunner是Spring Boot提供的一个接口,用于在应用程序启动后执行一些初始化操作。
-
我此时恍然大悟打开看了我写的项目源码发现了自己springboot生命周期里面写的当spring程序启动时候默认用工具类发一个请求把token存到redis中,
-
@Component public class MyApplicationRunner implements ApplicationRunner { // 打包时候必须启动本地redis不然会打包失败 @Autowired private RedisTemplate redisTemplate; // 在这里执行一次性初始化操作,获取token等 @Override public void run(ApplicationArguments args) throws Exception { // 这操作应该在启动服务时候执行一次 String accessToken = TokenUtil.getAccessToken(); redisTemplate.opsForValue().set("accessToken", accessToken, 110, TimeUnit.MINUTES); } }
然后我发现本地redis我已经关了,我发现问题所在以后试着启动本地redis重新打包一下竟然打包成功了,然后我确定了我的猜测是对的,然后代码中出现了如下注释
// 打包时候必须启动本地redis不然会打包失败 @Autowired private RedisTemplate redisTemplate;
哈哈靠自己成功发现问题和解决问题当然记录一下呀。