spring-boot - Spring Boot devtools - 有时重新加载的bean在重新启动时不会被拾取
问题描述
我在使用 spring-devtools 重启功能时遇到了一些问题。似乎有时由于未知原因更改并且重新加载的 bean 没有被拾取。
应用类:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
命令行运行器:
@Component
@RequiredArgsConstructor
@Slf4j
public class TestCommandLineRunner implements CommandLineRunner {
private final TestService testService;
@Override
public void run(String... args) throws Exception {
log.info("{}", testService.test());
}
}
更改并重新加载的服务:
@Service
public class TestService {
public int test() {
return 12; //I am changing this number for tests
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chrosciu</groupId>
<artifactId>devtools-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>devtools-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
通常第一次重新加载工作正常:
2019-09-15 12:08:31.788 INFO 16464 --- [ Thread-5] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE)
2019-09-15 12:08:32.121 INFO 16464 --- [ restartedMain] com.chrosciu.DemoApplication : Starting DemoApplication on chma1 with PID 16464 (C:\Users\chma\gitroot\devtools-test\target\classes started by chma in C:\Users\chma\gitroot\devtools-test)
2019-09-15 12:08:32.122 INFO 16464 --- [ restartedMain] com.chrosciu.DemoApplication : No active profile set, falling back to default profiles: default
2019-09-15 12:08:32.663 INFO 16464 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-15 12:08:32.664 INFO 16464 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-15 12:08:32.666 INFO 16464 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-15 12:08:32.725 INFO 16464 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-15 12:08:32.725 INFO 16464 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 600 ms
2019-09-15 12:08:32.833 INFO 16464 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-15 12:08:32.900 INFO 16464 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-09-15 12:08:32.924 INFO 16464 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-15 12:08:32.925 INFO 16464 --- [ restartedMain] com.chrosciu.DemoApplication : Started DemoApplication in 0.86 seconds (JVM running for 18.403)
2019-09-15 12:08:32.926 INFO 16464 --- [ restartedMain] com.chrosciu.TestCommandLineRunner : 8
2019-09-15 12:08:32.927 INFO 16464 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
但是第二次或第三次崩溃如下,其中包含有关丢失 bean 的信息:
2019-09-15 12:09:15.482 INFO 16464 --- [ Thread-21] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE)
2019-09-15 12:09:15.842 INFO 16464 --- [ restartedMain] com.chrosciu.DemoApplication : Starting DemoApplication on chma1 with PID 16464 (C:\Users\chma\gitroot\devtools-test\target\classes started by chma in C:\Users\chma\gitroot\devtools-test)
2019-09-15 12:09:15.843 INFO 16464 --- [ restartedMain] com.chrosciu.DemoApplication : No active profile set, falling back to default profiles: default
2019-09-15 12:09:16.396 INFO 16464 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-15 12:09:16.397 INFO 16464 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-15 12:09:16.398 INFO 16464 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-15 12:09:16.483 INFO 16464 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-15 12:09:16.483 INFO 16464 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 637 ms
2019-09-15 12:09:16.714 WARN 16464 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testCommandLineRunner' defined in file [C:\Users\chma\gitroot\devtools-test\target\classes\com\chrosciu\TestCommandLineRunner.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.chrosciu.TestService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2019-09-15 12:09:16.714 INFO 16464 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-09-15 12:09:16.723 INFO 16464 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-15 12:09:16.969 ERROR 16464 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.chrosciu.TestCommandLineRunner required a bean of type 'com.chrosciu.TestService' that could not be found.
Action:
Consider defining a bean of type 'com.chrosciu.TestService' in your configuration.
有谁知道发生了什么?任何帮助表示赞赏。
解决方案
可能是您用来编译所做更改的任何内容都是删除类文件,然后重新编译源代码,而不是在旧类文件的顶部写入新的类文件。如果删除类文件和重新编译源之间的时间大于 DevTools 的静默期,则应用程序将在没有更改的类的情况下重新启动 - DevTools 会认为您已删除它而不是更改它。
您可能希望增加静默期,以便 DevTools 在同一批次中看到重新编译的类文件的删除和创建。默认静默期为 400 毫秒。以下示例将其设置为 1 秒:
spring.devtools.restart.quiet-period=1s
您可以在文档的相关部分了解更多关于 DevTools 的静默期以及如何配置它的信息。
推荐阅读
- mysql - MySQL查询给工资低于平均工资的员工5%的奖金
- php - 如何在我的 wordpress 站点中解决这个问题致命错误:无法重新声明 true_plugins_activate()(之前在
- c# - 如何从 sqlite c# 中的以下查询中获取表名
- python - anaconda提示符中的Python命令不起作用
- javascript - Javascript Reactjs:有没有办法以表单形式运行 MySQL 查询
- c# - 服务器端 mp3 音高/键更改
- azure - 如何使用 Powershell 从 Azure APP 服务获取配置->应用程序设置
- python - 在pytorch中分解一批会导致不同的结果,为什么?
- javascript - Chrome 扩展 - 如何检测页面更新并重新加载扩展?
- r - 使用 mutate 和 rowSums 添加变量